【问题标题】:Is there a way to enable tor as system proxy settings through Python in Linux有没有办法在 Linux 中通过 Python 启用 tor 作为系统代理设置
【发布时间】:2019-05-28 10:00:00
【问题描述】:

我正在做一个项目,该项目需要访问一些启用和未启用 Tor 的网站,并记录内容差异。 我正在研究 deepin(这是一个基于 linux 的 debian 发行版),并使用 Python 2.7 来完成任务。问题是我必须手动启用/禁用 tor,并在每次运行脚本时更改系统代理设置。现在,我知道我可以从 Python 本身发出一个 shell 命令来启用 tor(服务 tor 启动),但我不知道如何从 Python 启用/禁用系统代理设置。

我已经尝试过this,但没有成功。

【问题讨论】:

标签: python linux proxy tor


【解决方案1】:

使用os.system 像这样设置所需的代理。

import os
os.system("export http_proxy="http://username:Password@Proxy_IP:Port/")

要取消设置,只需使用

os.system("unset http_proxy")

编辑

Tor 使用 SOCKS 代理。对于 socks 代理,请使用

os.system("export socks_proxy="socks://username:Password@Proxy_IP:Port/")

【讨论】:

  • 问题是,Tor 不是 HTTP 代理,而是 SOCKS 代理。我尝试将您的代码修改为“socks_proxy”而不是“http_proxy”,然后尝试使用requests.session()从httpbin.org/ip获取我的IP,但返回的IP与我的原始IP地址相同,这意味着请求没有通过tor。我用过:os.system("export socks_proxy=\"http://:@127.0.0.1:9050/\"")
  • @KhizarAmin 我正在更新我的 socks 代理答案。你对袜子的尝试是部分正确的。请参阅答案中的编辑以获取正确的命令。
【解决方案2】:

搞定了,在这里发帖以防其他人遇到同样的问题:

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

将 network.proxy.type 更改为 1 会重置代理设置。找到解决方案here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    • 2015-02-19
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多