在python中获取新的Ip

时间:2013-05-04 17:22:28

标签: python tor

我正在尝试通过python获取新的ip。

我在stackoverflow上找到了这个脚本:

import urllib2
from TorCtl import TorCtl

proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"})
opener = urllib2.build_opener(proxy_support) 

def newId():
    conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051, passphrase="123")
    conn.send_signal("NEWNYM")

for i in range(0, 10):
    print "case "+str(i+1)
    newId()
    proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"})
    urllib2.install_opener(opener)
    print(urllib2.urlopen("http://www.ifconfig.me/ip").read())

我的vidalia跑步和私密。我正确设置了我的设置:

系统偏好中的

(在mac上):

Web代理(HTTP):127.0.0.1:8118 和HTTPS相同

在我的privoxy配置文件中,我有这一行:

 forward-socks5   /               127.0.0.1:9051 .

在我的vidalia设置中,我有:

Image

虽然我仍然在运行代码时仍停留在案例1上,但我无法获得IP。 这是我的vidalia的日志:

May 04 19:15:27.381 [Notice] New control connection opened.
May 04 19:15:27.382 [Notice] New control connection opened.
May 04 19:15:33.709 [Notice] New control connection opened.
May 04 19:15:38.653 [Notice] New control connection opened.
May 04 19:15:57.382 [Notice] New control connection opened.
May 04 19:15:57.463 [Notice] New control connection opened.
May 04 19:15:57.464 [Notice] New control connection opened.
May 04 19:16:03.710 [Notice] New control connection opened.
May 04 19:16:19.656 [Notice] New control connection opened.
May 04 19:16:22.448 [Notice] New control connection opened.
May 04 19:16:22.569 [Notice] New control connection opened.
May 04 19:16:22.900 [Notice] New control connection opened.
May 04 19:16:27.382 [Notice] New control connection opened.
May 04 19:16:27.412 [Notice] New control connection opened.
May 04 19:16:27.413 [Notice] New control connection opened.

我做错了什么?

编辑:

当配置文件中的行处于活动状态时,我无法加载任何网站。

显然,如果我等了足够长的时间,我会收到错误:

urllib2.HTTPError: HTTP Error 503: Forwarding failure

2 个答案:

答案 0 :(得分:1)

这个问题似乎经常出现(12),所以我刚为它添加了FAQ entry。 Tor不支持循环IP的方法,但它允许您创建新标识。请不要过度,因为它会给Tor网络带来很大的负担。

TorCtl is deprecated,所以这是使用stem ...

的示例
from stem import Signal
from stem.control import Controller

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

答案 1 :(得分:0)

forward-socks5   /               127.0.0.1:9050 .

forward-socks5   /               127.0.0.1:9051 .