简体   繁体   English

Python-IP不会随词干变化吗?

[英]Python - IP not changing with stem?

I wrote a script that changes the IP with stem, but it seems like it does not work. 我编写了一个脚本,该脚本使用了词干来更改IP,但似乎不起作用。 Here is a shortened version of the script: 这是脚本的简化版本:

from stem import Signal
from stem.control import Controller
from stem.connection import connect

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

def printIP():
    my_ip = urlopen('http://ip.42.pl/raw').read()
    print("IP -> %s" % my_ip)

#Some of my other codes

while(true):
    j+=1
    if j == 2:
        changeIP()
        j = 0
        printIP()

And it just prints my public IP again and again. 它只是一次又一次地打印我的公共IP。 It should print the same IP 2 times and then change, but it doesn't. 它应该打印相同的IP 2次,然后更改,但是没有。 My torrc config is correctly configured. 我的torrc配置已正确配置。

ControlPort = 9051
HashedControlPassword 16:AD2DD67382E391D960F7E38F49A1AAB31479A0576222AB885C3CCFD70B
cookie authentication 1

I even tried putting the hashed Control password in the control.authenticate(password='AD2DD67382E391D960F7E38F49A1AAB31479A0576222AB885C3CCFD70B') , but it still did not work, and I also want my script to not use it. 我什至尝试将散列的Control密码放入control.authenticate(password='AD2DD67382E391D960F7E38F49A1AAB31479A0576222AB885C3CCFD70B') ,但该control.authenticate(password='AD2DD67382E391D960F7E38F49A1AAB31479A0576222AB885C3CCFD70B')仍然无法正常工作,我也希望脚本不使用它。 I have been searching for this weeks now, and I found out that I can use SocksiPy module, but I can't do it. 我一直在搜索这周,发现可以使用SocksiPy模块,但是我做不到。

Please recode my script, Thank you very much. 请重新编码我的脚本,非常感谢。

I am assuming you are working on windows. 我假设您正在Windows上工作。 I am not sure if this will work on Linux. 我不确定这是否可以在Linux上运行。

I would suggest you to download a fresh copy of Tor without altering the torrc config. 我建议您下载Tor的新副本,而不更改torrc配置。 Personally, I didn't configure the hashcontrolpassword but the following python code still works for me. 就个人而言,我没有配置hashcontrolpassword但是以下python代码仍然对我hashcontrolpassword Try the step 1 listed here : https://stackoverflow.com/a/48144473/9183144 尝试在此处列出的步骤1https : //stackoverflow.com/a/48144473/9183144

Following step 1 , You should be able to see 127.0.0.1:9150 and 127.0.0.1:9151 when you run netstat -an in your terminal. 步骤1之后 ,在终端中运行netstat -an时,应该能够看到127.0.0.1:9150127.0.0.1:9151

After that copy the following code and try running it (change the directory to your Tor folder). 之后,复制以下代码并尝试运行它(将目录更改为Tor文件夹)。

# library to launch and kill Tor process
import os
import subprocess

# library for Tor connection
import socket
import socks
import http.client
import time
import requests
from stem import Signal
from stem.control import Controller

def launchTor():
    # start Tor (wait 30 sec for Tor to load)
    sproc = subprocess.Popen(r'.../Tor Browser/Browser/firefox.exe')
    time.sleep(30)
    return sproc

def killTor(sproc):
    sproc.kill()

def connectTor():
    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9150, True)
    socket.socket = socks.socksocket
    print("Connected to Tor")

def set_new_ip():
    # disable socks server and enabling again
    socks.setdefaultproxy()
    """Change IP using TOR"""
    with Controller.from_port(port=9151) as controller:
        controller.authenticate()
        socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9150, True)
        socket.socket = socks.socksocket
        controller.signal(Signal.NEWNYM)

def checkIP():
    conn = http.client.HTTPConnection("icanhazip.com")
    conn.request("GET", "/")
    time.sleep(3)
    response = conn.getresponse()
    print('current ip address :', response.read())

# Launch Tor and connect to Tor network
sproc = launchTor()
connectTor()

# Check current IP
checkIP()

# Set new IP
set_new_IP()

# Check newly assigned IP
time.sleep(10)
checkIP()

# remember to kill process 
killTor(sproc)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM