问题描述
我想通过 http 代理服务器传递所有 Python 的流量,例如,我检查了 urlib2 并请求包,它们可以配置为使用代理,但我怎么能使用诸如 Python 的系统范围代理之类的东西来代理所有数据出来了?
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?
推荐答案
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.............
然后运行python脚本
then run python script
$ python my_script.py
更新
你也可以使用 redsocks使用此工具,无论是否经过身份验证,您都可以静默地将所有 TCP 连接重定向到代理.但是你必须小心,因为它不仅适用于 python 的所有连接.
And 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 be carefull because it's for all connections not only for the python.
Windows 系统,您可以使用诸如 freecap、proxifier, proxycap,并配置为在 python 可执行文件后面运行
Windows systems you can use tools like freecap, proxifier, proxycap, and configure to run behind the python executable
这篇关于如何通过 http 代理传递所有 Python 的流量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!