Python3 websocket通訊

Mr_F發表於2017-08-01

一. 客戶端服務端進行握手

1.客戶端向服務端傳送握手包

GET ws://localhost:8000/ HTTP/1.1
Host: localhost:8000
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Origin: http://localhost:63342
Sec-WebSocket-Version: 13
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: zh-CN,zh;q=0.8
Sec-WebSocket-Key: 60jj9YDlrvPB+DzLmVJzcg==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits複製程式碼

2.服務端進行迴應,返回給客戶端

HTTP/1.1 101 Switching Protocols
Upgrade: WebSocket
Connection: Upgrade
Sec-WebSocket-Accept: Cx7gYudkmGmF8rMFP1W1HbLkWRA=複製程式碼

客戶端傳送握手請求,然後服務端返回正確的資料,經過客戶端的確認完成握手,然後就可以進行通訊了;
這裡客戶端傳送的Sec-WebSocket-Key是一個Base64 encode的值,它是瀏覽器隨機生成的,然後服務端通過這個值生成一個Sec-WebSocket-Accept;
方法: key+'258EAFA5-E914-47DA-95CA-C5AB0DC85B11',然後SHA-1加密,再進行base-64加密;
下面是握手程式碼:

HANDSHAKE_STR = (
   "HTTP/1.1 101 Switching Protocols\r\n"
   "Upgrade: WebSocket\r\n"
   "Connection: Upgrade\r\n"
   "Sec-WebSocket-Accept: %(token)s\r\n\r\n"
)
GUID_STR = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
def parseHeaders(self, msg):
    # print(msg)
    headers = {}
    msg = str(msg, encoding="utf-8")
    header, data = msg.split('\r\n\r\n', 1)
    for x in header.split("\r\n")[1:]:
        # 這裡是 “:+space”
        key, value = x.split(": ", 1)
        headers[key] = value
    return headers
# 握手資料
def handShakeData(self,c_req):
    sec_key = self.parseHeaders(c_req)['Sec-WebSocket-Key']
    key = sec_key.encode("ascii") + GUID_STR.encode("ascii")
    accept_token = base64.b64encode(hashlib.sha1(key).digest()).decode("ascii")
    s_res = HANDSHAKE_STR % {'token': accept_token}
    return s_res複製程式碼

二. 進行通訊

大概就是握手完成後,客戶端傳送訊息到服務端,然後服務端將訊息傳送到每個連線上的客戶端;

資料互動協議

  0                   1                   2                   3
  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 +-+-+-+-+-------+-+-------------+-------------------------------+
 |F|R|R|R| opcode|M| Payload len |    Extended payload length    |
 |I|S|S|S|  (4)  |A|     (7)     |             (16/64)           |
 |N|V|V|V|       |S|             |   (if payload len==126/127)   |
 | |1|2|3|       |K|             |                               |
 +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
 |     Extended payload length continued, if payload len == 127  |
 + - - - - - - - - - - - - - - - +-------------------------------+
 |                               |Masking-key, if MASK set to 1  |
 +-------------------------------+-------------------------------+
 | Masking-key (continued)       |          Payload Data         |
 +-------------------------------- - - - - - - - - - - - - - - - +
 :                     Payload Data continued ...                :
 + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
 |                     Payload Data continued ...                |
 +---------------------------------------------------------------+複製程式碼
第一個位元組:
  • FIN:描述訊息是否結束;佔1bit;如果是1表示這是資料包的最後一幀,0則表示後續還有資料;
  • RSV1, RSV2, RSV3:擴充套件定義,預設0,各佔1bit;
  • opcode:表示傳輸的資料包型別,佔4bit,(15種情況)
    • 0x0 表示為附加資料
    • 0x1 表示為text型別資料
    • 0x2 表示binary型別資料
    • 0x3~7 暫未定義
    • 0x8 表示連線關閉
    • 0x9 表示ping型別資料
    • 0xA 表示pong型別資料
    • 0xB~F 暫未定義
