简体   繁体   中英

using a proxy for whole session in requests module

I am using burp suite as a local proxy to see the requests made.

import requests
p = {'http': 'http://127.0.0.1:8080', 'https': 'http://127.0.0.1:8080'}
s = requests.Session()
s.proxies.update(p)
res = s.get('http://httpbin.org')

I want to define proxy once for whole session. Using proxy every time like:

res = s.get('http://httpbin.org', proxies=p)

works fine but just using s.proxies.update(p) doesn't work. Any thoughts? I have seen this question but no help.

I don't believe you can do what you want to do.. According to the requests documentation at http://docs.python-requests.org/en/master/user/advanced/#proxies :

If you need to use a proxy, you can configure individual requests with the proxies argument to any request method ... [or you can] configure proxies by setting the environment variables HTTP_PROXY and HTTPS_PROXY.

So if you wanted to not have to switch the proxy, and you have a set proxy at http://127.0.0.1:8080 , then I would just follow the docs:

$ export HTTP_PROXY="http://127.0.0.1:8080"
$ export HTTPS_PROXY="http://127.0.0.1:8080"

$ python
>>> import requests
>>> requests.get('http://example.org')

Of course, setting the envs can be done programmatically, but it just depends on what you want to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM