编写HTTP请求py文件:httpRequests.py
import requests class Requestes(): ‘‘‘ HTTP请求类 ‘‘‘ def __init__(self,host,test_url,method,param): ‘‘‘ :param host: 服务器ip与port :param test_url: 测试接口地址 :param method: 请求方法 :param param: 请求参数 ‘‘‘ self.host = host self.test_url = test_url self.method = method self.param = param def http_requests(self): ‘‘‘ HTTP请求方法,方法暂时有GET与POST :return:请求响应结果 ‘‘‘ try: if self.method.upper() == "GET": #Excel获取出的数据为str类型,param需要dict类型,需要eval()方法转换一下 response = requests.get(self.host+self.test_url,eval(self.param)) return response elif self.method.upper() == "POST": response = requests.post(self.host+self.test_url,eval(self.param)) return response else: print(‘请求方法错误,错误方法%s‘%self.method) except Exception as e: print(‘请求错误,错误原因%s‘ % e) raise e
原文地址:https://www.cnblogs.com/wuhl-89/p/9335523.html
时间: 2024-10-31 13:47:47