第二個位元組:
  • MASK:表示是否經過掩碼處理;如果是1,表示經過掩碼處理,Masking-key的資料為掩碼密碼
  • Payload length:
    • 如果值為0~125,則就是payload的真實長度;(若MASK=1,則Masking-key為第3~6個位元組)
    • 如果值為126,則後面的2個位元組(16bit)為payload的真實長度;(若MASK=1,則Masking-key為第5~8個位元組)
    • 如果值為127,則後面的8個位元組(64bit)為payload的真實長度;(若MASK=1,則Masking-key為第11~14個位元組)

1. 將客戶端傳送的資料解析出真實的資料

方法:將Payload Data資料的每一位x,與Masking-key的第i%4位進行異或運算,i是x在Payload Data中的索引;
下面是解析的程式碼:

def parseData(self,msg):
    g_code_length = msg[1] & 127
    if g_code_length == 126:
        g_code_length = struct.unpack('!H', msg[2:4])[0]
        masks = msg[4:8]
        data = msg[8:]
    elif g_code_length == 127:
        g_code_length = struct.unpack('!Q', msg[2:10])[0]
        masks = msg[10:14]
        data = msg[14:]
    else:
        masks = msg[2:6]
        data = msg[6:]

    i = 0
    raw_by = bytearray()
    for d in data:
        raw_by.append( int(d) ^ int(masks[i % 4]) )
        i += 1
    print(u"總長度是:%d" % int(g_code_length))
    raw_str = raw_by.decode()
    return raw_str複製程式碼

2. 將資料傳送到客戶端

傳送的資料格式:固定位元組+包長度位元組+原始資料
例:\x81\x0cHello World!

三. 完整的程式碼
服務端:


import socket
import base64,hashlib
import threading
import struct
from collections import deque

HANDSHAKE_STR = (
   "HTTP/1.1 101 Switching Protocols\r\n"
   "Upgrade: WebSocket\r\n"
   "Connection: Upgrade\r\n"
   "Sec-WebSocket-Accept: %(token)s\r\n\r\n"
)
GUID_STR = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
STREAM = 0x0
TEXT = 0x1
BINARY = 0x2
CLOSE = 0x8
PING = 0x9
PONG = 0xA


