python-websocket-server hacking

來源:互聯網
上載者:User

標籤:open   tput   val   接收   https   text   替換   and   for   

/************************************************************************* *                  python-websocket-server hacking * 說明: *     跟一下python-websocket-server怎麼使用,這個lib還算是目前想用的。 *                               *                                      2017-10-6 深圳 南山平山村 曾劍鋒 ************************************************************************/一、參考文檔:    https://github.com/ZengjfOS/python-websocket-server二、client.html    <html>    <head>      <title>Simple client</title>          <script type="text/javascript">            var ws;                function init() {              // Connect to Web Socket          ws = new WebSocket("ws://localhost:9001/");              // Set event handlers.          // 串連WebSocket伺服器成功,開啟成功          ws.onopen = function() {            output("onopen");          };                    // 收到WebSocket伺服器資料          ws.onmessage = function(e) {            // e.data contains received string.            output("onmessage: " + e.data);          };                    // 關閉WebSocket串連          ws.onclose = function() {            output("onclose");          };              // WebSocket串連出現錯誤          ws.onerror = function(e) {            output("onerror");            console.log(e)          };            }                // 將當前文字框中的內容發送到WebSocket        function onSubmit() {          var input = document.getElementById("input");          // You can send message to the Web Socket using ws.send.          ws.send(input.value);          output("send: " + input.value);          input.value = "";          input.focus();        }                // 關閉WebSocket串連        function onCloseClick() {          ws.close();        }                // 在介面上顯示接收到的資料,將替換掉一些需要轉義的字元        function output(str) {          var log = document.getElementById("log");          var escaped = str.replace(/&/, "&amp;").replace(/</, "&lt;").            replace(/>/, "&gt;").replace(/"/, "&quot;"); // "          log.innerHTML = escaped + "<br>" + log.innerHTML;        }          </script>    </head>        <!-- 文檔載入完畢之後,會調用init函數進行處理 -->    <body onload="init();">      <!-- 點擊submit之後,調用onSubmit函數 -->      <form onsubmit="onSubmit(); return false;">        <!-- 發送資料的輸入框 -->        <input type="text" id="input">        <input type="submit" value="Send">        <!-- 點擊關閉按鈕關閉WebSocket串連 -->        <button onclick="onCloseClick(); return false;">close</button>      </form>      <!-- 顯示發送、接收到資料 -->      <div id="log"></div>    </body>    </html>三、server.py    # 載入WebsocketServer模組    from websocket_server import WebsocketServer        # Called for every client connecting (after handshake)    def new_client(client, server):        print("New client connected and was given id %d" % client[‘id‘])        # 發送給所有的串連        server.send_message_to_all("Hey all, a new client has joined us")            # Called for every 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):        if len(message) > 200:            message = message[:200]+‘..‘        print("Client(%d) said: %s" % (client[‘id‘], message))            # 發送給所有的串連        server.send_message_to_all(message)            # Server Port    PORT=9001    # 建立Websocket Server    server = WebsocketServer(PORT)    # 有裝置串連上了    server.set_fn_new_client(new_client)    # 中斷連線    server.set_fn_client_left(client_left)    # 接收到資訊    server.set_fn_message_received(message_received)    # 開始監聽    server.run_forever()

 

python-websocket-server hacking

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.