如何强制所有对 pythons requests.get 的调用默认使用代理?

How to force all calls to pythons requests.get to use proxy by default?

我在我的代码中使用第三方库来获取访问令牌 (ADAL)。这个库有很多对 requests.getrequests.post 的调用。如何强制所有调用使用用户提供的代理,而不必修改对 requests.get('http://example.com', proxies=proxies).

的每个调用

我无法导出 HTTP_PROXY。我必须在我的脚本中完成它。

您可以修改补丁请求。

在脚本的最开头:

import requests
import functools

orig_get = requests.get

proxies = { 
  'http': 'http://10.10.1.10:3128',
  'https': 'http://10.10.1.10:1080',
}
requests.get = functools.partial(orig_get, proxies=proxies)