【问题标题】:Why can't change my ip? Tor & Python为什么不能更改我的ip? Tor & Python
【发布时间】:2017-04-08 16:45:32
【问题描述】:

嗨,我一直在尝试在一些抓取网站的请求后更改我的 ip,但是当我运行我在某些博客上找到的这段代码时,任何事情都会发生,我的意思是我的 ip 是相同的在两个输出中。有人知道我做错了什么吗?

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

def set_new_ip():
    """Change IP using TOR"""
    with Controller.from_port(port=9051) as controller:
        controller.authenticate(password='my_password')
        controller.signal(Signal.NEWNYM)

local_proxy = '127.0.0.1:8118'
http_proxy = {'http://': local_proxy,
          'https://': local_proxy}

current_ip = requests.get(url='http://icanhazip.com/',
                      proxies=http_proxy,
                      verify=False)

print(current_ip.text)

set_new_ip()

current_ip = requests.get(url='http://icanhazip.com/',
                      proxies=http_proxy,
                      verify=False)

print(current_ip.text)

【问题讨论】:

    标签: python ip tor


    【解决方案1】:

    嘿,我找到了一个视频并解决了我的问题,这是视频https://www.youtube.com/watch?v=qTc8Vqn57pM

    这是我的代码

    from stem import Signal
    from stem.control import Controller
    import requests
    import socks, socket
    import time
    
    
    with Controller.from_port(port=9051) as controller:
        controller.authenticate(password='my_password')
        socks.setdefaultproxy(proxy_type=socks.PROXY_TYPE_SOCKS5, addr="127.0.0.1", port=9050)
        socket.socket = socks.socksocket
        while True:
            current_ip = requests.get(url='http://icanhazip.com/')
            print(current_ip.text)
            controller.signal(Signal.NEWNYM)
            time.sleep(controller.get_newnym_wait())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多