gpt4 book ai didi

python - 使用tor代理时如何控制tor

转载 作者:行者123 更新时间:2023-12-04 19:07:47 25 4
gpt4 key购买 nike

我正在尝试使用 tor 连接到资源,更改身份。但是在第一次连接后尝试更改身份会导致错误。
代码:

import urllib2, socks, socket
from stem import Signal
from stem.control import Controller


def newI():
with Controller.from_port(port=9051) as controller:
controller.authenticate()
controller.signal(Signal.NEWNYM)

newI()

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)
socket.socket = socks.socksocket

headers = {'User-Agent': 'Mozilla/3.0 (x86 [en] Windows NT 5.1; Sun)'}
req = urllib2.Request('https://google.com', None, headers)
response = urllib2.urlopen(req)
html = response.read()

newI()

我得到错误:
  File "/media/all/Run/e/c.py", line 21, in <module>
newI()
File "/media/all/Run/e/c.py", line 7, in newI
with Controller.from_port(port=9051) as controller:
File "/usr/local/lib/python2.7/dist-packages/stem/control.py", line 659, in from_port
control_port = stem.socket.ControlPort(address, port)
File "/usr/local/lib/python2.7/dist-packages/stem/socket.py", line 314, in __init__
self.connect()
File "/usr/local/lib/python2.7/dist-packages/stem/socket.py", line 187, in connect
self._socket = self._make_socket()
File "/usr/local/lib/python2.7/dist-packages/stem/socket.py", line 340, in _make_socket
control_socket.connect((self._control_addr, self._control_port))
File "/usr/lib/python2.7/dist-packages/socks.py", line 369, in connect
self.__negotiatesocks5(destpair[0],destpair[1])
File "/usr/lib/python2.7/dist-packages/socks.py", line 236, in __negotiatesocks5
raise Socks5Error(ord(resp[1]),_generalerrors[ord(resp[1])])
TypeError: __init__() takes exactly 2 arguments (3 given)

也许我需要禁用代理。告诉我如何正确地做到这一点。

更新:

使用 pycurl 它的工作:
import pycurl
import cStringIO
from stem import Signal
from stem.control import Controller


def newI():
with Controller.from_port(port=9051) as controller:
controller.authenticate()
controller.signal(Signal.NEWNYM)

newI()

buf = cStringIO.StringIO()
c = pycurl.Curl()
c.setopt(pycurl.URL, 'http://google.com/')
c.setopt(c.WRITEFUNCTION, buf.write)
c.setopt(pycurl.PROXY, 'localhost')
c.setopt(pycurl.PROXYPORT, 9050)
c.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5)
c.perform()
html = buf.getvalue()[7:]
buf.close()

newI()

最佳答案

问题是由 socket.socket = socks.socksocket 引起的线。

作为临时解决方案,可以保留 socket.socket 的备份副本。并在要求 Tor 提供新身份之前使用它来取消设置代理,然后再次设置代理。

代码如下所示:

import urllib2, socks, socket
from stem import Signal
from stem.control import Controller

old_socket = socket.socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)
socket.socket = socks.socksocket

def newI():
socket.socket = old_socket # don't use proxy
with Controller.from_port(port=9051) as controller:
controller.authenticate()
controller.signal(Signal.NEWNYM)
# set up the proxy again
socket.socket = socks.socksocket

newI()

headers = {'User-Agent': 'Mozilla/3.0 (x86 [en] Windows NT 5.1; Sun)'}
req = urllib2.Request('https://google.com', None, headers)
response = urllib2.urlopen(req)
html = response.read()

newI()

但是,我仍在等待有人发布有关错误发生原因的解释,以及更好的解决方法。

关于python - 使用tor代理时如何控制tor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20304301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com