IP保持不变 [英] IP remains unchanged

查看:171
本文介绍了IP保持不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过代码连接到Tor并更改我的身份。到目前为止我得到的结果是我成功连接但不能改变我的身份。这是我的代码:

I am trying to connect to Tor by code and change my identity. The results that I have gotten so far are that I connect successfully but can't change my identity. Here is my code :

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() 

任何帮助或建议表示赞赏。

Any help or advice is appreciated.

推荐答案

如果您使用的是基于unix的操作系统,则可以使用subprocess和killall与 HUP 创建新身份:

If you are using a unix based OS you can use subprocess and killall with HUP to create a new identity:

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....

这篇关于IP保持不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