资源描述:
《websocket的python代码》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、用Python实现一个简单的WebSocket服务器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.数据长度
2、在128-65525之间时, PayloadLength位设为126,后面额外使用16bit表示长度(前面的126不再是长度的一部分)3.数据长度在65526-2^64-1之间时, PayloadLength位设为127,后面额外使用64bit表示长度(前面的127不再是长度的一部分)1.Fin(bit0):determinesifthisisthelastframeinthemessage.Thiswouldbesetto1ontheendofaseriesofframes,orinasingle-fr
3、amemessage,itwouldbesetto1asitisboththefirstandlastframe.2.RSV1,RSV2,RSV3(bits1-3):thesethreebitsarereservedforwebsocketextensions,andshouldbe0unlessaspecificextensionrequirestheuseofanyofthesebytes.3.Opcode(bits4-7):thesefourbitsdeteriminethetypeofthefra
4、me.ControlframescommunicateWebSocketstate,whilenon-controlframescommunicatedata.Thevarioustypesofcodesinclude:1.x0:continuationframe;thisframecontainsdatathatshouldbeappendedtothepreviousframe2.x1:textframe;thisframe(andanyfollowing)containstext3.x2:binar
5、yframe;thisframe(andanyfollowing)containsbinarydata4.x3-x7:non-controlreservedframes;thesearereservedforpossiblewebsocketextensions5.x8:closeframe;thisframeshouldendtheconnection6.x9:pingframe7.xA:pongframe1.xB-xF:controlreservedframes2.Mask(bit8):thisbit
6、determineswhetherthisspecificframeusesamaskornot.3.PayloadLength(bits9-15,or16-31,or16-79):thesesevenbytesdeterminethepayloadlength.Ifthelengthis126,thelengthisactuallydeterminedbybits16through31(thatis,thefollowingtwobytes).Ifthelengthis127,thelengthisac
7、tuallydeterminedbybits16through79(thatis,thefollowingeightbytes).4.MaskingKey(thefollowingfourbytes):thisrepresentsthemask,iftheMaskbitissetto1.5.PayloadData(thefollowingdata):finally,thedata.Thepayloaddatamaybesentovermultipleframes;weknowthesizeoftheent
8、iremessagebythepayloadlengththatwassent,andcanappenddatatogethertoformasinglemessageuntilwereceivethemessagewiththeFinflag.Eachconsecutivepayload,ifitexists,willcontainthe0“continuationframe”opcode.服务器端代码(为方便阅读,请在下面