import requests
class HttpRequest:
"""使用requests封装get和post请求"""
def http_request(self, url, data, method, cookie=None):
"""url:请求的地址http://xxx:port
data 传递的参数,非必填参数,传递字典
method 请求方法,传递字符串
cookie 请求的时候传递的cookies值
"""
if method.lower == ‘get‘:
res = requests.get(url, data, cookies=cookie)
else:
res = requests.post(url, data, cookies=cookie)
return res
if __name__ == ‘__main__‘:
# 登录接口
url = "http://test.lemonban.com/futureloan/mvc/api/member/login"
data = {"mobilephone": "18688773467", "pwd": "123456"}
res = HttpRequest().http_request(url, data, ‘get‘)
print(res.json())
# 充值接口
recharge_url = "http://test.lemonban.com/futureloan/mvc/api/member/recharge"
recharge_data = {"mobilephone": "18688773467", "amount": "1000"}
res = HttpRequest().http_request(recharge_url, recharge_data, ‘post‘, res.cookies)
print(res.json())
# 请求https的网址时requests.post(url, data, verify=False)
原文:https://www.cnblogs.com/albeexu/p/12944878.html