本文分享为Python的Requests在Session中设定默认proxies和timeout的方法。
当针对一个domain的多个请求短时间内被连续发出时,用Session对象比用requests的动词方法更有效率。如果需要代理或者tiemout,可以将设定绑定在Session对象上,后面请求的写法也会更简洁。
Session对象有proxies
属性,并可以直接赋值,但似乎不起作用,用update
方法则可以。
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> session=requests.Session()
# Session对象的属性和方法,有proxies,没有timeout
>>> session.
session.__attrs__ session.__init__( session.__weakref__ session.headers session.put(
session.__class__( session.__module__ session.adapters session.hooks session.rebuild_auth(
session.__delattr__( session.__new__( session.auth session.max_redirects session.rebuild_method(
session.__dict__ session.__reduce__( session.cert session.merge_environment_settings( session.rebuild_proxies(
session.__doc__ session.__reduce_ex__( session.close( session.mount( session.request(
session.__enter__( session.__repr__( session.cookies session.options( session.resolve_redirects(
session.__exit__( session.__setattr__( session.delete( session.params session.send(
session.__format__( session.__setstate__( session.get( session.patch( session.stream
session.__getattribute__( session.__sizeof__( session.get_adapter( session.post( session.trust_env
session.__getstate__( session.__str__( session.get_redirect_target( session.prepare_request( session.verify
session.__hash__( session.__subclasshook__( session.head( session.proxies
>>> session.proxies
{}
>>> session.proxies={"http":"http://127.0.0.1:1080"}
>>> session.proxies
{'http': 'http://127.0.0.1:1080'}
>>> session.proxies.update({"http":"http://127.0.0.1:1080"})
>>> session.proxies
{'http': 'http://127.0.0.1:1080'}
在Requests的早期版本,Session对象有timeout
属性,可以直接赋值,并在整个会话期间起作用,但因为timeout是传输层的问题,而非会话层任务,因此最晚在1.0
之后的版本被移除了。相关讨论见“requests.Session” should allow “timeout” attribute · Issue #1563和Feature Request: Add timeout to session · Issue #2011等。目前推荐的方法是:
class MyHTTPAdapter(requests.adapters.HTTPAdapter):
def __init__(self, timeout=None, *args, **kwargs):
self.timeout = timeout
super(MyHTTPAdapter, self).__init__(*args, **kwargs)
def send(self, *args, **kwargs):
kwargs['timeout'] = self.timeout
return super(MyHTTPAdapter, self).send(*args, **kwargs)
s = requests.Session()
s.mount("http://", MyHTTPAdapter(timeout=10))
https://github.com/kennethreitz/requests/issues/2011
参考资料
- Feature Request: Add timeout to session · Issue #2011
- How can I set a single proxy for a requests session object?
-- EOF --
本文最后修改于6年前 (2019-05-13)