用python實(shí)現(xiàn)的websocket代碼_第1頁(yè)
用python實(shí)現(xiàn)的websocket代碼_第2頁(yè)
用python實(shí)現(xiàn)的websocket代碼_第3頁(yè)
用python實(shí)現(xiàn)的websocket代碼_第4頁(yè)
用python實(shí)現(xiàn)的websocket代碼_第5頁(yè)
已閱讀5頁(yè),還剩5頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、文檔供參考,可復(fù)制、編制,期待您的好評(píng)與關(guān)注! 用python實(shí)現(xiàn)的websocket代碼ubuntu下python2.76windows Python 2.79, chrome37 firefox35通過(guò)代碼是在別人(cddn有人提問(wèn))基礎(chǔ)上改的, 主要改動(dòng)了parsedata和sendmessage這2個(gè)函數(shù).改代碼參考下面了這段文檔. 主要是第5條, 發(fā)送的數(shù)據(jù)長(zhǎng)度分別是 8bit和 16bit和 64 bit(即 127, 65535,和264-1)三種情況 發(fā)送和收取是一樣的, 例如1.長(zhǎng)度小于125時(shí)(由于使用126, 127用作標(biāo)志位.)2

2、. 數(shù)據(jù)長(zhǎng)度在128-65525之間時(shí), Payload Length位設(shè)為126, 后面額外使用16bit表示長(zhǎng)度(前面的126不再是長(zhǎng)度的一部分)3.數(shù)據(jù)長(zhǎng)度在65526-264-1之間時(shí), Payload Length位設(shè)為127, 后面額外使用64bit表示長(zhǎng)度(前面的127不再是長(zhǎng)度的一部分)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 sing

3、le-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 th

4、e 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 frame2. x1: text frame; this frame (and any following) contains t

5、ext3. x2: binary frame; this frame (and any following) contains binary data4. x3 - x7: non-control reserved frames; these are reserved for possible websocket extensions5. x8: close frame; this frame should end the connection6. x9: ping frame7. xA: pong frame8. xB - xF: control reserved frames4. Mask

