gpt4 book ai didi

python - IP保持不变

转载 作者:行者123 更新时间:2023-11-28 16:34:50 28 4
gpt4 key购买 nike

我正在尝试通过代码连接到 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()

如有任何帮助或建议,我们将不胜感激。

最佳答案

如果您使用的是基于 unix 的操作系统,您可以将 subprocess 和 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....

关于python - IP保持不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27656298/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com