python3 request模块初使用

时间:2021-07-17 11:10:57
 import requests
class Interface_Request: def __init__(self,url,mobilephone,pwd):
'''login参数初始化'''
self.url = url
self.dict = {'mobilephone':mobilephone,'pwd':pwd} def http_request(self,way):
'''login接口请求'''
if way=='get':
return requests.get(self.url,self.dict).headers
elif way=='post':
while(True):
Question = input('是否使用json传递?请输入y/n')
if Question=='y' or Question=='n':
if Question=='y':
return requests.post(self.url,json=self.dict).headers
elif Question == 'n':
return requests.post(self.url,self.dict).headers
else:
print('请重新输入!')
continue
if __name__ == '__main__':
login = Interface_Request('URL地址','手机号','密码')
print('get传参:',login.http_request('get'))
print('post传参:',login.http_request('post'))