空间收藏于 2014-06-26
00:24 阅读(0)
如果想用2进制类型数据结构需要在python里需要用到struct和ctype模块, 另外python3的文档有点少,找了半天发现网上都是一样的你转我转你的, 没办法,只能看源代码了。 学习内容 : 1 是否可以传输2进制内容? 2 2机制内容是否可以用压缩算法处理?【另开日志】 3 如何防御xsrf模拟请求攻击?【另开日志】 达成目标: 1首先是正常的http请求 通过 2然后首发二进制请求 通过 3nginx代理配置 通过 4启动脚本代码 通过 启动脚本代码 :#!/bin/bash export PYTHONPATH=/www/app/webservice/libs PID=` ps -ef | grep -v grep | grep python34 | awk '{print$2}'` if [ "$PID" != "" ] ; then kill -15 $PID fi nohup /opt/python34/bin/python3.4 test.py > log/console.log & 服务端代码: import tornado.ioloop import tornado.web import mybytes class MainHandler (tornado.web.RequestHandler): “”“Normal”“”def get ( self ) : import json nums=[1,2,3,4,5,6]; result={'data':nums}; self.write(json.dumps(result)); def post( self ) : a=self.get_argument("f"); b=self.get_argument("i"); self.write('you wirte %s %s' % (a , b));
class BytesHandler (tornado.web.RequestHandler): def get ( self ): """ get bytes response """ bytes=mybytes.ByteArray(); bytes.writeInt(1); bytes.writeUTF("0123456789"); bytes.writeFloat(3.14); a = bytes.toString (); self.write( a ); def post (self): """ client send binary to this.and we can unpack it ? vo= ID int[1] PI float NAME char[10] scans=tornado.web.stream_request_body(BytesHandler); structc= mybytes.ByteStrcut(vo); bytes = mybytes.ByteArray(structc); a=bytes.decode(bytes); """ tornado.web.stream_request_body(BytesHandler); pass; def data_received(self, data): self.write(data); application= tornado.web.Application([ (r"/rest/2014/main",MainHandler), (r"/rest/2014/bytes",BytesHandler), ]) if __name__ == '__main__': application.listen(8090); tornado.ioloop.IOLoop.instance().start(); 测试的客户端代码: package{ import flash.display.Sprite; import flash.events.Event; import flash.net.URLLoader; import flash.net.URLRequest; import flash.net.URLVariables; import flash.utils.ByteArray; import flash.utils.Endian; public class mywebappDev extends Sprite { public function mywebappDev() { PostEntity(); GetbinaryEntity(); PostbinaryEntity(); } //发送 binary 数据 protected function PostbinaryEntity():void { var loader:URLLoader = new URLLoader; loader.addEventListener(Event.COMPLETE , onLoadedBinary_POST); var request:URLRequest = new URLRequest; request.url = "?"+Math.random(); request.method = "POST"; request.contentType ="application/octet-stream"; var bytes:ByteArray = new ByteArray(); bytes.endian = Endian.LITTLE_ENDIAN; bytes.writeInt(10); bytes.writeFloat(3.14); bytes.writeUTF("1234567890"); loader.dataFormat = "binary"; request.data = bytes; loader.load( request); } protected function onLoadedBinary_POST(e:Event):void { var bytes:ByteArray = e.target.data as ByteArray; bytes.endian = Endian.LITTLE_ENDIAN; trace("发送之后返回的 binary 数据", bytes.readInt(), bytes.readFloat() ,bytes.readUTF()); } //获取 binary 数据 protected function GetbinaryEntity():void { var loader:URLLoader = new URLLoader; loader.addEventListener(Event.COMPLETE , onLoadedBinary_GET); var request:URLRequest = new URLRequest; request.url = "?"+Math.random(); request.method = "GET"; var data:URLVariables = new URLVariables(); loader.dataFormat = "binary"; loader.load( request); } protected function onLoadedBinary_GET(e:Event):void { var bytes:ByteArray =e.target.data as ByteArray ; bytes.endian = Endian.LITTLE_ENDIAN; trace("Get发送之后返回的 binary 数据",bytes.bytesAvailable); trace(bytes.readInt(),bytes.readUTFBytes(10),bytes.readFloat().toFixed(2)); } //正常POST提交数据 protected function PostEntity():void { var loader:URLLoader = new URLLoader; loader.addEventListener(Event.COMPLETE , onLoaded); var request:URLRequest = new URLRequest; request.url = "?"+Math.random(); request.method = "POST"; var data:URLVariables = new URLVariables(); data["f"] = 3.14 data["i"] = int(2); request.data = data; loader.dataFormat = "binary"; loader.load( request); } protected function onLoaded(e:Event):void { trace("正常POST提交返回的数据",e.target.data); } } } 返回的结果控制台输出[SWF] mywebappDev.swf - 解压缩后为 2,927 个字节 Get发送之后返回的 binary 数据 18 1 0123456789 3.14 正常POST提交返回的数据 you wirte 3.14 2 发送之后返回的 binary 数据 10 3.140000104904175 1234567890 [卸装 SWF] mywebappDev.swf 之后是我封装的一个读写2进制数据的模块 import structimport ctypes import re __all__ = [ "ByteArray" , "ByteStruct", "test" ]; class ByteArray : """ ByteArray is package for Bytes Create by wangwb 2015/06/26 """ def __init__ ( self, model = None, endian = "little" ) : self.endian = endian; self.itor = []; self.position = 0; self.rawData = []; if model == None : self.format = []; self.needFmt= True; self.model = None; else : self.format = model.getFormat(endian); self.needFmt= False; self.model = model ; def writeInt ( self, i ) : if self.needFmt : self.itor.append('i'); self.rawData.append(i);
def writeFloat( self , i ) : if self.needFmt : self.itor.append('f'); self.rawData.append(i);
def writeUTF ( self , string ): string = string.encode("utf-8"); strlen = len(string); if self.needFmt : self.itor.append("%ds" % strlen); self.rawData.append(string);
def readUTF ( self , rawData ): return rawData.decode();
def writeDouble( self , i ): if self.needFmt : self.itor.append('d'); self.rawData.append(i);
def writeBoolean(self , i ) : if self.needFmt : self.itor.append('?'); self.rawData.append(i);
def writeShort ( self , i ): if self.needFmt : self.itor.append('h'); self.rawData.append(i);
def writeUShort ( self , i ): if self.needFmt : self.itor.append('H'); self.rawData.append(i);
def writeLong ( self , i ): if self.needFmt : self.itor.append('l'); self.rawData.append(i);
def writeULong( self , i ): if self.needFmt : self.itor.append('L'); self.rawData.append(i);
def writeUint( slef , i ): if self.needFmt : self.itor.append('I'); self.rawData.append(i);
def writeInt64 (self , i ): if self.needFmt : self.itor.append('q'); self.rawData.append(i);
def writeInt64 ( self , i ): if self.needFmt : self.itor.append('Q'); self.rawData.append(i);
def toString (self) : if self.needFmt : if self.endian == "little" : self.format = "<" + "".join(self.itor); else : self.format = ">" + "".join(self.itor); self.needFmt= False; return struct.pack( self.format , *(self.rawData));
def encode (self) : if self.needFmt : if self.endian == "little" : self.format = "<" + "".join(self.itor); else : self.format = ">" + "".join(self.itor); self.needFmt= False; self.struct = struct.Struct(self.format); self.length = self.struct.size; self.buffs = ctypes.create_string_buffer(self.struct.size); self.struct.pack_into(self.buffs,0,*(self.rawData)); return self.buffs;
def decode (self , bytes): if self.needFmt : return None; self.struct = struct.Struct(self.format); self.length = self.struct.size; return self.struct.unpack_from(bytes,0);
pattern_trim = re.compile(r'\s'); pattern_repl = re.compile(r'(?P<name>[a-z0-9A-Z]+)\:(?P<type>(int|float|char|double))(\[(?P<repeat>([0-9]+?))\])?'); keywords= { 'int':'i', 'float':'f', 'double':'d', 'char':'s', 'uint':'I' }; class ByteStruct : def __parse__ ( self, cstr ): tmp = []; cstr,leng = re.subn( pattern_trim , "" , cstr); codes = cstr.split(","); lines = len ( codes ); for i in range (0,lines ,1): result = re.match(pattern_repl, codes[i]); ctype = keywords[result.group("type")]; repeat = result.group("repeat"); if repeat : ctype = repeat + ctype; tmp.append(ctype) ; return ("".join(tmp));
def __init__ ( self, word ) : self.rdata = {}; self.format = self.__parse__(word);
def getFormat ( self , endian ) : if endian == "little" : self.format = "<" + self.format; else : self.format = ">" + self.format; return self.format; pass;
def test () : """ simple to used BytesArray. """ c=""" arr:int[1], PI:float, word:char[10], f:double """ #encode
bytes = ByteArray(); bytes.writeInt(1); bytes.writeFloat(3.14); bytes.writeUTF("0123456789"); bytes.writeDouble(3.12); a = bytes.encode(); print(type(a)); print( type(bytes.toString()) ); #print(bytes.decode(a)[14].decode()); print(bytes.decode(a)); #print(bytes.toString()); #decode structc = ByteStruct (c); bytes_d = ByteArray(structc); d = bytes_d.decode(a) ; print(type(d));
if __name__ == '__main__' : test(); 还不够熟练。看来还是需要多写啊。 |