Python-websocket-server hacking

Source: Internet
Author: User

/************************************************************************* * 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 (/&/,"&amp;"). Replace (/</,"&lt;"). Replace (/>/,"&gt;"). Replace (/"/, "&quot;"); // "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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.