资源描述:
《用Python实现一个简单的WebSocket服务器》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、ubuntu下python2.76windows Python 2.79,chrome37firefox35通过代码是在别人(cddn有人提问)基础上改的,主要改动了parsedata和sendmessage这2个函数.改代码参考下面了这段文档.主要是第5条,发送的数据长度分别是8bit和16bit和64bit(即 127,65535,和2^64-1)三种情况 发送和收取是一样的,例如1.长度小于125时(由于使用126,127用作标志位.)2.数据长度在128-65525之间时, PayloadLength位设为1
2、26,后面额外使用16bit表示长度(前面的126不再是长度的一部分)3.数据长度在65526-2^64-1之间时, PayloadLength位设为127,后面额外使用64bit表示长度(前面的127不再是长度的一部分)1.Fin(bit0):determinesifthisisthelastframeinthemessage.Thiswouldbesetto1ontheendofaseriesofframes,orinasingle-framemessage,itwouldbesetto1asitisbothth
3、efirstandlastframe.2.RSV1,RSV2,RSV3(bits1-3):thesethreebitsarereservedforwebsocketextensions,andshouldbe0unlessaspecificextensionrequirestheuseofanyofthesebytes.3.Opcode(bits4-7):thesefourbitsdeteriminethetypeoftheframe.ControlframescommunicateWebSocketstate,wh
4、ilenon-controlframescommunicatedata.Thevarioustypesofcodesinclude:4.1.x0:continuationframe;thisframecontainsdatathatshouldbeappendedtothepreviousframe2.x1:textframe;thisframe(andanyfollowing)containstext3.x2:binaryframe;thisframe(andanyfollowing)containsbinaryd
5、ata1.x3-x7:non-controlreservedframes;thesearereservedforpossiblewebsocketextensions2.x8:closeframe;thisframeshouldendtheconnection3.x9:pingframe4.xA:pongframe5.xB-xF:controlreservedframes2.Mask(bit8):thisbitdetermineswhetherthisspecificframeusesamaskornot.3.Pay
6、loadLength(bits9-15,or16-31,or16-79):thesesevenbytesdeterminethepayloadlength.Ifthelengthis126,thelengthisactuallydeterminedbybits16through31(thatis,thefollowingtwobytes).Ifthelengthis127,thelengthisactuallydeterminedbybits16through79(thatis,thefollowingeightby
7、tes).4.MaskingKey(thefollowingfourbytes):thisrepresentsthemask,iftheMaskbitissetto1.5.PayloadData(thefollowingdata):finally,thedata.Thepayloaddatamaybesentovermultipleframes;weknowthesizeoftheentiremessagebythepayloadlengththatwassent,andcanappenddatatogetherto
8、formasinglemessageuntilwereceivethemessagewiththeFinflag.Eachconsecutivepayload,ifitexists,willcontainthe0“continuationframe”opcode.服务器[python] viewplain copy1.#coding=utf8