python 版websocket实现
ubuntu下python2.76
windows python 2.79, chrome37 firefox35通过
代码是在别人(cddn有人提问)基础上改的, 主要改动了parsedata和sendmessage这2个函数.
改代码参考下面了这段文档. 主要是第5条, 发送的数据长度分别是 8bit和 16bit和 64 bit(即 127, 65535,和2^64-1)三种情况
发送和收取是一样的, 例如
1.长度小于125时(由于使用126, 127用作标志位.)
2. 数据长度在128-65525之间时, Payload Length位设为126, 后面额外使用16bit表示长度(前面的126不再是长度的一部分)
3.数据长度在65526-2^64-1之间时, Payload Length位设为127, 后面额外使用64bit表示长度(前面的127不再是长度的一部分)
- Fin (bit 0): determines if this is the last frame in the message. This would be set to 1 on the end of a series of frames, or in a single-frame message, it would be set to 1 as it is both the first and last frame.
 - RSV1, RSV2, RSV3 (bits 1-3): these three bits are reserved for websocket extensions, and should be 0 unless a specific extension requires the use of any of these bytes.
 Opcode (bits 4-7): these four bits deterimine the type of the frame. Control frames communicate WebSocket state, while non-control frames communicate data. The various types of codes include:
- x0: continuation frame; this frame contains data that should be appended to the previous frame
 - x1: text frame; this frame (and any following) contains text
 - x2: binary frame; this frame (and any following) contains binary data
 - x3 - x7: non-control reserved frames; these are reserved for possible websocket extensions
 - x8: close frame; this frame should end the connection
 - x9: ping frame
 - xA: pong frame
 - xB - xF: control reserved frames
 
- Mask (bit 8): this bit determines whether this specific frame uses a mask or not.
 - Payload Length (bits 9-15, or 16-31, or 16-79): these seven bytes determine the payload length. If the length is 126, the length is actually determined by bits 16 through 31 (that is, the following two bytes). If the length is 127, the length is actually determined by bits 16 through 79 (that is, the following eight bytes).
 - Masking Key (the following four bytes): this represents the mask, if the Mask bit is set to 1.
 - Payload Data (the following data): finally, the data. The payload data may be sent over multiple frames; we know the size of the entire message by the payload length that was sent, and can append data together to form a single message until we receive the message with the Fin flag. Each consecutive payload, if it exists, will contain the 0 “continuation frame” opcode.
 
