/*************************************************************************
* 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) > :
message = message[:]+'..'
print("Client(%d) said: %s" % (client['id'], message)) # 发送给所有的连接
server.send_message_to_all(message) # Server Port
PORT=
# 创建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的更多相关文章

  1. python websocket网页实时显示远程服务器日志信息

    功能:用websocket技术,在运维工具的浏览器上实时显示远程服务器上的日志信息 一般我们在运维工具部署环境的时候,需要实时展现部署过程中的信息,或者在浏览器中实时显示程序日志给开发人员看.你还在用 ...

  2. Python websocket

    一.自己实现websocket 网上流传的都是Python2的websocket实现 # coding=utf8 # !/usr/bin/python import struct, socket im ...

  3. 运维开发:python websocket网页实时显示远程服务器日志信息

    功能:用websocket技术,在运维工具的浏览器上实时显示远程服务器上的日志信息 一般我们在运维工具部署环境的时候,需要实时展现部署过程中的信息,或者在浏览器中实时显示程序日志给开发人员看.你还在用 ...

  4. Python 处理server返回gzip内容

    Python 如何处理server返回gzip压缩过的内容,代码如下: from StringIO import StringIOimport gzip request = urllib2.Reque ...

  5. 小测几种python web server的性能

    http://blog.csdn.net/raptor/article/details/8038476 因为换了nginx就不再使用mod_wsgi来跑web.py应用了,现在用的是gevent-ws ...

  6. 使用Jetty搭建Java Websocket Server,实现图像传输

    https://my.oschina.net/yushulx/blog/298140 How to Implement a Java WebSocket Server for Image Transm ...

  7. SpringBoot报错:Failed to load ApplicationContext(javax.websocket.server.ServerContainer not available)

    引起条件: WebSocket+单元测试,单元测试报错! 解决方法: SpringBootTest增加webEnvironment参数. https://docs.spring.io/spring-b ...

  8. io.undertow.websockets.jsr.ServerWebSocketContainer cannot be cast to org.apache.tomcat.websocket.server.WsServerContainer

    Caused by: java.lang.ClassCastException: io.undertow.websockets.jsr.ServerWebSocketContainer cannot ...

  9. Python WebSocket长连接心跳与短连接

    python websocket 安装 pip install websocket-client 先来看一下,长连接调用方式: ws = websocket.WebSocketApp("ws ...

  10. Python Web Server Gateway Interface -- WSGI

    了解了HTTP协议和HTML文档,我们其实就明白了一个Web应用的本质就是: 浏览器发送一个HTTP请求: 服务器收到请求,生成一个HTML文档: 服务器把HTML文档作为HTTP响应的Body发送给 ...

随机推荐

  1. redis.conf配置文件说明

    # Redis 配置文件 # 当配置中需要配置内存大小时,可以使用 1k, 5GB, 4M 等类似的格式,其转换方式如下(不区分大小写) # # 1k => 1000 bytes # 1kb = ...

  2. 2018 Multi-University Training Contest 2 Solution

    A - Absolute 留坑. B - Counting Permutations 留坑. C - Cover 留坑. D - Game puts("Yes") #include ...

  3. Flask Web 开发 错误页面自定义

    如果要自定义错误画面,无法大多数情况是自定义404和500的情况 首先,要在路由中设置 通过app.error_handler这个装饰器来绑定响应函数 @main.errorhandler(404) ...

  4. Horizon代码的层次结构

    Horizon中包含多个dashboard,每个dashboard又包含多个panel,每个panel有可以包含多个Tab.

  5. “System.Runtime.InteropServices.COMException (0x80070422): 无法启动服务”解决方法

    应用程序中发生了无法处理的异常.如果单击“退出”,应用程序将立即关闭.无法启动服务,原因可能是已被禁用或其相关联设备没有启动.(异常来自HRESULT:0X80070422).点击详细内容:有关调用实 ...

  6. Django:用户登录实例

    Django:用户登录实例 一.源代码 1,login.html代码(登录界面): <!DOCTYPE html> <html lang="zh-CN"> ...

  7. hyper-v安装虚拟机ubuntu 18.04 64bit后无法使能增强模式怎么办

    1.获取脚本来使能增强模式 $ sudo apt-get update $ sudo apt install git $ git clone https://github.com/jterry75/x ...

  8. CCleaner如何禁用开机自动启动

    https://forum.piriform.com/topic/42073-ccleaner-starts-on-startup/ 在options-->setting里面选择开机不启动 在O ...

  9. [Opencv]图像的梯度与边缘检测(转)

    文章来源:https://blog.csdn.net/on2way/article/details/46851451 梯度简单来说就是求导,在图像上表现出来的就是提取图像的边缘(不管是横向的.纵向的. ...

  10. Markdown中的表格

    参考:在简书上用Markdown写表格 | Tables | Are | Cool | | ------------- |:-------------:| -----:| | col 3 is | r ...