/************************************************************************* * Python-websocket-server ha cking * Description: * Follow the python-websocket-server How to use, this lib is still considered to be used. * 2017-10-6 Shenzhen Nanshan Ping Shan village Zengjianfeng **************************** ********************************************/First, reference document: https://Github.com/zengjfos/python-websocket-serverSecond, client.html"Text/javascript">varws; function init () {//Connect to Web SocketWS =NewWebSocket ("ws://localhost:9001/"); //Set event handlers. //Connection WebSocket Server successful, open successfullyWs.onopen =function () {Output ("OnOpen"); }; //receive WebSocket server dataWs.onmessage =function (e) {//E.data contains received string.Output"OnMessage:"+e.data); }; //Close WebSocket ConnectionWs.onclose =function () {Output ("OnClose"); }; //websocket connection Error occurredWs.onerror =function (e) {output ("onerror"); Console.log (e)}; } //sends the contents of the current text box to the WebSocketfunction OnSubmit () {varinput = document.getElementById ("input"); //You can send a message to the Web Socket using Ws.send.ws.send (Input.value); Output ("Send:"+input.value); Input.value=""; Input.focus (); } //Close WebSocket Connectionfunction Oncloseclick () {ws.close (); } //displays the received data on the interface, replacing some characters that need to be escapedfunction Output (str) {varLog = document.getElementById ("Log"); varescaped = Str.replace (/&/,"&"). Replace (/</,"<"). Replace (/>/,">"). Replace (/"/, """); // "log.innerhtml= escaped +"<br>"+log.innerhtml; } </script> "init ();"> <!--Click Submit, call the OnSubmit function--<form onsubmit="OnSubmit (); return false;"> <!--input box for sending data--<input type="text"Id="input"> <input type="Submit"Value="Send"> <!--Click the Close button to close WebSocket Connect-<button onclick="Oncloseclick (); return false;">close</button> </form> <!--display send, receive data--<div id="Log"></div> </body> third, server.py # load Websocketserver module fromwebsocket_server Import Websocketserver # called forevery client connecting (after handshake) def new_client (client, server): print ("New client connected and was given ID%d"% client['ID']) # sent to all connections Server.send_message_to_all ("Hey All, a new client has joined US") # called forevery client disconnecting def client_left (client, server): print ("Client (%d) disconnected"% client['ID'] # Called when a client sends a message DEF message_received (client, server, message):ifLen (message) > $: Message= message[: $]+'..'Print ("Client (%d) said:%s"% (client['ID'], message) # Sent to all connections Server.send_message_to_all (message) # Server port Port=9001# Create WebSocket server server=Websocketserver (PORT) # has a device connected to Server.set_fn_new_client (new_client) # disconnect Server.set_fn_client_left (CLI Ent_left) # Received information server.set_fn_message_received (message_received) # Start listening server.run_forever ()
Python-websocket-server hacking