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发送给 ...
随机推荐
- C#检查文件是否被占用
第一种方法: using System.IO; using System.Runtime.InteropServices; [DllImport("kernel32.dll")] ...
- 通过IP地址和子网掩码与运算计算相关地址
通过IP地址和子网掩码与运算计算相关地址 知道IP地址和子网掩码后可以算出 网络地址 广播地址 地址范围 本网有几台主机 例一:下面例子IP地址为192.168.100.5 子网掩码是255.255. ...
- idea中使用junit测试时使用Scanner类无法正常测试
解决办法是:在main函数中测试方可有效. public static void main(String[] args){ Scanner sc = new Scanner(System.in);// ...
- Lyft Level 5 Challenge 2018 - Elimination Round
A. King Escape 签. #include <bits/stdc++.h> using namespace std; ], y[]; int f1(int X, int Y) { ...
- Python 非递归遍历图
class Queue: def __init__(self,max_size): self.max_size = int(max_size) self.queue = [] def put(self ...
- 前端学习笔记之HTTP协议
阅读目录 一 HTTP协议简介 二 HTTP协议之请求Request 三 HTTP协议之响应Response 四 HTTP协议完整工作流程 五 HTTP协议关键性总结 六 自定义套接字分析HTTP协议 ...
- 20145301《网络对抗》Exp2 后门原理与实践
20145301<网络对抗>Exp2 后门原理与实践 基础问题回答 例举你能想到的一个后门进入到你系统中的可能方式? 系统或者某些软件自身留下的后门. 钓鱼网站等非正规网站上捆绑下载 例举 ...
- windows下hadoop安装配置(转载)
Windows平台安装配置Hadoop 步骤: 1. JDK安装(不会的戳这) 2. 下载hadoop2.5.2.tar.gz,或者自行去百度下载. 3. 下载hadooponwindows-mast ...
- postman 安装桌面版
https://github.com/postmanlabs/postman-app-support
- ssh connection refused
执行sudo apt-get install openssh-server命令安装SSH服务