开发者

Python HyBi10 websocket server

I've struggled the past 2 hours with the new Websocket version. I've managed to get the handshake and receiving these new frames, but I'm having problems sending them now.

I'm encoding my text like this:

def encode_hybi(buf, opcode, base64=False):
    """ Encode a HyBi style WebSocket frame.
    Optional opcode:
    0x0 - continuation
    0x1 - text frame (base64 encode buf)
    0x2 - binary frame (use raw buf)
    0x8 - connection close
    0x9 - ping
    0xA - pong
    """
    if base64:
        buf = b64encode(buf)

    b1 = 0x80 | (opcode & 0x0f) # FIN + opcode
    payload_len = len(buf)
    if payload_len <= 125:
        header = struct.pack('>BB', b1, payload_len)
    elif payloa开发者_运维技巧d_len > 125 and payload_len < 65536:
        header = struct.pack('>BBH', b1, 126, payload_len)
    elif payload_len >= 65536:
        header = struct.pack('>BBQ', b1, 127, payload_len)

    #print("Encoded: %s" % repr(header + buf))

    #return header + buf, len(header), 0
    return header+buf

But I don't know in what form I have to pour it to send it over the socket.

By the way: isn't there some easy python websocket module somewhere? My code has now seen 3 websocket versions and it's an utter mess.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