Python接口自动化之requests请求封装!_python requests封装接口方法(1)

   日期:2024-12-26    作者:vfng5 移动:http://ljhr2012.riyuangf.com/mobile/quote/40659.html

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

Python接口自动化之requests请求封装!_python requests封装接口方法(1)

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Python知识点,真正体系化

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

正文

 

if name == ‘main’:
# 以下是测试代码
# get请求接口
url = ‘https://api.apiopen.top/getJoke?page=1&count=2&type=video’
res = RequestHandler().get(url)
# post请求接口
url2 = ‘http://127.0.0.1:8000/user/login/’
payload = {
“username”: “vivi”,
“password”: “123456”
}
res2 = RequestHandler().post(url2,json=payload)
print(res.json())
print(res2.json())

 

{‘code’: 200, ‘message’: ‘成功!’, ‘result’: [{‘sid’: ‘31004305’, ‘text’: ‘羊:师傅,理个发,稍微修一下就行’, ‘type’: ‘video’, ‘thumbnail’: ‘http://wimg.spriteapp.cn/picture/2020/0410/5e8fbf227c7f3_wpd.jpg’, ‘video’: ‘http://uvideo.spriteapp.cn/video/2020/0410/5e8fbf227c7f3_wpd.mp4’, ‘images’: None, ‘up’: ‘95’, ‘down’: ‘1’, ‘forward’: ‘0’, ‘comment’: ‘25’, ‘uid’: ‘23189193’, ‘name’: ‘青川小舟’, ‘header’: ‘http://wimg.spriteapp.cn/profile/large/2019/12/24/5e01934bb01b5_mini.jpg’, ‘top_comments_content’: None, ‘top_comments_voiceuri’: None, ‘top_comments_uid’: None, ‘top_comments_name’: None, ‘top_comments_header’: None, ‘passtime’: ‘2020-04-12 01:43:02’}, {‘sid’: ‘30559863’, ‘text’: ‘机器人女友,除了不能生孩子,其他的啥都会,价格239000元’, ‘type’: ‘video’, ‘thumbnail’: ‘http://wimg.spriteapp.cn/picture/2020/0306/5e61a41172a1b_wpd.jpg’, ‘video’: ‘http://uvideo.spriteapp.cn/video/2020/0306/5e61a41172a1b_wpd.mp4’, ‘images’: None, ‘up’: ‘80’, ‘down’: ‘6’, ‘forward’: ‘3’, ‘comment’: ‘20’, ‘uid’: ‘23131273’, ‘name’: ‘水到渠成’, ‘header’: ‘http://wimg.spriteapp.cn/profile/large/2019/07/04/5d1d90349cd1a_mini.jpg’, ‘top_comments_content’: ‘为游戏做的秀’, ‘top_comments_voiceuri’: ‘’, ‘top_comments_uid’: ‘10250040’, ‘top_comments_name’: ‘不得姐用户’, ‘top_comments_header’: ‘http://wimg.spriteapp.cn/profile’, ‘passtime’: ‘2020-04-11 20:43:49’}]}
{‘token’: ‘eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VybmFtZSI6InZpdmkiLCJleHAiOjE1ODY4NTc0MzcsImVtYWlsIjoidml2aUBxcS5jb20ifQ.k6y0dAfNU2o9Hd9LFfxEk1HKgczlQfUaKE-imPfTsm4’, ‘user_id’: 1, ‘username’: ‘vivi’}

 

现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的
qq群号:691998057【暗号:csdn999】

 

def get(url, params=None, **kwargs):
r""“Sends a GET request.
:param url: URL for the new :class: object.
:param params: (optional) Dictionary, list of tuples or bytes to send
in the query string for the :class:.
:param **kwargs: Optional arguments that takes.
:return: :class: object
:rtype: requests.Response
“””
kwargs.setdefault(‘allow_redirects’, True)
return request(‘get’, url, params=params, **kwargs)

 

def post(url, data=None, json=None, **kwargs):
r""“Sends a POST request.
:param url: URL for the new :class: object.
:param data: (optional) Dictionary, list of tuples, bytes, or file-like
object to send in the body of the :class:.
:param json: (optional) json data to send in the body of the :class:.
:param **kwargs: Optional arguments that takes.
:return: :class: object
:rtype: requests.Response
“””
return request(‘post’, url, data=data, json=json, **kwargs)

 

def request(method, url, **kwargs):
“”“Constructs and sends a :class:.
:param method: method for the new :class: object.
:param url: URL for the new :class: object.
:param params: (optional) Dictionary, list of tuples or bytes to send
in the query string for the :class:.
:param data: (optional) Dictionary, list of tuples, bytes, or file-like
object to send in the body of the :class:.
:param json: (optional) A JSON serializable Python object to send in the body of the :class:.
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:.
:param cookies: (optional) Dict or CookieJar object to send with the :class:.
:param files: (optional) Dictionary of (or ) for multipart encoding upload.
can be a 2-tuple , 3-tuple
or a 4-tuple , where is a string
defining the content type of the given file and a dict-like object containing additional headers
to add for the file.
:param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
:param timeout: (optional) How many seconds to wait for the server to send data
before giving up, as a float, or a :ref: tuple.
:type timeout: float or tuple
:param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to .
:type allow_redirects: bool
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
:param verify: (optional) Either a boolean, in which case it controls whether we verify
the server’s TLS certificate, or a string, in which case it must be a path
to a CA bundle to use. Defaults to .
:param stream: (optional) if , the response content will be immediately downloaded.
:param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, (‘cert’, ‘key’) pair.
:return: :class: object
:rtype: requests.Response
Usage::
>>> import requests
>>> req = requests.request(‘GET’, ‘https://httpbin.org/get’)
<Response [200]>
“””
# By using the ‘with’ statement we are sure the session is closed, thus we
# avoid leaving sockets open which can trigger a ResourceWarning in some
# cases, and look like a memory leak in others.
with sessions.Session() as session:
return session.request(method=method, url=url, **kwargs)


特别提示:本信息由相关用户自行提供,真实性未证实,仅供参考。请谨慎采用,风险自负。


举报收藏 0评论 0
0相关评论
相关最新动态
推荐最新动态
点击排行
{
网站首页  |  关于我们  |  联系方式  |  使用协议  |  隐私政策  |  版权隐私  |  网站地图  |  排名推广  |  广告服务  |  积分换礼  |  网站留言  |  RSS订阅  |  违规举报  |  鄂ICP备2020018471号