问题背景:
在使用RF框架跑自动化用例的时候,需要多台window机器进行配合,RF只存于其中一台window机器上面,其他机器可通过该机器发起的http请求做出相应的配合操作,这个时候就需要其他window机器监听接受http请求并且给出response以响应操作是否成功。
解决方案:
步骤一:在远程机器起一个http服务,用作监听发来的请求,根据具体的请求去调用相应的函数执行操作。脚本如下:
ip = socket.gethostbyname(hostname)
print(ip)
host = (ip, 8080)
# 操作函数名称列表
functionList = ['open_demo']
# 要操作的具体函数
def open_demo(data):
print('do something xxx')
return data
class Request(CGIHTTPRequestHandler):
def do_POST(self):
# 获取的请求中带的参数
data = self.rfile.read(int(self.headers['Content-Length']))
data = str(data, 'utf-8')
print(type(data))
start = self.path.rindex('/')
# 获取要执行的操作
urlkey = self.path[start+1:]
if urlkey in functionList:
msg = globals()[urlkey](data)
print(msg)
print(type(msg))
# 构造responese返回
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(msg).encode('utf-8'))
if __name__ == '__main__':
# 启动http服务
server = HTTPServer(host, Request)
server.serve_forever()
步骤二:在本地机器上发起调用
import requests
headers = {"Content-Type": "application/json"}
request_data = {
"type": 0,
"userName": "xxxxxxxxx",
"password": "xxxxxxxxx"
}
url = "http://192.168.0.104:8080/open_demo"
resp = requests.post(url, headers=headers, json=request_data)
print(resp.content)
通过如上操作,就能够实现简易服务端的目的,实现了自动化用例跨多台window机器的配合操作
调用结果展示:
接收端:
发送端: