[英]Is there a way to enable tor as system proxy settings through Python in Linux
I am working on a project which requires accessing some websites with and without tor enabled, and record the content differences. 我正在做一个项目,该项目需要在启用和未启用Tor的情况下访问某些网站,并记录内容差异。 I am working on deepin (which is a linux based debian distro), and using Python 2.7 to accomplish the task.
我正在研究deepin(这是一个基于Linux的debian发行版),并使用Python 2.7完成任务。 The problem is that I have to manually enable/disable tor, and change the system proxy settings every time I run the script.
问题是我必须手动启用/禁用tor,并在每次运行脚本时更改系统代理设置。 Now, I am aware that I can issue a shell command from Python itself to enable tor (service tor start), but I cannot figure out how to enable/disable the system proxy settings from Python.
现在,我知道可以从Python本身发出shell命令来启用tor(服务tor启动),但是我无法弄清楚如何从Python启用/禁用系统代理设置。
Use os.system
to set the desired proxy like this. 使用
os.system
这样设置所需的代理。
import os
os.system("export http_proxy="http://username:Password@Proxy_IP:Port/")
To unset, simply use 要取消设置,只需使用
os.system("unset http_proxy")
EDIT 编辑
Tor uses SOCKS proxy. Tor使用SOCKS代理。 For socks proxy, use
对于袜子代理,请使用
os.system("export socks_proxy="socks://username:Password@Proxy_IP:Port/")
Got the thing working, posting here in case someone else has the same problem: 事情正常了,如果其他人遇到相同的问题,请在此处发布:
from selenium import webdriver
import stem.process
from stem import Signal
from stem.control import Controller
from splinter import Browser
proxyIP = "127.0.0.1"
proxyPort = 9050
proxy_settings = {"network.proxy.type":0,
"network.proxy.socks": proxyIP,
"network.proxy.socks_port": proxyPort
}
browser = Browser('firefox', profile_preferences=proxy_settings)
driver = browser.driver
driver.get('https://whatismyip.com')
changing network.proxy.type to 1 resets the proxy settings. 将network.proxy.type更改为1将重置代理设置。 Solution found here
解决方案在这里找到
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.