在请求模块

时间:2017-03-26 11:29:17

标签: python proxy python-requests

我使用burp suite作为本地代理来查看请求。

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')

我想为整个会话定义一次代理。每次使用代理如:

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

工作正常,但只使用s.proxies.update(p)无法正常工作。有什么想法吗?我见过this question但没有帮助。

1 个答案:

答案 0 :(得分:0)

我不相信你能做你想做的事。根据http://docs.python-requests.org/en/master/user/advanced/#proxies的请求文件:

  

如果您需要使用代理,您可以使用任何请求方法的proxies参数配置各个请求... [或者您可以]通过设置环境变量HTTP_PROXY和HTTPS_PROXY来配置代理。

因此,如果您不想切换代理,并且您在http://127.0.0.1:8080设置了代理,那么我只需按照文档进行操作:

$ 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')

当然,设置envs 可以以编程方式完成,但这取决于你想要做什么。