python-websocket-server hacking
生活随笔
收集整理的這篇文章主要介紹了
python-websocket-server hacking
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
/************************************************************************** python-websocket-server hacking* 說明:* 跟一下python-websocket-server怎么使用,這個(gè)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 Socketws = new WebSocket("ws://localhost:9001/");// Set event handlers.// 連接WebSocket服務(wù)器成功,打開成功ws.onopen = function() {output("onopen");};// 收到WebSocket服務(wù)器數(shù)據(jù)ws.onmessage = function(e) {// e.data contains received string.output("onmessage: " + e.data);};// 關(guān)閉WebSocket連接ws.onclose = function() {output("onclose");};// WebSocket連接出現(xiàn)錯(cuò)誤ws.onerror = function(e) {output("onerror");console.log(e)};}// 將當(dāng)前文本框中的內(nèi)容發(fā)送到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();}// 關(guān)閉WebSocket連接
function onCloseClick() {ws.close();}// 在界面上顯示接收到的數(shù)據(jù),將替換掉一些需要轉(zhuǎn)義的字符
function output(str) {var log = document.getElementById("log");var escaped = str.replace(/&/, "&").replace(/</, "<").replace(/>/, ">").replace(/"/, """); // "log.innerHTML = escaped + "<br>" + log.innerHTML;}</script></head><!-- 文檔加載完畢之后,會(huì)調(diào)用init函數(shù)進(jìn)行處理 --><body οnlοad="init();"><!-- 點(diǎn)擊submit之后,調(diào)用onSubmit函數(shù) --><form οnsubmit="onSubmit(); return false;"><!-- 發(fā)送數(shù)據(jù)的輸入框 --><input type="text" id="input"><input type="submit" value="Send"><!-- 點(diǎn)擊關(guān)閉按鈕關(guān)閉WebSocket連接 --><button οnclick="onCloseClick(); return false;">close</button></form><!-- 顯示發(fā)送、接收到數(shù)據(jù) --><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'])# 發(fā)送給所有的連接server.send_message_to_all("Hey all, a new client has joined us")# Called for every client disconnectingdef client_left(client, server):print("Client(%d) disconnected" % client['id'])# Called when a client sends a messagedef message_received(client, server, message):if len(message) > 200:message = message[:200]+'..'print("Client(%d) said: %s" % (client['id'], message))# 發(fā)送給所有的連接server.send_message_to_all(message)# Server PortPORT=9001# 創(chuàng)建Websocket Serverserver = WebsocketServer(PORT)# 有設(shè)備連接上了server.set_fn_new_client(new_client)# 斷開連接server.set_fn_client_left(client_left)# 接收到信息server.set_fn_message_received(message_received)# 開始監(jiān)聽server.run_forever()
?
轉(zhuǎn)載于:https://www.cnblogs.com/zengjfgit/p/7631989.html
總結(jié)
以上是生活随笔為你收集整理的python-websocket-server hacking的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (转)OOP(面向对象编程)的几大原则
- 下一篇: Python rfind()方法