使用词干切换身份时的一般 SOCKS 服务器故障 [英] General SOCKS server failure when switching identity using stem

查看:45
本文介绍了使用词干切换身份时的一般 SOCKS 服务器故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在远程服务器 (Ubuntu) 上运行的 Tor 端口为 9150,控制端口为 9151.我已经确认两者都通过 netstat -ant 运行.

I have Tor running on a remote server (Ubuntu) on port 9150 with the control port on 9151. I've confirmed both are running via netstat -ant.

这是我的代码,它引发了 SOCKS5Error: 0x01: General SOCKS server failure 错误.

Here is my code which is eliciting the SOCKS5Error: 0x01: General SOCKS server failure error.

import socks
import socket
socks.set_default_proxy(socks.SOCKS5, server_ip, 9150)
socket.socket = socks.socksocket

我可以从任何库发出请求,并使用 Tor ip 地址成功返回响应.

I can make requests from any library and successfully get responses back with a tor ip address.

但是以下是导致错误的原因:

However the following is what causes the error:

from stem import Signal
from stem.control import Controller

with Controller.from_port(port = 9151) as controller:
  controller.authenticate(password)
  controller.signal(Signal.NEWNYM)

如果我运行上面的没有使用socks(第一个片段)设置代理,我可以毫无问题地发出信号.

If I run the above without setting up the proxy using socks (first snippet), I can issue signals with no trouble.

推荐答案

一旦连接到 Tor,就无法打开新控制器.尝试在脚本顶部打开一个控制器.然后 Tor 连接和信号器使用相同的控制器对象.

You can't open a new controller once you've connected to Tor. Try opening a controller right at the top of your script. Then both the Tor connection and signaller use the same controller object.

这似乎适用于 Python3:

This seems to work with Python3:

import time

import socket
import socks

import requests
from bs4 import BeautifulSoup
from stem import Signal
from stem.control import Controller

controller = Controller.from_port(port=9051)


def connectTor():
    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5 , "127.0.0.1", 9050, True)
    socket.socket = socks.socksocket


def renew_tor():
    controller.authenticate(<INSERT YOUR PASSPHRASE HERE>)
    controller.signal(Signal.NEWNYM)


def show_my_ip():
    url = "http://www.showmyip.gr/"
    r = requests.Session()
    page = r.get(url)
    soup = BeautifulSoup(page.content, "lxml")
    ip_address = soup.find("span",{"class":"ip_address"}).text.strip()
    print(ip_address)


for i in range(10):
    renew_tor()
    connectTor()
    showmyip()
    time.sleep(10)

这篇关于使用词干切换身份时的一般 SOCKS 服务器故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