Websocket协议 - 从服务器发送数据时出错 - Python 3

时间:2017-10-06 15:01:32

标签: python-3.x websocket

我正在尝试将数据从服务器发送到客户端,但我仍然收到此错误:
A server must not mask any frames that it sends to the client.

我的代码是:

def encode(data): # data is "hello" for example.
    data_length = len(data) # 5

    FIN     = "1"
    RSV1    = "0"
    RSV2    = "0"
    RSV3    = "0"
    OPCODE  = "0001"
    MASK    = "0"
    LEN     = "0"
    EXT_LEN = "" # extra len
    LOOP    = 2

    # Set Length for framing
    if data_length <= 125:
        LEN  = bin(len(data))[2:].rjust(7,"0")
    elif data_length <= 65536:
        LEN  = bin(126)[2:].rjust(7,"0")
        LOOP = 4
        EXT_LEN = bin(data_length)[2:].rjust(7+16,"0")
    else:
        LEN  = bin(127)[2:].rjust(7,"0")
        LOOP = 10
        EXT_LEN = bin(data_length)[2:].rjust(7+64,"0")

    ALL     = FIN+RSV1+RSV2+RSV3+OPCODE+MASK+LEN+EXT_LEN
    ENCODED = b"".join([chr(int(ALL[i*8:i*8+8],2)).encode() for i in range(LOOP)]) # "\xc2\x81\x05"
    DATA    = data.encode() # b"hello"

    return ENCODED+DATA # b'\xc2\x81\x05hello'

正如你所看到的,我并没有掩饰任何东西,但它仍然坚持说我不能掩饰。我不明白我在哪里掩饰?
谢谢大家..

0 个答案:

没有答案