首页 / PYTHON / python接口自动化
python接口自动化
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python接口自动化,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2393字,纯文字阅读大概需要4分钟。
内容图文
![python接口自动化](/upload/InfoBanner/zyjiaocheng/671/189e98750238483cab54f39a39f7b0f3.jpg)
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)
response = r.text
# # print('get请求结果:%s'%response)
return response
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)
response = r.text
# # print("post请求结果为:%s" % response)
return response
except Exception as e:
print("post请求错误,错误原因:%s" % e)
if __name__ == '__main__':
demo = ApiDemo()
msg = demo.api_get('http://www.baidu.com')
print(msg)
2、在api_login文件中加入
# conding:utf8
from 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.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)
内容总结
以上是互联网集市为您收集整理的python接口自动化全部内容,希望文章能够帮你解决python接口自动化所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。