python接口自动化

1、在base.py文件中写入

# conding:utf8

import requests
import json

class ApiDemo(object):

def __init__(self):
pass

# 请求方式为get
def api_get(self, url, params=None, headers=None):
try:
r = requests.get(url=url, params=params, headers=None)
# print('get请求结果:%s'%response)
return r
except Exception as e:
print('get请求错误结果:%s'%e)

# 请求方式为post
def api_post(self, url, data, headers=None):
# data = json.dumps(data)
try:
r = requests.post(url=url, data=data, headers=None)
return r
# print("post请求结果为:%s" % response)
except Exception as e:
print("post请求错误,错误原因:%s" % e)

if __name__ == '__main__':
demo = ApiDemo()
msg = demo.api_get('http://www.baidu.com')
print(msg.text)


2、在api_login文件中加入
# conding:utf8

from api_base.base import ApiDemo

class ApiLogin(ApiDemo):

ios_headers = {'User-Agent': 'LiGuClient/2.3.70 (iPhone; iOS 11.3.1; Scale/3.00)'}
# self.android_headers = {'User-Agent':''}
web_headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36'}

def __init__(self, url, username, password, type):
self.url = url
self.username = username
self.password = password
self.type = type
self.headers = self.ios_headers

def login(self):
self.data = {'account': self.username,
'password': self.password,
'type': self.type}
res = self.api_post(url=self.url, data=self.data, headers=self.ios_headers)
return res

if __name__ == '__main__':
url = '***'
L = ApiLogin(url=url, username='15911113333', password='36be741433c076b2984843dcb068ad610958ebbf1ed087ce928ede1a09406f993b43628519c61dcc4c9ffe6d701c9863b23ce7744c64452625f089b9d79abb62a588343185fe5b2b9fd7c85985941c991f3e3625afa89d20b0af9ea7690431d3ca2146a2e62e957fb00456dfebfedf01541ffe9eb4a95c5411e28cc6e94f6d4c', type=0)
l = L.login()
# l = L.login(url=url, username='15911113333', password='36be741433c076b2984843dcb068ad610958ebbf1ed087ce928ede1a09406f993b43628519c61dcc4c9ffe6d701c9863b23ce7744c64452625f089b9d79abb62a588343185fe5b2b9fd7c85985941c991f3e3625afa89d20b0af9ea7690431d3ca2146a2e62e957fb00456dfebfedf01541ffe9eb4a95c5411e28cc6e94f6d4c', type=0)
print(l.headers)

 

 

posted @ 2019-11-06 15:58  菜菜_包包  阅读(453)  评论(0编辑  收藏  举报