6、 (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 len

7、gth 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 multipl

8、e 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.具體代碼:python view pla

9、in copy 1. #coding=utf8  2. #!/usr/bin/python  3.   4.   5. import struct,socket  6. import hashlib  7. import threading,random  8. import time  9. import struct  10. from

10、0;base64 import b64encode, b64decode  11.   12.   13. connectionlist =   14. g_code_length = 0  15. g_header_length = 0  16.   17.   18. def hex2dec(string_num): 

11、60;19.     return str(int(string_num.upper(), 16)  20.   21.   22.   23.   24. def get_datalength(msg):  25.     global g_code_length  26.     global&

12、#160;g_header_length      27.       28.     print (len(msg)  29.     g_code_length = ord(msg1) & 127  30.     received_length 

13、= 0;  31.     if g_code_length = 126:  32.         #g_code_length = msg2:4  33.         #g_code_length = (ord(msg2)<<8) 

14、;+ (ord(msg3)  34.         g_code_length = struct.unpack('>H', str(msg2:4)0  35.         g_header_length = 8  36.     elif g

15、_code_length = 127:  37.         #g_code_length = msg2:10  38.         g_code_length = struct.unpack('>Q', str(msg2:10)0  39.    

16、     g_header_length = 14  40.     else:  41.         g_header_length = 6  42.     g_code_length = int(g_code_length)  43. &#

17、160;   return g_code_length  44.           45. def parse_data(msg):  46.     global g_code_length  47.     g_code_length = ord(msg1) &a

18、mp; 127  48.     received_length = 0;  49.     if g_code_length = 126:  50.         g_code_length = struct.unpack('>H', str(msg2:4)0&#

19、160; 51.         masks = msg4:8  52.         data = msg8:  53.     elif g_code_length = 127:  54.      &#

20、160;  g_code_length = struct.unpack('>Q', str(msg2:10)0  55.         masks = msg10:14  56.         data = msg14:  57.   

21、0; else:  58.         masks = msg2:6  59.         data = msg6:  60.   61.   62.     i = 0  63.  

22、0;  raw_str = ''  64.   65.   66.     for d in data:  67.         raw_str += chr(ord(d)  ord(masksi%4)  68.    &

23、#160;    i += 1  69.   70.   71.     print (u"總長(zhǎng)度是:%d" % int(g_code_length)      72.     return raw_str    73.  

24、60;74.   75. def sendMessage(message):  76.     global connectionlist  77.       78.     message_utf_8 = message.encode('utf-8')  79.     for&

25、#160;connection in connectionlist.values():  80.         back_str =   81.         back_str.append('x81')  82.         data

26、_length = len(message_utf_8)  83.   84.   85.         if data_length <= 125:  86.             back_str.append(chr(data_length) 

27、 87.         elif data_length <= 65535 :  88.             back_str.append(struct.pack('b', 126)  89.       &#

28、160;     back_str.append(struct.pack('>h', data_length)  90.             #back_str.append(chr(data_length >> 8)  91.        

29、0;    #back_str.append(chr(data_length & 0xFF)  92.             #a = struct.pack('>h', data_length)  93.          &

30、#160;  #b = chr(data_length >> 8)  94.             #c = chr(data_length & 0xFF)  95.         elif data_length 

31、<= (264-1):  96.             #back_str.append(chr(127)  97.             back_str.append(struct.pack('b', 127)  98.   &#

32、160;         back_str.append(struct.pack('>q', data_length)  99.             #back_str.append(chr(data_length >> 8)  100.    

33、60;        #back_str.append(chr(data_length & 0xFF)        101.         else :  102.            

34、     print (u'太長(zhǎng)了')          103.         msg = ''  104.         for c in back_str: 

35、 105.             msg += c;  106.         back_str = str(msg)   + message_utf_8#.encode('utf-8')      107.

36、        #connection.send(str.encode(str(u"x00%sxFFnn" % message) #這個(gè)是舊版  108.         #print (u'send message:' +  message)  109.   &

37、#160;     if back_str != None and len(back_str) > 0:  110.             print (back_str)  111.          

38、0;  connection.send(back_str)  112.   113.   114. def deleteconnection(item):  115.     global connectionlist  116.     del connectionlist'connection'+item  117.  

39、; 118.   119. class WebSocket(threading.Thread):#繼承Thread  120.   121.   122.     GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"  123.   124.   125.     def 

40、;_init_(self,conn,index,name,remote, path="/"):  126.         threading.Thread._init_(self)#初始化父類(lèi)Thread  127.         self.conn = conn  128.     

41、    self.index = index  129.          = name  130.         self.remote = remote  131.        

42、60;self.path = path  132.         self.buffer = ""  133.         self.buffer_utf8 = ""  134.         

43、;self.length_buffer = 0  135.     def run(self):#重載Thread的run  136.         print('Socket%s Start!' % self.index)  137.         head

44、ers =   138.         self.handshaken = False  139.   140.   141.         while True:  142.          

45、   if self.handshaken = False:  143.                 print ('Socket%s Start Handshaken with %s!' % (self.index,self.remote)  14

46、4.                 self.buffer += bytes.decode(self.conn.recv(1024)  145.   146.   147.                &#

47、160;if self.buffer.find('rnrn') != -1:  148.                     header, data = self.buffer.split('rnrn', 1)  149.   &#

48、160;                 for line in header.split("rn")1:  150.                     

49、    key, value = line.split(": ", 1)  151.                         headerskey = value  152. 

50、60; 153.   154.                     headers"Location" = ("ws:/%s%s" %(headers"Host", self.path)  155.     

51、;                key = headers'Sec-WebSocket-Key'  156.                     token =

52、0;b64encode(hashlib.sha1(str.encode(str(key + self.GUID).digest()  157.   158.   159.                     handshake="HTTP/1.1 101 Switching 

53、Protocolsrn"  160.                         "Upgrade: websocketrn"  161.            &#

54、160;            "Connection: Upgradern"  162.                         "Sec-WebSocket-Accep

55、t: "+bytes.decode(token)+"rn"  163.                         "WebSocket-Origin: "+str(headers"Origin")+"rn" 

56、60;164.                         "WebSocket-Location: "+str(headers"Location")+"rnrn"  165.   166.   167.  

57、0;                  self.conn.send(str.encode(str(handshake)  168.                     self.handshak

58、en = True    169.                     print ('Socket %s Handshaken with %s success!' %(self.index, self.remote)  

59、;  170.                     sendMessage(u'Welcome, ' +  + ' !')    171.        &

60、#160;            self.buffer_utf8 = ""  172.                     g_code_length = 0   

61、                   173.   174.   175.             else:  176.          

62、;       global g_code_length  177.                 global g_header_length  178.             

63、60;   mm=self.conn.recv(128)  179.                 if len(mm) <= 0:  180.                

64、60;    continue  181.                 if g_code_length = 0:  182.                 

65、60;   get_datalength(mm)  183.                 #接受的長(zhǎng)度  184.                 self.length_buffer =

66、0;self.length_buffer + len(mm)  185.                 self.buffer = self.buffer + mm  186.              &#

67、160;  if self.length_buffer - g_header_length < g_code_length :  187.                     continue  188.      &#

68、160;          else :  189.                     self.buffer_utf8 = parse_data(self.buffer) #utf8    &

69、#160;             190.                     msg_unicode = str(self.buffer_utf8).decode('utf-8', 'ignore')

70、 #unicode  191.                     if msg_unicode='quit':  192.                 &

71、#160;       print (u'Socket%s Logout!' % (self.index)  193.                         nowTime = tim

72、e.strftime('%H:%M:%S',time.localtime(time.time()  194.                         sendMessage(u'%s %s say: %s' % (nowTime, self

73、.remote, +' Logout')                        195.                  &#

74、160;      deleteconnection(str(self.index)  196.                         self.conn.close()  197.      

75、60;                  break #退出線程  198.                     else:  199.   &#

76、160;                     #print (u'Socket%s Got msg:%s from %s!' % (self.index, msg_unicode, self.remote)  200.    &#

77、160;                    nowTime = time.strftime(u'%H:%M:%S',time.localtime(time.time()  201.              &

78、#160;          sendMessage(u'%s %s say: %s' % (nowTime, self.remote, msg_unicode)    202.                 &

79、#160;   #重置buffer和bufferlength  203.                     self.buffer_utf8 = ""  204.           

80、          self.buffer = ""  205.                     g_code_length = 0  206.     

81、;                self.length_buffer = 0  207.             self.buffer = ""  208.   209.   

82、210. class WebSocketServer(object):  211.     def _init_(self):  212.         self.socket = None  213.     def begin(self):  214.     &

83、#160;   print( 'WebSocketServer Start!')  215.         self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  216.         self.socket.setsock

84、opt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)  217.         self.socket.bind("",12345)  218.         self.socket.listen(50)  219.   220.   221.  &#

85、160;      global connectionlist  222.   223.   224.         i=0  225.         while True:  226.      

86、0;      connection, address = self.socket.accept()  227.   228.   229.             username=address0       230.    

87、         newSocket = WebSocket(connection,i,username,address)  231.             newSocket.start() #開(kāi)始線程,執(zhí)行run函數(shù)  232.        

88、     connectionlist'connection'+str(i)=connection  233.             i = i + 1  234.   235.   236. if _name_ = "_main_":

89、60; 237.     server = WebSocketServer()  238.     server.begin()  客戶(hù)端 測(cè)試了chrome37, firefox35html view plain copy 1. <!DOCTYPE html>  2. <html>  3. <head> 

90、60;4.     <title>WebSocket</title>  5.   6.     <style>  7.         html, body   8.            &#

91、160;font: normal 0.9em arial, helvetica;  9.           10.   11.         #log   12.             

92、;width: 440px;  13.             height: 200px;  14.             border: 1px solid #7F9DB9;  15.      

93、;       overflow: auto;  16.           17.   18.         #msg   19.            &

94、#160;width: 330px;  20.           21.     </style>  22.   23.     <script>  24.         var socket; 

95、60;25.   26.         function init()   27.             var host = "ws:/:12345/"  28.        

96、;     try   29.                 socket = new WebSocket(host);  30.               

97、0; socket.onopen = function (msg)   31.                     log('Connected');  32.           

98、0;       33.                 socket.onmessage = function (msg)   34.               &#

99、160;     log(msg.data);  35.                   36.                 socket.onclose = f

100、unction (msg)   37.                     log("Lose Connection!");  38.               &

101、#160;   39.               40.             catch (ex)   41.             

102、0;   log(ex);  42.               43.             $("msg").focus();  44.          

103、; 45.   46.         function send()   47.             var txt, msg;  48.             

104、txt = $("msg");  49.             msg = txt.value;  50.             if (!msg)   51.     

105、60;           alert("Message can not be empty");  52.                 return;  53.               54.             txt.value = ""  55.           

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論