我正在尝试通过代码连接到Tor并更改我的身份。到目前为止我得到的结果是我成功连接但不能改变我的身份。这是我的代码:
import socket
import socks
import httplib
def connectTor():
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5,"127.0.0.1",9150,True)
socket.socket = socks.socksocket
def newIdentity():
socks.setdefaultproxy()
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(("127.0.0.1",9151))
s.send("AUTHENTICATE\r\n")
response = s.recv(128)
if response.startswith("250"):
s.send("SIGNAL NEWNYM\r\n")
s.close()
connectTor()
def showIP():
conn = httplib.HTTPConnection("my-ip.herokuapp.com")
conn.request("GET","/")
response = conn.getresponse()
print (response.read())
def main():
connectTor()
print("Connected to Tor")
showIP()
print("Hew Id is")
newIdentity()
showIP()
main()
感谢任何帮助或建议。
答案 0 :(得分:3)
如果您使用的是基于unix的操作系统,则可以使用子进程和killall与HUP一起创建新标识:
url = 'http://my-ip.heroku.com'
import socks
import socket
socks.set_default_proxy(socks.SOCKS5, "localhost", 9050)
socket.socket = socks.socksocket
import requests
res = requests.get(url)
response = res.content
print(response)
from subprocess import check_call
check_call(["killall","-HUP", "tor"])
res = requests.get(url)
response = res.content
print(response)
In [2]: paste
url = 'http://my-ip.heroku.com'
import socks
import socket
socks.set_default_proxy(socks.SOCKS5, "localhost", 9050)
socket.socket = socks.socksocket
import requests
res = requests.get(url)
response = res.content
print(response)
from subprocess import check_call
check_call(["killall","-HUP", "tor"])
res = requests.get(url)
response = res.content
print(response)
## -- End pasted text --
94.242....
95.130....