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不再是长度的一部分)

  1. 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.
  2. 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.
  3. 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:

    1. x0: continuation frame; this frame contains data that should be appended to the previous frame
    2. x1: text frame; this frame (and any following) contains text
    3. x2: binary frame; this frame (and any following) contains binary data
    4. x3 - x7: non-control reserved frames; these are reserved for possible websocket extensions
    5. x8: close frame; this frame should end the connection
    6. x9: ping frame
    7. xA: pong frame
    8. xB - xF: control reserved frames
  4. Mask (bit 8): this bit determines whether this specific frame uses a mask or not.
  5. 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).
  6. Masking Key (the following four bytes): this represents the mask, if the Mask bit is set to 1.
  7. 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.

服务器

  1. #coding=utf8
  2. #!/usr/bin/python
  3. import struct,socket
  4. import hashlib
  5. import threading,random
  6. import time
  7. import struct
  8. from base64 import b64encode, b64decode
  9. connectionlist = {}
  10. g_code_length = 0
  11. g_header_length = 0
  12. def hex2dec(string_num):
  13. return str(int(string_num.upper(), 16))
  14. def get_datalength(msg):
  15. global g_code_length
  16. global g_header_length
  17. print (len(msg))
  18. g_code_length = ord(msg[1]) & 127
  19. received_length = 0;
  20. if g_code_length == 126:
  21. #g_code_length = msg[2:4]
  22. #g_code_length = (ord(msg[2])<<8) + (ord(msg[3]))
  23. g_code_length = struct.unpack('>H', str(msg[2:4]))[0]
  24. g_header_length = 8
  25. elif g_code_length == 127:
  26. #g_code_length = msg[2:10]
  27. g_code_length = struct.unpack('>Q', str(msg[2:10]))[0]
  28. g_header_length = 14
  29. else:
  30. g_header_length = 6
  31. g_code_length = int(g_code_length)
  32. return g_code_length
  33. def parse_data(msg):
  34. global g_code_length
  35. g_code_length = ord(msg[1]) & 127
  36. received_length = 0;
  37. if g_code_length == 126:
  38. g_code_length = struct.unpack('>H', str(msg[2:4]))[0]
  39. masks = msg[4:8]
  40. data = msg[8:]
  41. elif g_code_length == 127:
  42. g_code_length = struct.unpack('>Q', str(msg[2:10]))[0]
  43. masks = msg[10:14]
  44. data = msg[14:]
  45. else:
  46. masks = msg[2:6]
  47. data = msg[6:]
  48. i = 0
  49. raw_str = ''
  50. for d in data:
  51. raw_str += chr(ord(d) ^ ord(masks[i%4]))
  52. i += 1
  53. print (u"总长度是:%d" % int(g_code_length))
  54. return raw_str
  55. def sendMessage(message):
  56. global connectionlist
  57. message_utf_8 = message.encode('utf-8')
  58. for connection in connectionlist.values():
  59. back_str = []
  60. back_str.append('\x81')
  61. data_length = len(message_utf_8)
  62. if data_length <= 125:
  63. back_str.append(chr(data_length))
  64. elif data_length <= 65535 :
  65. back_str.append(struct.pack('b', 126))
  66. back_str.append(struct.pack('>h', data_length))
  67. #back_str.append(chr(data_length >> 8))
  68. #back_str.append(chr(data_length & 0xFF))
  69. #a = struct.pack('>h', data_length)
  70. #b = chr(data_length >> 8)
  71. #c = chr(data_length & 0xFF)
  72. elif data_length <= (2^64-1):
  73. #back_str.append(chr(127))
  74. back_str.append(struct.pack('b', 127))
  75. back_str.append(struct.pack('>q', data_length))
  76. #back_str.append(chr(data_length >> 8))
  77. #back_str.append(chr(data_length & 0xFF))
  78. else :
  79. print (u'太长了')
  80. msg = ''
  81. for c in back_str:
  82. msg += c;
  83. back_str = str(msg)   + message_utf_8#.encode('utf-8')
  84. #connection.send(str.encode(str(u"\x00%s\xFF\n\n" % message))) #这个是旧版
  85. #print (u'send message:' +  message)
  86. if back_str != None and len(back_str) > 0:
  87. print (back_str)
  88. connection.send(back_str)
  89. def deleteconnection(item):
  90. global connectionlist
  91. del connectionlist['connection'+item]
  92. class WebSocket(threading.Thread):#继承Thread
  93. GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
  94. def __init__(self,conn,index,name,remote, path="/"):
  95. threading.Thread.__init__(self)#初始化父类Thread
  96. self.conn = conn
  97. self.index = index
  98. self.name = name
  99. self.remote = remote
  100. self.path = path
  101. self.buffer = ""
  102. self.buffer_utf8 = ""
  103. self.length_buffer = 0
  104. def run(self):#重载Thread的run
  105. print('Socket%s Start!' % self.index)
  106. headers = {}
  107. self.handshaken = False
  108. while True:
  109. if self.handshaken == False:
  110. print ('Socket%s Start Handshaken with %s!' % (self.index,self.remote))
  111. self.buffer += bytes.decode(self.conn.recv(1024))
  112. if self.buffer.find('\r\n\r\n') != -1:
  113. header, data = self.buffer.split('\r\n\r\n', 1)
  114. for line in header.split("\r\n")[1:]:
  115. key, value = line.split(": ", 1)
  116. headers[key] = value
  117. headers["Location"] = ("ws://%s%s" %(headers["Host"], self.path))
  118. key = headers['Sec-WebSocket-Key']
  119. token = b64encode(hashlib.sha1(str.encode(str(key + self.GUID))).digest())
  120. handshake="HTTP/1.1 101 Switching Protocols\r\n"\
  121. "Upgrade: websocket\r\n"\
  122. "Connection: Upgrade\r\n"\
  123. "Sec-WebSocket-Accept: "+bytes.decode(token)+"\r\n"\
  124. "WebSocket-Origin: "+str(headers["Origin"])+"\r\n"\
  125. "WebSocket-Location: "+str(headers["Location"])+"\r\n\r\n"
  126. self.conn.send(str.encode(str(handshake)))
  127. self.handshaken = True
  128. print ('Socket %s Handshaken with %s success!' %(self.index, self.remote))
  129. sendMessage(u'Welcome, ' + self.name + ' !')
  130. self.buffer_utf8 = ""
  131. g_code_length = 0
  132. else:
  133. global g_code_length
  134. global g_header_length
  135. mm=self.conn.recv(128)
  136. if len(mm) <= 0:
  137. continue
  138. if g_code_length == 0:
  139. get_datalength(mm)
  140. #接受的长度
  141. self.length_buffer = self.length_buffer + len(mm)
  142. self.buffer = self.buffer + mm
  143. if self.length_buffer - g_header_length < g_code_length :
  144. continue
  145. else :
  146. self.buffer_utf8 = parse_data(self.buffer) #utf8
  147. msg_unicode = str(self.buffer_utf8).decode('utf-8', 'ignore') #unicode
  148. if msg_unicode=='quit':
  149. print (u'Socket%s Logout!' % (self.index))
  150. nowTime = time.strftime('%H:%M:%S',time.localtime(time.time()))
  151. sendMessage(u'%s %s say: %s' % (nowTime, self.remote, self.name+' Logout'))
  152. deleteconnection(str(self.index))
  153. self.conn.close()
  154. break #退出线程
  155. else:
  156. #print (u'Socket%s Got msg:%s from %s!' % (self.index, msg_unicode, self.remote))
  157. nowTime = time.strftime(u'%H:%M:%S',time.localtime(time.time()))
  158. sendMessage(u'%s %s say: %s' % (nowTime, self.remote, msg_unicode))
  159. #重置buffer和bufferlength
  160. self.buffer_utf8 = ""
  161. self.buffer = ""
  162. g_code_length = 0
  163. self.length_buffer = 0
  164. self.buffer = ""
  165. class WebSocketServer(object):
  166. def __init__(self):
  167. self.socket = None
  168. def begin(self):
  169. print( 'WebSocketServer Start!')
  170. self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  171. self.socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
  172. self.socket.bind(("127.0.0.1",12345))
  173. self.socket.listen(50)
  174. global connectionlist
  175. i=0
  176. while True:
  177. connection, address = self.socket.accept()
  178. username=address[0]
  179. newSocket = WebSocket(connection,i,username,address)
  180. newSocket.start() #开始线程,执行run函数
  181. connectionlist['connection'+str(i)]=connection
  182. i = i + 1
  183. if __name__ == "__main__":
  184. server = WebSocketServer()
  185. server.begin()