服务器
- #coding=utf8
 - #!/usr/bin/python
 - import struct,socket
 - import hashlib
 - import threading,random
 - import time
 - import struct
 - from base64 import b64encode, b64decode
 - connectionlist = {}
 - g_code_length = 0
 - g_header_length = 0
 - def hex2dec(string_num):
 - return str(int(string_num.upper(), 16))
 - def get_datalength(msg):
 - global g_code_length
 - global g_header_length
 - print (len(msg))
 - g_code_length = ord(msg[1]) & 127
 - received_length = 0;
 - if g_code_length == 126:
 - #g_code_length = msg[2:4]
 - #g_code_length = (ord(msg[2])<<8) + (ord(msg[3]))
 - g_code_length = struct.unpack('>H', str(msg[2:4]))[0]
 - g_header_length = 8
 - elif g_code_length == 127:
 - #g_code_length = msg[2:10]
 - g_code_length = struct.unpack('>Q', str(msg[2:10]))[0]
 - g_header_length = 14
 - else:
 - g_header_length = 6
 - g_code_length = int(g_code_length)
 - return g_code_length
 - def parse_data(msg):
 - global g_code_length
 - g_code_length = ord(msg[1]) & 127
 - received_length = 0;
 - if g_code_length == 126:
 - g_code_length = struct.unpack('>H', str(msg[2:4]))[0]
 - masks = msg[4:8]
 - data = msg[8:]
 - elif g_code_length == 127:
 - g_code_length = struct.unpack('>Q', str(msg[2:10]))[0]
 - masks = msg[10:14]
 - data = msg[14:]
 - else:
 - masks = msg[2:6]
 - data = msg[6:]
 - i = 0
 - raw_str = ''
 - for d in data:
 - raw_str += chr(ord(d) ^ ord(masks[i%4]))
 - i += 1
 - print (u"总长度是:%d" % int(g_code_length))
 - return raw_str
 - def sendMessage(message):
 - global connectionlist
 - message_utf_8 = message.encode('utf-8')
 - for connection in connectionlist.values():
 - back_str = []
 - back_str.append('\x81')
 - data_length = len(message_utf_8)
 - if data_length <= 125:
 - back_str.append(chr(data_length))
 - elif data_length <= 65535 :
 - back_str.append(struct.pack('b', 126))
 - back_str.append(struct.pack('>h', data_length))
 - #back_str.append(chr(data_length >> 8))
 - #back_str.append(chr(data_length & 0xFF))
 - #a = struct.pack('>h', data_length)
 - #b = chr(data_length >> 8)
 - #c = chr(data_length & 0xFF)
 - elif data_length <= (2^64-1):
 - #back_str.append(chr(127))
 - back_str.append(struct.pack('b', 127))
 - back_str.append(struct.pack('>q', data_length))
 - #back_str.append(chr(data_length >> 8))
 - #back_str.append(chr(data_length & 0xFF))
 - else :
 - print (u'太长了')
 - msg = ''
 - for c in back_str:
 - msg += c;
 - back_str = str(msg) + message_utf_8#.encode('utf-8')
 - #connection.send(str.encode(str(u"\x00%s\xFF\n\n" % message))) #这个是旧版
 - #print (u'send message:' + message)
 - if back_str != None and len(back_str) > 0:
 - print (back_str)
 - connection.send(back_str)
 - def deleteconnection(item):
 - global connectionlist
 - del connectionlist['connection'+item]
 - class WebSocket(threading.Thread):#继承Thread
 - GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
 - def __init__(self,conn,index,name,remote, path="/"):
 - threading.Thread.__init__(self)#初始化父类Thread
 - self.conn = conn
 - self.index = index
 - self.name = name
 - self.remote = remote
 - self.path = path
 - self.buffer = ""
 - self.buffer_utf8 = ""
 - self.length_buffer = 0
 - def run(self):#重载Thread的run
 - print('Socket%s Start!' % self.index)
 - headers = {}
 - self.handshaken = False
 - while True:
 - if self.handshaken == False:
 - print ('Socket%s Start Handshaken with %s!' % (self.index,self.remote))
 - self.buffer += bytes.decode(self.conn.recv(1024))
 - if self.buffer.find('\r\n\r\n') != -1:
 - header, data = self.buffer.split('\r\n\r\n', 1)
 - for line in header.split("\r\n")[1:]:
 - key, value = line.split(": ", 1)
 - headers[key] = value
 - headers["Location"] = ("ws://%s%s" %(headers["Host"], self.path))
 - key = headers['Sec-WebSocket-Key']
 - token = b64encode(hashlib.sha1(str.encode(str(key + self.GUID))).digest())
 - handshake="HTTP/1.1 101 Switching Protocols\r\n"\
 - "Upgrade: websocket\r\n"\
 - "Connection: Upgrade\r\n"\
 - "Sec-WebSocket-Accept: "+bytes.decode(token)+"\r\n"\
 - "WebSocket-Origin: "+str(headers["Origin"])+"\r\n"\
 - "WebSocket-Location: "+str(headers["Location"])+"\r\n\r\n"
 - self.conn.send(str.encode(str(handshake)))
 - self.handshaken = True
 - print ('Socket %s Handshaken with %s success!' %(self.index, self.remote))
 - sendMessage(u'Welcome, ' + self.name + ' !')
 - self.buffer_utf8 = ""
 - g_code_length = 0
 - else:
 - global g_code_length
 - global g_header_length
 - mm=self.conn.recv(128)
 - if len(mm) <= 0:
 - continue
 - if g_code_length == 0:
 - get_datalength(mm)
 - #接受的长度
 - self.length_buffer = self.length_buffer + len(mm)
 - self.buffer = self.buffer + mm
 - if self.length_buffer - g_header_length < g_code_length :
 - continue
 - else :
 - self.buffer_utf8 = parse_data(self.buffer) #utf8
 - msg_unicode = str(self.buffer_utf8).decode('utf-8', 'ignore') #unicode
 - if msg_unicode=='quit':
 - print (u'Socket%s Logout!' % (self.index))
 - nowTime = time.strftime('%H:%M:%S',time.localtime(time.time()))
 - sendMessage(u'%s %s say: %s' % (nowTime, self.remote, self.name+' Logout'))
 - deleteconnection(str(self.index))
 - self.conn.close()
 - break #退出线程
 - else:
 - #print (u'Socket%s Got msg:%s from %s!' % (self.index, msg_unicode, self.remote))
 - nowTime = time.strftime(u'%H:%M:%S',time.localtime(time.time()))
 - sendMessage(u'%s %s say: %s' % (nowTime, self.remote, msg_unicode))
 - #重置buffer和bufferlength
 - self.buffer_utf8 = ""
 - self.buffer = ""
 - g_code_length = 0
 - self.length_buffer = 0
 - self.buffer = ""
 - class WebSocketServer(object):
 - def __init__(self):
 - self.socket = None
 - def begin(self):
 - print( 'WebSocketServer Start!')
 - self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 - self.socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
 - self.socket.bind(("127.0.0.1",12345))
 - self.socket.listen(50)
 - global connectionlist
 - i=0
 - while True:
 - connection, address = self.socket.accept()
 - username=address[0]
 - newSocket = WebSocket(connection,i,username,address)
 - newSocket.start() #开始线程,执行run函数
 - connectionlist['connection'+str(i)]=connection
 - i = i + 1
 - if __name__ == "__main__":
 - server = WebSocketServer()
 - server.begin()
 
