为什么不能改变我的IP? Tor&蟒蛇

时间:2017-04-08 16:45:33

标签: python ip tor

您好我曾经尝试在抓取网站的一些请求后更改我的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)

1 个答案:

答案 0 :(得分:0)

嘿,我发现了一个视频并解决了我的问题,这里是视频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())