用Python实现一个简单的WebSocket服务器
windows Python , chrome37 firefox35通过
代码是在别人(cddn有人提问)基础上改的, 主要改动了parsedata和sendmessage这2个函数.
改代码参考下面了这段文档. 主要是第5条, 发送的数据长度分别是 8bit和 16bit和 64 bit(即 127, 65535,和2^64-1)三种情况
发送和收取是一样的, 例如
(由于使用126, 127用作标志位.)
2. 数据长度在128-65525之间时, Payload Length位设为126, 后面额外使用16bit表示长度(前面的126不再是长度的一部分)
-2^64-1之间时, Payload Length位设为127, 后面额外使用64bit表示长度(前面的127不再是长度的一部分)
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.
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.
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:
x0: continuation frame; this frame contains data that should be appended to the previous frame
x1: text frame; this frame (and any following) contains text
x2: binary frame; this frame (and any following) contains binary data
x3 - x7: non-control reserved frames; these are reserved for possible websocket extensions
x8: close frame; this frame should end the connection
x9: ping fram
websocket的python代码 来自淘豆网m.daumloan.com转载请标明出处.