客户端

测试了chrome37, firefox35

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>WebSocket</title>
  5. <style>
  6. html, body {
  7. font: normal 0.9em arial, helvetica;
  8. }
  9. #log {
  10. width: 440px;
  11. height: 200px;
  12. border: 1px solid #7F9DB9;
  13. overflow: auto;
  14. }
  15. #msg {
  16. width: 330px;
  17. }
  18. </style>
  19. <script>
  20. var socket;
  21. function init() {
  22. var host = "ws://127.0.0.1:12345/";
  23. try {
  24. socket = new WebSocket(host);
  25. socket.onopen = function (msg) {
  26. log('Connected');
  27. };
  28. socket.onmessage = function (msg) {
  29. log(msg.data);
  30. };
  31. socket.onclose = function (msg) {
  32. log("Lose Connection!");
  33. };
  34. }
  35. catch (ex) {
  36. log(ex);
  37. }
  38. $("msg").focus();
  39. }
  40. function send() {
  41. var txt, msg;
  42. txt = $("msg");
  43. msg = txt.value;
  44. if (!msg) {
  45. alert("Message can not be empty");
  46. return;
  47. }
  48. txt.value = "";
  49. txt.focus();
  50. try {
  51. socket.send(msg);
  52. } catch (ex) {
  53. log(ex);
  54. }
  55. }
  56. window.onbeforeunload = function () {
  57. try {
  58. socket.send('quit');
  59. socket.close();
  60. socket = null;
  61. }
  62. catch (ex) {
  63. log(ex);
  64. }
  65. };
  66. function $(id) {
  67. return document.getElementById(id);
  68. }
  69. function log(msg) {
  70. $("log").innerHTML += "<br>" + msg;
  71. }
  72. function onkey(event) {
  73. if (event.keyCode == 13) {
  74. send();
  75. }
  76. }
  77. </script>
  78. </head>
  79. <body onload="init()">
  80. <h3>WebSocket</h3>
  81. <br><br>
  82. <div id="log"></div>
  83. <input id="msg" type="textbox" onkeypress="onkey(event)"/>
  84. <button onclick="send()">发送</button>
  85. </body>
  86. </html>

