如何通过 http 代理传递所有 Python 的流量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31639742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:20:38  来源:igfitidea点击:

How to pass all Python's traffics through a http proxy?

pythonpython-2.7proxyreverse-proxy

提问by habedi

I want to pass all Python's traffics through a http proxy server, I checked urlib2 and requests packages for instance, they can be configured to use proxies but how could I use something like a system-wide proxy for Python to proxy all the data out?

我想通过 http 代理服务器传递所有 Python 的流量,例如,我检查了 urlib2 并请求包,它们可以配置为使用代理,但是我怎么能使用诸如 Python 的系统范围代理之类的东西来代理所有数据呢?

回答by efirvida

Linux systemfirst export the environment variables like this

Linux系统先这样导出环境变量

$ export http_proxy="http://<user>:<pass>@<proxy>:<port>"
$ export HTTP_PROXY="http://<user>:<pass>@<proxy>:<port>"

$ export https_proxy="http://<user>:<pass>@<proxy>:<port>"
$ export HTTPS_PROXY="http://<user>:<pass>@<proxy>:<port>"

or in the script that you want to pass through the proxy

或在您要通过代理传递的脚本中

import os

proxy = 'http://<user>:<pass>@<proxy>:<port>'

os.environ['http_proxy'] = proxy 
os.environ['HTTP_PROXY'] = proxy
os.environ['https_proxy'] = proxy
os.environ['HTTPS_PROXY'] = proxy

#your code goes here.............

then run python script

然后运行python脚本

$ python my_script.py

UPDATE

更新

An also you can use redsockswith this tool you can redirect silently all your TCP connections to a PROXY with or without authentication. But you have to take care because it's for all connections not only for the python.

您还可以将redsocks与此工具一起使用,您可以将所有 TCP 连接以静默方式重定向到代理,无论是否进行身份验证。但是你必须小心,因为它不仅适用于 python 的所有连接。

Windows systemsyou can use tools like freecap, proxifier, proxycap, and configure to run behind the python executable

Windows系统,您可以使用freecapproxifierproxycap和configure等工具在python可执行文件后面运行。

回答by chisaipete

Admittedly, this isn't exactly what you are looking for, but if you know the programmatic source of all your network traffic in your python code, you can do this proxy wrapping in the code itself. A pure python solution is suggested using the PySocksmodule in the following link:

诚然,这并不是您要寻找的内容,但是如果您知道 Python 代码中所有网络流量的程序化来源,则可以在代码本身中执行此代理包装。建议使用PySocks以下链接中的模块使用纯 python 解决方案:

https://github.com/httplib2/httplib2/issues/22

https://github.com/httplib2/httplib2/issues/22

import httplib2
# detect presense of proxy and use env varibles if they exist
pi = httplib2.proxy_info_from_environment()
if pi:
    import socks
    socks.setdefaultproxy(pi.proxy_type, pi.proxy_host, pi.proxy_port)
    socks.wrapmodule(httplib2)    

# now all calls through httplib2 should use the proxy settings
httplib2.Http()

Now every call made using httplib2will use those proxy settings. You should be able to wrap any networking module that uses sockets to use this proxy.

现在,每次使用 using 进行的调用都httplib2将使用这些代理设置。您应该能够包装任何使用套接字来使用此代理的网络模块。

https://github.com/Anorov/PySocks

https://github.com/Anorov/PySocks

They mention there that this isn't recommended, but it seems to be working fine for me.

他们在那里提到不推荐这样做,但它似乎对我来说工作正常。