资源描述:
《python实现的websocket代码_计算机软件及应用_it计算机_专业资料》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、ubuntu下python2.76windowsPython2.79,chrome37firefox35通过代码是在别人(cddn冇人提问)基础上改的,主要改动了parsedata和sendmessage这2个函数.改代码参考卜•面了这段文档.主要是笫5条,发送的数据长度分别是8bit和16bit和64bit(即127,65535,和2A64-1)三种情况发送和收取是一样的,例如1•长度小于125时(由于使用126,127用作标志位.)2.数据长度在128-65525Z间时,PayloadLength位设为126,后面额外使用16bit表示长度(前
2、而的126不再是长度的一部分)3.数据长度在65526-2A64-1Z间时,PayloadLength位设为127,后面额外使用64bit表示长度(前面的127不再是长度的一部分)LFin(bit0):determinesifthisisthelastframeinthemessage.Thiswouldbesetto1ontheendofaseriesofframes,orinasingle-framemessage,itwouldbesetto1asitisboththefirstandlastframe.2.RSV1,RSV2,RSV3(bi
3、ts1-3):thesethreebitsarereservedforwebsocketextensions,andshouldbe0unlessaspecificextensionrequirestheuseofanyofthesebytes.3.Opcode(bits4-7):thesefourbitsdeteriminethetypeoftheframe.ControlframescommunicateWebSocketstate,whilenon-controlframescommunicatedata.Thevarioustypesofc
4、odesinclude:1.xO:continuationframe;thisframecontainsdatathatshouldbeappendedtothepreviousframe2.xl:textframe;thisframe(andanyfollowing)containstext3.x2:binaryframe;thisframe(andanyfollowing)containsbinarydata4.x3・x7:non-controlreservedframes;thesearereservedforpossiblewebsocke
5、textensions5.x8:closeframe;thisframeshouldendtheconnection6.x9:pingframe7.xA:pongframe8.xB-xF:controlreservedframes2.Mask(bit8):thisbitdetermineswhetherthisspecificframeusesamaskornot:.3.PayloadLength(bits9-15,or16-31,or16-79):thesesevenbytesdeterminethepayloadlength.Iftheleng
6、this126,thelengthisactuallydeterminedbybits16through31(thatis,thefollowingtwobytes).Ifthelengthis127,thelengthisactuallydeterminedbybits16through79(thatis,thefollowingeightbytes).4.MaskingKey(thefollowingfourbytes):thisrepresentsthemask,iftheMaskbitissetto1.5.PayloadData(thefo
7、llowingdata):finally,thedata.Thepayloaddatamaybesentovermultipleframes;weknowthesizeoftheentiremessagebythepayloadlengththatwassent,andcanappenddatatogethertoformasinglemessageuntilwereceivethemessagewiththeFinflag.Eachconsecutivepayload,ifitexists,willcontainthe0"continuation
8、frame"opcode.服务器[python]viewplaincopy1.#coding=utf82.#!/usr/b