/*************************************************************************
* 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. Extjs之表单提交

    Extjs的三种提交方式: 表单Ajax提交,普通提交,单独Ajax提交: 表单Ajax提交(默认提交方式) 提交函数:当按下表单中的提交按钮时执行下面的 btn函数,按照表单的 name进行提交. ...

  2. scikit-learn 中常用的评估模型

    一,scikit-learn中常用的评估模型 1.评估分类模型: ​ 2.评估回归模型: ​ 二.常见模型评估解析: •对于二分类问题,可将样例根据其真实类别和分类器预测类别划分为:(T,F表示预测的 ...

  3. Java 强引用、软引用、弱引用、幻象引用有什么区别

    1)引用出现的根源 引用出现的根源是由于GC内存回收的基本原理.GC回收本质上是回收对象.目前比较流行的回收算法是可达性分析算法.从GC roots开始安装一定的逻辑判断一个对象是否可达,不可达的话就 ...

  4. WebApi_返回Post格式数据

    [HttpPost] public HttpResponseMessage Post([FromBody] DingTalkCallBack bodyMsg, string signature, st ...

  5. 【转载】package-info

    本文是转载,原文地址:http://strong-life-126-com.iteye.com/blog/806246 package-info.java对于经常使用外部包的程序员来说应该是熟悉陌生人 ...

  6. TED #06# Questioning the universe

    Stephen Hawking: Questioning the universe 1. 第一段: There is nothing bigger or older than the universe ...

  7. # 20145106 《Java程序设计》第2周学习总结

    教材学习内容总结 感觉这本书的第三章和c语言有很多共同之处.因为以前学过c所以看起来并没有十分费劲.虽然以前学习过c, 但是还是忘记了long整数占8个字节这件事情.另外我第一次接触布尔这个概念.了解 ...

  8. Ubuntu16.04 国内更新源

    在修改source.list之前要先备份 sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak 替换内容到source.list中 阿里云源: ...

  9. Execution Order for the ApiController

    Execution Order for the ApiController Assuming the request goes into the ApiController scope, the op ...

  10. 一些常用的JavaScript正则表达式

    1.正数,最多n位小数 /^(([1-9]\d*(\.\d{1,n})?)|(0\.\d{1,n}))$/ 2.手机号码 /^1[34578]\d{9}$/