您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > ROS API接口官方教程支持v4
ROSAPI接口官方教程支持v4.x以上版本ExampleclientthisissimpleAPIclientinPython2exampleforPython3usage:api.pyip-addressusernamepasswordafterthattypewordsfromkeyboard,terminatingthemwithnewlineSinceemptywordterminatessentence,youshouldpressentertwiceafterlastwordbeforesentencewillbesenttorouter.#!/usr/bin/pythonimportsys,posix,time,md5,binascii,socket,selectclassApiRos:Routerosapidef__init__(self,sk):self.sk=skself.currenttag=0deflogin(self,username,pwd):forrepl,attrsinself.talk([/login]):chal=binascii.unhexlify(attrs['=ret'])md=md5.new()md.update('x00')md.update(pwd)md.update(chal)self.talk([/login,=name=+username,=response=00+binascii.hexlify(md.digest())])deftalk(self,words):ifself.writeSentence(words)==0:returnr=[]while1:i=self.readSentence();iflen(i)==0:continuereply=i[0]attrs={}forwini[1:]:j=w.find('=',1)if(j==-1):attrs[w]=''else:attrs[w[:j]]=w[j+1:]r.append((reply,attrs))ifreply=='!done':returnrdefwriteSentence(self,words):ret=0forwinwords:self.writeWord(w)ret+=1self.writeWord('')returnretdefreadSentence(self):r=[]while1:w=self.readWord()ifw=='':returnrr.append(w)defwriteWord(self,w):print+wself.writeLen(len(w))self.writeStr(w)defreadWord(self):ret=self.readStr(self.readLen())print+retreturnretdefwriteLen(self,l):ifl0x80:self.writeStr(chr(l))elifl0x4000:l|=0x8000self.writeStr(chr((l&0xFF))self.writeStr(chr(l&0xFF))elifl0x200000:l|=0xC00000self.writeStr(chr((l16)&0xFF))self.writeStr(chr((l&0xFF))self.writeStr(chr(l&0xFF))elifl0x10000000:l|=0xE0000000self.writeStr(chr((l24)&0xFF))self.writeStr(chr((l16)&0xFF))self.writeStr(chr((l&0xFF))self.writeStr(chr(l&0xFF))else:self.writeStr(chr(0xF0))self.writeStr(chr((l24)&0xFF))self.writeStr(chr((l16)&0xFF))self.writeStr(chr((l&0xFF))self.writeStr(chr(l&0xFF))defreadLen(self):c=ord(self.readStr(1))if(c&0x80)==0x00:passelif(c&0xC0)==0x80:c&=~0xC0c=8c+=ord(self.readStr(1))elif(c&0xE0)==0xC0:c&=~0xE0c=8c+=ord(self.readStr(1))c=8c+=ord(self.readStr(1))elif(c&0xF0)==0xE0:c&=~0xF0c=8c+=ord(self.readStr(1))c=8c+=ord(self.readStr(1))c=8c+=ord(self.readStr(1))elif(c&0xF8)==0xF0:c=ord(self.readStr(1))c=8c+=ord(self.readStr(1))c=8c+=ord(self.readStr(1))c=8c+=ord(self.readStr(1))returncdefwriteStr(self,str):n=0;whilenlen(str):r=self.sk.send(str[n:])ifr==0:raiseRuntimeError,connectionclosedbyremoteendn+=rdefreadStr(self,length):ret=''whilelen(ret)length:s=self.sk.recv(length-len(ret))ifs=='':raiseRuntimeError,connectionclosedbyremoteendret+=sreturnretdefmain():s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)s.connect((sys.argv[1],8728))apiros=ApiRos(s);apiros.login(sys.argv[2],sys.argv[3]);inputsentence=[]while1:r=select.select([s,sys.stdin],[],[],None)ifsinr[0]:#somethingtoreadinsocket,readsentencex=apiros.readSentence()ifsys.stdininr[0]:#readlinefrominputandstripoffnewlinel=sys.stdin.readline()l=l[:-1]#ifemptyline,sendsentenceandstartwithnew#otherwiseappendtoinputsentenceifl=='':apiros.writeSentence(inputsentence)inputsentence=[]else:inputsentence.append(l)if__name__=='__main__':main()Examplerun:debian@localhost:~/api-test$./api.py10.0.0.1admin''/login!done=ret=93b438ec9b80057c06dd9fe67d56aa9a/login=name=admin=response=00e134102a9d330dd7b1849fedfea3cb57!done/user/getall/user/getall!re=.id=*1=disabled=no=name=admin=group=full=address=0.0.0.0/0=netmask=0.0.0.0!done以上部分只是简介,详情查看官方ROSAPI接口Wiki:接口官方教程支持v4.x以上版本
本文标题:ROS API接口官方教程支持v4
链接地址:https://www.777doc.com/doc-4437669 .html