参考:用Python实现一个简单的WebSocket服务器

由于使用125, 126, 127用作标志位.

python 版websocket实现的更多相关文章

  1. C#版Websocket实例

    C#版Websocket实例   Demo地址:www.awbeci.xyz websocket有java.nodejs.python,Php等等版本,我使用的是C#版本,服务器端是Fleck,git ...

  2. 【原】Learning Spark (Python版) 学习笔记(三)----工作原理、调优与Spark SQL

    周末的任务是更新Learning Spark系列第三篇,以为自己写不完了,但为了改正拖延症,还是得完成给自己定的任务啊 = =.这三章主要讲Spark的运行过程(本地+集群),性能调优以及Spark ...

  3. 数据结构:顺序表(python版)

    顺序表python版的实现(部分功能未实现) #!/usr/bin/env python # -*- coding:utf-8 -*- class SeqList(object): def __ini ...

  4. python版恶俗古风自动生成器.py

    python版恶俗古风自动生成器.py """ python版恶俗古风自动生成器.py 模仿自: http://www.jianshu.com/p/f893291674c ...

  5. python实现websocket服务器,可以在web实时显示远程服务器日志

    一.开始的话 使用python简单的实现websocket服务器,可以在浏览器上实时显示远程服务器的日志信息. 之前做了一个web版的发布系统,但没实现在线看日志,每次发布版本后,都需要登录到服务器上 ...

  6. LAMP一键安装包(Python版)

    去年有出一个python整的LAMP自动安装,不过比较傻,直接调用的yum 去安装了XXX...不过这次一样有用shell..我也想如何不调用shell 来弄一个LAMP自动安装部署啥啥的..不过尼玛 ...

  7. 编码的秘密(python版)

    编码(python版) 最近在学习python的过程中,被不同的编码搞得有点晕,于是看了前人的留下的文档,加上自己的理解,准备写下来,分享给正在为编码苦苦了挣扎的你. 编码的概念 编码就是将信息从一种 ...

  8. Zabbix 微信报警Python版(带监控项波动图片)

    #!/usr/bin/python # -*- coding: UTF- -*- #Function: 微信报警python版(带波动图) #Environment: python import ur ...

  9. 豆瓣top250(go版以及python版)

      最近学习go,就找了一个例子练习[go语言爬虫]go语言爬取豆瓣电影top250,思路大概就是获取网页,然后根据页面元素,用正则表达式匹配电影名称.评分.评论人数.原文有个地方需要修改下patte ...

随机推荐

  1. freecms怎样在信息列表类标签中提取info.content

    原文地址:http://javaz.cn/site/javaz/site_study/info/2015/22026.html 项目地址:http://www.freeteam.cn/ 因为info. ...

  2. 修改Eclipse/MyEclipse项目的默认编码

    应该是中文操作系统的原因,eclipse默认的新项目的编码是GBK,出于对编码支持的考虑,项目组中最好统一要求是UTF-8编码进行开发. 修改eclipse的配置,可以使得eclipse的新建项目的默 ...

  3. 《Windows核心编程》第1章——错误处理

    GetLastError: GetLastError返回错误编码,即便出错函数后边跟随一个正确执行了的函数,也不会覆盖原先的错误代码: 考虑多线程的情况.子线程中的错误代码不会被主线程捕获: 但是子函 ...

  4. Node.js安装备忘录

    一.准备工作 Node.js下载地址 http://nodejs.org/download/ Current version: v0.10.29 二.平台的选择 2.1 Windows平台 根据自己平 ...

  5. RTC设备驱动

    问题:pcf8563 RTC设备驱动不能被正常的加载!问题分析过程. 问题在下午得到解决,虽然解决的办法比较笨,采用的是不断的使用printk来跟踪rtc-8563驱动的加载的过程,以及iic模块的工 ...

  6. JavaBean示例

    例1.通过非可视化的JavaBean,封装邮箱地址对象,通过JSP页面调用该对象来验证邮箱地址是否合法. (1)创建名称为Email的JavaBean对象,用于封装邮箱地址,关键代码如下: packa ...

  7. HttpMessageConverter

    HttpMessageConverter<T>是Spring3的一个重要接口,它负责将请求信息转换为一个对象(类型为T),将对象(类型为T)输出为响应信息. DispatcherServl ...

  8. WF4.0(2)----设计工作流

    自从做了程序员,发现自己长胖了,而且自己的身体抵抗力也出了问题,最近身体不适,公司工作任务最近也很赶,上次写了WF4.0的简介,这次就工作中工作流设计的几种方式稍微总结一下.设计工作流包括四种方式:流 ...

  9. Dropwizard框架入门

    最近项目用到了Dropwizard框架,个人感觉还不错,那么这里就从他们官网入手,然后加上自己的实现步骤让大家初步了解这个框架. 官网对DW(Dropwizard)的定义是跨越了一个库和框架之间的界限 ...

  10. Caused by: com.alibaba.dubbo.remoting.TimeoutException: Waiting server-side response timeout by scan timer. start time: 2016-07-20 16:27:34.873, end time: 2016-07-20 16:27:39.895, client elapsed: 0 ms

    方案一: 重启dubbo连接 zookeeper 方案二: 经压测,greys跟踪得知,是dubbo的monitor的问题.主要超时的方法是dubbo的getIP方法,monitor每次收集数据的时候 ...