class WebSocket(threading.Thread):
    def __init__(self,conn,addr,index):
        threading.Thread.__init__(self)
        self.index = index
        self.conn = conn
        self.addr = addr
        #傳送的資料快取
        self.buffer = bytearray()
        self.sendToClientData = deque()

    # 組裝header 獲取‘Sec-WebSocket-Key’
    def parseHeaders(self, msg):
        # print(msg)
        headers = {}
        msg = str(msg, encoding="utf-8")
        header, data = msg.split('\r\n\r\n', 1)
        for x in header.split("\r\n")[1:]:
            # 這裡是 “:+space”
            key, value = x.split(": ", 1)
            headers[key] = value
        return headers
    # 握手資料
    def handShakeData(self,c_req):
        sec_key = self.parseHeaders(c_req)['Sec-WebSocket-Key']
        key = sec_key.encode("ascii") + GUID_STR.encode("ascii")
        accept_token = base64.b64encode(hashlib.sha1(key).digest()).decode("ascii")
        s_res = HANDSHAKE_STR % {'token': accept_token}
        return s_res
    # 得到資料長度 (包含描述位元組)
    def getMsglen(self,msg):
        g_code_length = msg[1] & 127
        if g_code_length == 126:
            g_code_length = struct.unpack('!H', msg[2:4])[0]
            g_code_length += 8
        elif g_code_length == 127:
            g_code_length = struct.unpack('!Q', msg[2:10])[0]
            g_code_length += 14
        else:
            g_code_length += 6
        g_code_length = int(g_code_length)
        print(g_code_length)
        return g_code_length
    # 解析資料
    def parseData(self,msg):
        g_code_length = msg[1] & 127
        if g_code_length == 126:
            g_code_length = struct.unpack('!H', msg[2:4])[0]
            masks = msg[4:8]
            data = msg[8:]
        elif g_code_length == 127:
            g_code_length = struct.unpack('!Q', msg[2:10])[0]
            masks = msg[10:14]
            data = msg[14:]
        else:
            masks = msg[2:6]
            data = msg[6:]

        i = 0
        raw_by = bytearray()
        for d in data:
            raw_by.append( int(d) ^ int(masks[i % 4]) )
            i += 1
        print(raw_by)
        print(u"總長度是:%d" % int(g_code_length))
        raw_str = raw_by.decode()
        # raw_str = str(raw_by)
        return raw_str
    # 傳送訊息
    def sendMessage(self,message):
        # 遍歷連線上的列表
        for conn in connections.values():
            # 傳送訊息到client (除了自己)
            if conn != self.conn:
                self._sendMessage(False,TEXT,message)
                while self.sendToClientData:
                    data = self.sendToClientData.popleft()
                    conn.send(data[1])

    # FIN 後面是否還有資料;opcode 傳輸的資料包型別;message 傳輸的資料(str)
    def _sendMessage(self,FIN,opcode,message):
        payload = bytearray()
        b1 = 0
        b2 = 0
        if FIN is False:
            b1 |= 0x80
        b1 |= opcode        # 若opcode=TEXT    b'0x81'

        payload.append(b1)      #
        msg_utf = message.encode('utf-8')
        msg_len = len(msg_utf)

        if msg_len <= 125:
            b2 |= msg_len

            payload.append(b2)
        elif msg_len >= 126 and msg_len <= 65535:
            b2 |= 126
            payload.append(b2)
            payload.extend(struct.pack("!H", msg_len))
        elif msg_len <= (2 ^ 64 - 1):
            b2 |= 127
            payload.append(b2)
            payload.extend(struct.pack("!Q", msg_len))
        else:
            print("傳輸的資料太長了! ——(_sendMessage)")
        # 拼接上需要傳送的資料
        # 格式大概這樣:bytearray(b'\x81\x0cHello World!')   '\x0c'是傳送的資料長度
        if msg_len > 0:
            payload.extend(msg_utf)

        self.sendToClientData.append((opcode,payload))

    # 端開連線
    def _disConnected(self):
        self.conn.close()
        print("斷開的連線conn%s" % (self.index))
        print(connections)
        del connections['conn%s' % (self.index)]
        print(connections)
    # 
    def run(self):
        # print(self.listeners)
        self.isHandShake = False
        while True:
            if self.isHandShake == False:
                print("start handshake %s" % (self.addr[0]))
                # 接收客戶端內容
                c_req = self.conn.recv(1024)
                # print(c_req)
                handData = self.handShakeData(c_req)
                self.conn.sendall(str.encode(handData))
                self.isHandShake = True
            else:
                message = self.conn.recv(16384)    # 16 * 1024   16384   # bytes
                print(message)
                # 斷開連線
                if message[0]&127 == CLOSE:    # 0x88   136
                    self._disConnected()
                    return
                self.buffer.extend(message)  # bytes
                code_length = self.getMsglen(self.buffer)  # 資料中帶的 資料長度(包含描述位元組)
                # 資料完整就傳送
                if code_length == len(self.buffer):
                    buffer_utf = self.parseData(self.buffer)      # str
                    self.sendMessage(buffer_utf)
                    print(message)
                    self.buffer = bytearray()

connections = {}
class websocketServer(object):
    def __init__(self,host, port):
        self.serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.serversocket.bind((host, port))
        self.serversocket.listen(5)

    def server(self):
        print(self.serversocket)
        index =0
        while True:
            conn,addr = self.serversocket.accept()
            print(conn)
            print(addr)
            # 新建執行緒
            newSocket = WebSocket(conn,addr,index)
            # 開始執行緒,執行run函式
            newSocket.start()
            connections['conn%s'% (index)] = conn
            index += 1
            print(connections)


websocketServer = websocketServer("127.0.0.1",8000)
websocketServer.server()複製程式碼

感謝您的閱讀;

相關文章