RouterOS之python调用API

RouterOS之python调⽤API
本⽂主要讲述官⽅提供的客户端以及⾃⼰写的增删查改⼯具:
ros_tool.py 功能总汇,展⽰界⾯⽤了python的GUL,模块⽤的为  tkinter  模块
其他⽂件为单个功能⽂件。
运⾏⽅式:python3  ros_tool.py  ip  username  password    (LINUX下,装过ros,改为⾃⼰的 ip,⽤户名,密码 直接运⾏即可)
ros为python以及其他语⾔提供了接⼝,官⽅给出了⼀个  TCP客户端的例⼦:
# !/usr/bin/env python3
# -*- coding:utf-8 -*-
import sys, time, binascii, socket, select
import hashlib
class ApiRos:
led数码管显示屏"Routeros api"
def __init__(self, sk):
self.sk = sk
self.currenttag = 0
def login(self, username, pwd):
for repl, attrs in self.talk(["/login"]):
chal = binascii.unhexlify((attrs['=ret']).encode('UTF-8'))
md = hashlib.md5()
md.update(b' 00')
md.de('UTF-8'))
md.update(chal)
self.talk(["/login", "=name=" + username,
"=response=00" + binascii.hexlify(md.digest()).decode('UTF-8')])
def talk(self, words):
if self.writeSentence(words) == 0: return        r = []
while 1:
i = adSentence();
if len(i) == 0: continue
reply = i[0]
attrs = {}
for w in i[1:]:
j = w.find('=', 1)
if (j == -1):
attrs[w] = ''
else:
attrs[w[:j]] = w[j + 1:]
r.append((reply, attrs))
if reply == '!done': return r
def writeSentence(self, words):
ret = 0
for w in words:
self.writeWord(w)
ret += 1
self.writeWord('')
return ret
def readSentence(self):
r = []
while 1:
w = adWord()
if w == '': return r
r.append(w)
def writeWord(self, w):
print(("<<< " + w))
self.writeLen(len(w))
self.writeStr(w)
def readWord(self):
ret = adLen())
print((">>> " + ret))
尼龙包胶线return ret
def writeLen(self, l):
if l < 0x80:
self.writeStr(chr(l))
elif l < 0x4000:
l |= 0x8000
self.writeStr(chr((l >> 8) & 0xFF))            self.writeStr(chr(l & 0xFF))
elif l < 0x200000:
l |= 0xC00000连续供墨打印机
self.writeStr(chr((l >> 16) & 0xFF))            self.writeStr(chr((l >> 8) & 0xFF))            self.writeStr(chr(l & 0xFF))
elif l < 0x10000000:
l |= 0xE0000000
self.writeStr(chr((l >> 24) & 0xFF))            self.writeStr(chr((l >> 16) & 0xFF))            self.writeStr(chr((l >> 8) & 0xFF))            self.writeStr(chr(l & 0xFF))
else:
self.writeStr(chr(0xF0))
self.writeStr(chr((l >> 24) & 0xFF))            self.writeStr(chr((l >> 16) & 0xFF))
薄膜式压力传感器self.writeStr(chr((l >> 8) & 0xFF))            self.writeStr(chr(l & 0xFF))
def readLen(self):
c = adStr(1))
if (c & 0x80) == 0x00:
pass
elif (c & 0xC0) == 0x80:
c &= ~0xC0
c <<= 8
c += adStr(1))
elif (c & 0xE0) == 0xC0:
c &= ~0xE0
c <<= 8
c += adStr(1))
c <<= 8
c += adStr(1))
elif (c & 0xF0) == 0xE0:
c &= ~0xF0
c <<= 8
c += adStr(1))
c <<= 8
c += adStr(1))
c <<= 8
c += adStr(1))
elif (c & 0xF8) == 0xF0:
c = adStr(1))
c <<= 8
c += adStr(1))
c <<= 8
c += adStr(1))
抗生素制作方法c <<= 8
c += adStr(1))
return c
def writeStr(self, str):
n = 0;
while n < len(str):
r = self.sk.send(bytes(str[n:], 'UTF-8'))
if r == 0: raise RuntimeError("connection closed by remote end")
n += r
def readStr(self, length):
ret = ''
while len(ret) < length:
s = v(length - len(ret))
if s == '': raise RuntimeError("connection closed by remote end")
ret += s.decode('UTF-8', 'replace')
return ret
def main():
s = None
for res addrinfo(sys.argv[1], "8728", socket.AF_UNSPEC, socket.SOCK_STREAM):        af, socktype, proto, canonname, sa = res
try:
s = socket.socket(af, socktype, proto)
except (, msg):
s = None
continue
try:
披肩按摩器
except (, msg):

本文发布于:2024-09-21 23:35:14,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/2/196305.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:提供   薄膜   查改   总汇
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议