客户端
测试了chrome37, firefox35
- <!DOCTYPE html>
 - <html>
 - <head>
 - <title>WebSocket</title>
 - <style>
 - html, body {
 - font: normal 0.9em arial, helvetica;
 - }
 - #log {
 - width: 440px;
 - height: 200px;
 - border: 1px solid #7F9DB9;
 - overflow: auto;
 - }
 - #msg {
 - width: 330px;
 - }
 - </style>
 - <script>
 - var socket;
 - function init() {
 - var host = "ws://127.0.0.1:12345/";
 - try {
 - socket = new WebSocket(host);
 - socket.onopen = function (msg) {
 - log('Connected');
 - };
 - socket.onmessage = function (msg) {
 - log(msg.data);
 - };
 - socket.onclose = function (msg) {
 - log("Lose Connection!");
 - };
 - }
 - catch (ex) {
 - log(ex);
 - }
 - $("msg").focus();
 - }
 - function send() {
 - var txt, msg;
 - txt = $("msg");
 - msg = txt.value;
 - if (!msg) {
 - alert("Message can not be empty");
 - return;
 - }
 - txt.value = "";
 - txt.focus();
 - try {
 - socket.send(msg);
 - } catch (ex) {
 - log(ex);
 - }
 - }
 - window.onbeforeunload = function () {
 - try {
 - socket.send('quit');
 - socket.close();
 - socket = null;
 - }
 - catch (ex) {
 - log(ex);
 - }
 - };
 - function $(id) {
 - return document.getElementById(id);
 - }
 - function log(msg) {
 - $("log").innerHTML += "<br>" + msg;
 - }
 - function onkey(event) {
 - if (event.keyCode == 13) {
 - send();
 - }
 - }
 - </script>
 - </head>
 - <body onload="init()">
 - <h3>WebSocket</h3>
 - <br><br>
 - <div id="log"></div>
 - <input id="msg" type="textbox" onkeypress="onkey(event)"/>
 - <button onclick="send()">发送</button>
 - </body>
 - </html>
 
python 版websocket实现的更多相关文章
- C#版Websocket实例
		
C#版Websocket实例 Demo地址:www.awbeci.xyz websocket有java.nodejs.python,Php等等版本,我使用的是C#版本,服务器端是Fleck,git ...
 - 【原】Learning Spark (Python版) 学习笔记(三)----工作原理、调优与Spark SQL
		
周末的任务是更新Learning Spark系列第三篇,以为自己写不完了,但为了改正拖延症,还是得完成给自己定的任务啊 = =.这三章主要讲Spark的运行过程(本地+集群),性能调优以及Spark ...
 - 数据结构:顺序表(python版)
		
