码迷,mamicode.com
首页 > 编程语言 > 详细

python requests.request 和session.request区别究竟在哪里

时间:2019-10-21 19:01:08      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:ioi   mem   func   info   out   import   ade   timeout   ini   

技术图片

 

 

 

 

 

import requests

hd={"X-auth":"eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJzeXN0ZW0iLCJBUEkiOiIvdW1sb2dpbiIsInRpbWUiOjE1NjU3NjkzNTAzMjIsInVzZXJOYW1lIjoidl93YmthaXlhbmciLCJpYXQiOjE1NjU3NjkzNTAsImp0aSI6ImZlNDMyM2I0LTk2NTctNDM3YS1iNjlmLTE1ZDZlNWUyNDZjNSJ9.nB9E93PLEiZZMjvmTOi2ytKCRVQk1hRtJdrxhQ_N87o"}

# api.py request() func
rep2=requests.request(method="post",url="http://10.107.119.12:8081/aaip-wms/video/queryeventbyid",data=json.dumps({"id":41579,"page":None,"size":None}))
print(rep2.text)

#sessions.py Session.session()and Session.request
s=requests.session()
rep3=s.request(method="post",url="http://10.10.119.12:8081/aaip/video/queryeventbyid",json=json.dumps({"id":41579,"page":None,"size":None}),headers=hd)
print(rep3.text)
"""
api.py
# 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.
def request(method, url, **kwargs):
with sessions.Session() as session:
return session.request(method=method, url=url, **kwargs)

sessions.py


class Session(object):


def __init__(self):
pass


def request(self,method, url,
params=None, data=None, headers=None, cookies=None, files=None,
auth=None, timeout=None, allow_redirects=True, proxies=None,
hooks=None, stream=None, verify=None, cert=None, json=None):
...
...
return resp


def session(self):
#返回一个实例化对象
return Session()
"""

python requests.request 和session.request区别究竟在哪里

标签:ioi   mem   func   info   out   import   ade   timeout   ini   

原文地址:https://www.cnblogs.com/SunshineKimi/p/11715285.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!