python-websocket-server hacking
/*************************************************************************
* 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(/&/, "&").replace(/</, "<").
replace(/>/, ">").replace(/"/, """); // "
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的更多相关文章
- python websocket网页实时显示远程服务器日志信息
功能:用websocket技术,在运维工具的浏览器上实时显示远程服务器上的日志信息 一般我们在运维工具部署环境的时候,需要实时展现部署过程中的信息,或者在浏览器中实时显示程序日志给开发人员看.你还在用 ...
- Python websocket
一.自己实现websocket 网上流传的都是Python2的websocket实现 # coding=utf8 # !/usr/bin/python import struct, socket im ...
- 运维开发:python websocket网页实时显示远程服务器日志信息
功能:用websocket技术,在运维工具的浏览器上实时显示远程服务器上的日志信息 一般我们在运维工具部署环境的时候,需要实时展现部署过程中的信息,或者在浏览器中实时显示程序日志给开发人员看.你还在用 ...
- Python 处理server返回gzip内容
Python 如何处理server返回gzip压缩过的内容,代码如下: from StringIO import StringIOimport gzip request = urllib2.Reque ...
- 小测几种python web server的性能
http://blog.csdn.net/raptor/article/details/8038476 因为换了nginx就不再使用mod_wsgi来跑web.py应用了,现在用的是gevent-ws ...
- 使用Jetty搭建Java Websocket Server,实现图像传输
https://my.oschina.net/yushulx/blog/298140 How to Implement a Java WebSocket Server for Image Transm ...
- SpringBoot报错:Failed to load ApplicationContext(javax.websocket.server.ServerContainer not available)
引起条件: WebSocket+单元测试,单元测试报错! 解决方法: SpringBootTest增加webEnvironment参数. https://docs.spring.io/spring-b ...
- 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 ...
- Python WebSocket长连接心跳与短连接
python websocket 安装 pip install websocket-client 先来看一下,长连接调用方式: ws = websocket.WebSocketApp("ws ...
- Python Web Server Gateway Interface -- WSGI
了解了HTTP协议和HTML文档,我们其实就明白了一个Web应用的本质就是: 浏览器发送一个HTTP请求: 服务器收到请求,生成一个HTML文档: 服务器把HTML文档作为HTTP响应的Body发送给 ...
随机推荐
- R语言apply()函数用法
在R语言的帮助文档里,apply函数的功能是: Retruns a vector or array or list of values obtained by applying a function ...
- 怎样使用CSS设置文字与文字间距距离?
[文字与文字间距距离,字与字距离间距CSS如何设置?]如果你也遇到W3Cschool用户唐婷大小姐类似的问题不妨也到W3Cschool编程问答进行提问. 对于使用CSS解决字间距的方法W3Cschoo ...
- ruby md5 sha1 base64加密
#md5加密 require 'md5' puts MD5.hexdigest('admin') #sha1加密 require 'digest/sha1' puts Digest::SHA1.hex ...
- hdu5021 树状数组+二分
这 题 说 的 是 给 了 一 个 K—NN 每次查询离loc 最近的k个数 然后将这k个数的权值加起来除以k 赋值给 loc 这个位置上的 权值 我说说 我的做法 假如 查询的是loc 这个 ...
- C#——文件上传(一般处理程序ashx)
Framework版本:.Net Framework 4 1.FileInfo实体 using System; using System.Collections.Generic; using Syst ...
- RC1意思
软件各种版本的表示 alpha 内部测试版 beta 外部测试版 demo 演示版 Enhance 增强版或者加强版 属于正式版 Free 自由版 Full version 完全版 属于正式版 sha ...
- 根据源Excel文件,新建Excel文件
/** * 描述:根据源Excel文件,创建新的Excel文件 * @param excelFile * @throws CheckException */public static void cre ...
- Python3基础 print 自带换行
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- git将多个commit合并成一个新的commit
问题: 有以下commit: 323udd ede234 6e7s6e 要合并第一个和第二个commit 方法有二: 方法一 使用git rebase -i hash-id,-i表示以交互模式进行co ...
- python 进制转换
print hex(),hex(-) #转换成十六进制 print oct(),oct(-) #转换成八进制 print bin(),bin(-) #转换成二进制 print int("字面 ...