顺序表python版的实现(部分功能未实现) #!/usr/bin/env python # -*- coding:utf-8 -*- class SeqList(object): def __ini ...
 - python版恶俗古风自动生成器.py
		
python版恶俗古风自动生成器.py """ python版恶俗古风自动生成器.py 模仿自: http://www.jianshu.com/p/f893291674c ...
 - python实现websocket服务器,可以在web实时显示远程服务器日志
		
一.开始的话 使用python简单的实现websocket服务器,可以在浏览器上实时显示远程服务器的日志信息. 之前做了一个web版的发布系统,但没实现在线看日志,每次发布版本后,都需要登录到服务器上 ...
 - LAMP一键安装包(Python版)
		
去年有出一个python整的LAMP自动安装,不过比较傻,直接调用的yum 去安装了XXX...不过这次一样有用shell..我也想如何不调用shell 来弄一个LAMP自动安装部署啥啥的..不过尼玛 ...
 - 编码的秘密(python版)
		
编码(python版) 最近在学习python的过程中,被不同的编码搞得有点晕,于是看了前人的留下的文档,加上自己的理解,准备写下来,分享给正在为编码苦苦了挣扎的你. 编码的概念 编码就是将信息从一种 ...
 - Zabbix 微信报警Python版(带监控项波动图片)
		
#!/usr/bin/python # -*- coding: UTF- -*- #Function: 微信报警python版(带波动图) #Environment: python import ur ...
 - 豆瓣top250(go版以及python版)
		
最近学习go,就找了一个例子练习[go语言爬虫]go语言爬取豆瓣电影top250,思路大概就是获取网页,然后根据页面元素,用正则表达式匹配电影名称.评分.评论人数.原文有个地方需要修改下patte ...
 
随机推荐
- pytest文档27-pytest分布式执行(pytest-xdist)
			
前言 平常我们手工测试用例非常多时,比如有1千条用例,假设每个用例执行需要1分钟.如果一个测试人员执行需要1000分钟才能执行完,当项目非常紧急的时候, 我们会用测试人力成本换取时间成本,这个时候多找 ...
 - Round #169 (Div. 2)C. Little Girl and Maximum Sum
			
1.用退化的线段树(也就是没有区间查询)做... 2.注意longlong. #include<cstdio> #include<cstring> #include<io ...
 - kafak-python函数使用详解
			
Consumer是非线程安全的 Kafka只保证消息不漏,即at lease once,而不保证消息不重.关键点:假如consumer挂了重启,那它将从committed offset位置(告诉ser ...
 - Flume-NG一些注意事项(转)
			
原文链接:记Flume-NG一些注意事项 这里只考虑flume本身的一些东西,对于JVM.HDFS.HBase等得暂不涉及.... 一.关于Source: 1.spool-source:适合静态文件, ...
 - 三个和数组有关的程序题目(C++)
			
题目一:有n个整数,使前面各数顺序向后移动m个位置 问题描述: 有n个整数,使前面各数顺序向后移动m个位置,最后m个数变成最前m个数 程序代码: #include<iostream> us ...
 - 关于asp.net页面缓存
			
1,ASPX页面缓存 页面缓存的使用方法非常的简单,只需要在aspx页的顶部加一句声明<%@ OutputCache Duration="60" VaryByParam=&q ...
 - UVA 12487 Midnight Cowboy(LCA+大YY)(好题)
			
题目pdf:http://acm.bnu.edu.cn/v3/external/124/12487.pdf 大致题意: 一棵树,一个人从A节点出发,等可能的选不论什么一条边走,有两个节点B,C求这个人 ...
 - 【转】NativeScript的工作原理:用JavaScript调用原生API实现跨平台
			
原文:https://blog.csdn.net/qq_21298703/article/details/44982547 -------------------------------------- ...
 - IIS7.5配置Asp.net项目出现HTTP 错误 404.17 - Not Found 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理。
			
近日在将一个Asp.net项目部署到IIS7.5上时却出现了HTTP 错误 404.17 - Not Found 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理. 因为IIS里面使用的都是默 ...
 - winform暴走表情制作器
			
c# winform暴走表情制作器,项目工程下载地址:项目下载地址 程序运行截图: 部分代码:获取鼠标所在的图片中坐标 private void pictureBox1_MouseDown(objec ...