通过Tor使用Python发出请求

通过Tor使用Python发出请求,python,tor,Python,Tor,我想使用Tor向一个网页发出多个GET请求。我想为每个请求使用不同的IP地址 import socks import socket socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 9150) socket.socket = socks.socksocket import requests print (requests.get('http://icanhazip.com')).content import time, socks, sock

我想使用Tor向一个网页发出多个GET请求。我想为每个请求使用不同的IP地址

import socks
import socket
socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 9150)
socket.socket = socks.socksocket
import requests
print (requests.get('http://icanhazip.com')).content
import time, socks, socket
from urllib2 import urlopen
from stem import Signal
from stem.control import Controller

nbrOfIpAddresses=3

with Controller.from_port(port = 9051) as controller:
   controller.authenticate(password = 'my_pwd')
   socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)
   socket.socket = socks.socksocket   

   for i in range(0, nbrOfIpAddresses):
       newIP=urlopen("http://icanhazip.com").read()
       print("NewIP Address: %s" % newIP)
       controller.signal(Signal.NEWNYM)
       if controller.is_newnym_available() == False:
        print("Waitting time for Tor to change IP: "+ str(controller.get_newnym_wait()) +" seconds")
        time.sleep(controller.get_newnym_wait())
   controller.close()

用这个,我提出了一个请求。如何更改IP地址以生成另一个IP地址?

以下是您要使用的代码(使用
pip安装stem下载stem软件包


祝你好运,希望这能奏效。

你的问题有两个方面-

  • 使用Tor发出请求
  • 根据要求更新连接(在您的情况下,每次请求后)

  • 第一部分 第一个很容易使用最新的(v2.10.0以上)库,另外还需要使用socks代理

    安装-

    pip install requests requests[socks]
    
    import requests
    
    def get_tor_session():
        session = requests.session()
        # Tor uses the 9050 port as the default socks port
        session.proxies = {'http':  'socks5://127.0.0.1:9050',
                           'https': 'socks5://127.0.0.1:9050'}
        return session
    
    # Make a request through the Tor connection
    # IP visible through Tor
    session = get_tor_session()
    print(session.get("http://httpbin.org/ip").text)
    # Above should print an IP different than your public IP
    
    # Following prints your normal public IP
    print(requests.get("http://httpbin.org/ip").text)
    
    from stem import Signal
    from stem.control import Controller
    
    # signal TOR for a new connection 
    def renew_connection():
        with Controller.from_port(port = 9051) as controller:
            controller.authenticate(password="password")
            controller.signal(Signal.NEWNYM)
    
    基本用法-

    pip install requests requests[socks]
    
    import requests
    
    def get_tor_session():
        session = requests.session()
        # Tor uses the 9050 port as the default socks port
        session.proxies = {'http':  'socks5://127.0.0.1:9050',
                           'https': 'socks5://127.0.0.1:9050'}
        return session
    
    # Make a request through the Tor connection
    # IP visible through Tor
    session = get_tor_session()
    print(session.get("http://httpbin.org/ip").text)
    # Above should print an IP different than your public IP
    
    # Following prints your normal public IP
    print(requests.get("http://httpbin.org/ip").text)
    
    from stem import Signal
    from stem.control import Controller
    
    # signal TOR for a new connection 
    def renew_connection():
        with Controller.from_port(port = 9051) as controller:
            controller.authenticate(password="password")
            controller.signal(Signal.NEWNYM)
    

    第二部分 要更新Tor IP,即要有一个新的可见出口IP,您需要能够通过其
    控制端口连接到Tor服务,然后发送
    NEWNYM
    信号

    默认情况下,正常Tor安装不会启用
    控制端口。您必须编辑并取消注释相应的行

    ControlPort 9051
    ## If you enable the controlport, be sure to enable one of these
    ## authentication methods, to prevent attackers from accessing it.
    HashedControlPassword 16:05834BCEDD478D1060F1D7E2CE98E9C13075E8D3061D702F63BCD674DE
    
    请注意,上面的
    HashedControlPassword
    用于密码
    “password”
    。如果要设置不同的密码,请在TORC中替换
    HashedControlPassword
    ,注意
    tor--hash password”“
    的输出,其中
    是要设置的密码

    针对Windows用户的警告:请参阅帖子

    windows上存在一个问题,如果使用以下命令安装tor,则TORC文件中的controlport设置将被忽略:

    tor --service install
    
    要解决此问题,请在编辑TORC文件后,键入以下命令:

    tor --service remove
    tor --service install -options ControlPort 9051
    

    好的,现在我们已经正确地配置了Tor,如果Tor已经在运行,您必须重新启动它

    sudo service tor restart
    
    Tor现在应该在9051
    ControlPort
    上启动并运行,通过它我们可以向它发送命令。我更喜欢用这个来控制Tor

    安装-

    pip install stem
    
    您现在可以通过调用以下函数来更新Tor IP

    更新IP-

    pip install requests requests[socks]
    
    import requests
    
    def get_tor_session():
        session = requests.session()
        # Tor uses the 9050 port as the default socks port
        session.proxies = {'http':  'socks5://127.0.0.1:9050',
                           'https': 'socks5://127.0.0.1:9050'}
        return session
    
    # Make a request through the Tor connection
    # IP visible through Tor
    session = get_tor_session()
    print(session.get("http://httpbin.org/ip").text)
    # Above should print an IP different than your public IP
    
    # Following prints your normal public IP
    print(requests.get("http://httpbin.org/ip").text)
    
    from stem import Signal
    from stem.control import Controller
    
    # signal TOR for a new connection 
    def renew_connection():
        with Controller.from_port(port = 9051) as controller:
            controller.authenticate(password="password")
            controller.signal(Signal.NEWNYM)
    
    要验证Tor是否有新的出口IP,只需重新运行第1部分中的代码。由于我不知道的原因,您需要创建一个新的
    会话
    对象才能使用新的IP

    session = get_tor_session()
    print(session.get("http://httpbin.org/ip").text)
    

    requesocks
    中的
    requests
    非常旧,它没有
    response.json()
    和许多其他东西

    我想保持我的代码干净。但是,
    请求
    目前还不支持socks5(有关更多详细信息,请阅读此线程)

    所以我现在使用了
    Privoxy
    作为连接Tor的http代理

    在Mac上安装和配置Privoxy

    brew install privoxy
    vim /usr/local/etc/privoxy/config
    # put this line in the config
    forward-socks5 / localhost:9050 .
    privoxy /usr/local/etc/privoxy/config
    
    sudo apt-get install privoxy
    sudo vim /etc/privoxy/config
    # put this line in the config
    forward-socks5 / localhost:9050 .
    sudo /etc/init.d/privoxy restart
    
    在Ubuntu上安装和配置Privoxy

    brew install privoxy
    vim /usr/local/etc/privoxy/config
    # put this line in the config
    forward-socks5 / localhost:9050 .
    privoxy /usr/local/etc/privoxy/config
    
    sudo apt-get install privoxy
    sudo vim /etc/privoxy/config
    # put this line in the config
    forward-socks5 / localhost:9050 .
    sudo /etc/init.d/privoxy restart
    
    现在我可以像http代理一样使用Tor了。下面是我的python脚本

    import requests
    
    proxies = {
      'http': 'http://127.0.0.1:8118',
    }
    
    print requests.get('http://httpbin.org/ip', proxies=proxies).text
    
    使用版本2.10.0中的SOCKS协议

    import requests
    proxies = {
        'http': 'socks5://localhost:9050',
        'https': 'socks5://localhost:9050'
    }
    url = 'http://httpbin.org/ip'
    print(requests.get(url, proxies=proxies).text)
    
    您可以使用库(无耻的插件)。它在PyPI上提供

    from torrequest import TorRequest
    
    with TorRequest() as tr:
      response = tr.get('http://ipecho.net/plain')
      print(response.text)  # not your IP address
    
      tr.reset_identity()
    
      response = tr.get('http://ipecho.net/plain')
      print(response.text)  # another IP address, not yours
    

    此答案完成Ashish Nitin Patil forwindows的答案 (请随时更新此答案)

    第二部分 上面的
    HashedControlPassword
    就是密码。如果要在控制台中设置不同的密码,请导航到
    \Tor Browser\Browser\TorBrowser\Tor
    ,然后键入以下命令:
    Tor.exe--hash password password\u XYZ | more
    )。它会给你一些类似于HashedControlPassword 16:54C092A8…
    这是你的密码。现在您可以将其添加到TORC文件(
    TorBrowser\Browser\TorBrowser\Data\Tor\TorC

    然后,您需要重新启动Tor:

    tor --service remove
    tor --service install -options ControlPort 9051
    
    要检查这是否有效,请键入
    netstat-an
    ,您现在将看到端口9051已打开

    请注意,
    tor--service install-…
    将创建
    tor Win32 service
    。出于某种原因,您似乎必须停止服务才能使用浏览器(运行
    services.msc


    编辑:您将找到许多信息(关于端口号和代理、Tor、Privoxy、自动切换用户代理…。

    此代码运行良好。使用Tor,它会在每次请求后更改IP地址

    import socks
    import socket
    socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 9150)
    socket.socket = socks.socksocket
    import requests
    print (requests.get('http://icanhazip.com')).content
    
    import time, socks, socket
    from urllib2 import urlopen
    from stem import Signal
    from stem.control import Controller
    
    nbrOfIpAddresses=3
    
    with Controller.from_port(port = 9051) as controller:
       controller.authenticate(password = 'my_pwd')
       socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)
       socket.socket = socks.socksocket   
    
       for i in range(0, nbrOfIpAddresses):
           newIP=urlopen("http://icanhazip.com").read()
           print("NewIP Address: %s" % newIP)
           controller.signal(Signal.NEWNYM)
           if controller.is_newnym_available() == False:
            print("Waitting time for Tor to change IP: "+ str(controller.get_newnym_wait()) +" seconds")
            time.sleep(controller.get_newnym_wait())
       controller.close()
    

    您可以尝试纯python tor协议实现。根本不需要原始的Tor客户端或Stem依赖项

    $ pip3 install torpy[requests]
    ...
    
    $ python3.7
    >>> from torpy.http.requests import TorRequests
    >>> with TorRequests() as tor_requests:
    ...    print("build circuit")
    ...    with tor_requests.get_session() as sess:
    ...        print(sess.get("http://httpbin.org/ip").json())
    ...        print(sess.get("http://httpbin.org/ip").json())
    ...    print("renew circuit")
    ...    with tor_requests.get_session() as sess:
    ...        print(sess.get("http://httpbin.org/ip").json())
    ...        print(sess.get("http://httpbin.org/ip").json())
    ...
    build circuit
    {'origin': '23.129.64.190, 23.129.64.190'}
    {'origin': '23.129.64.190, 23.129.64.190'}
    renew circuit
    {'origin': '198.98.50.112, 198.98.50.112'}
    {'origin': '198.98.50.112, 198.98.50.112'}
    

    因此,每次当您获得新会话时,您都会获得新标识(基本上,您会获得带有新退出节点的新回路)。请参阅自述文件中的更多示例,这是更新IP的好功能。Windows示例

    def renew_tor_ip():
        with Controller.from_port(port = 9051) as controller:
            controller.authenticate(password="aAjkaI19!!laksjd")
            controller.signal(Signal.NEWNYM)
    
    用法示例

    import requests
    import time
    from stem import Signal
    from stem.control import Controller
    
    
    def get_current_ip():
        session = requests.session()
    
        # TO Request URL with SOCKS over TOR
        session.proxies = {}
        session.proxies['http']='socks5h://localhost:9150'
        session.proxies['https']='socks5h://localhost:9150'
    
        try:
            r = session.get('http://httpbin.org/ip')
        except Exception as e:
            print(str(e))
        else:
            return r.text
    
    #16:8EE7AEE3F32EEEEB605C6AA6C47B47808CA6A81FA0D76546ADC05F0F15 to aAjkaI19!!laksjd
    #cmd shell "C:\Users\Arthur\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe" --hash-password aAjkaI19!!laksjd | more
    #Torcc config
    #ControlPort 9051
    #HashedControlPassword 16:8EE7AEE3F32EEEEB605C6AA6C47B47808CA6A81FA0D76546ADC05F0F15
    
    def renew_tor_ip():
        with Controller.from_port(port = 9051) as controller:
            controller.authenticate(password="aAjkaI19!!laksjd")
            controller.signal(Signal.NEWNYM)
    
    
    for i in range(5):
        print(get_current_ip())
        renew_tor_ip()
        time.sleep(5)
    

    这不是tor应该做的,即混淆或以其他方式使确定发送者的IP地址变得困难吗?使用stem@mhawke,但一旦进行了连接握手,它就会在它连接的IP地址跳集中保持不变,因此op可能会再次更改IP地址。在osx上,最后一行
    privoxy/usr/local/etc/privoxy/config
    返回此错误
    2016-08-06 23:47:01.761000048错误:配置文件中forward-socks5的参数数目错误。
    请求不允许使用socks 5代理。工作正常。您从未使用请求设置代理。当我用requests 2.11设置代理时,我得到连接错误
    无法建立新连接
    是,我错过了proxies参数。修正了,谢谢你的通知。我将请求更新到2.11并检查了这个脚本-它可以工作。你开始服务了吗?如果我停止tor,我会得到相同的错误。我尝试过这个解决方案,但得到了这个错误:错误(61,‘连接被拒绝’)。似乎9051端口在我的机器上没有打开,我在mac上,一直试图尽我所能打开这个端口,但仍然出现这个错误。有什么想法吗?@Kfn