匿名者很好地解释了这个套接字覆盖,答案几乎是完美的,除了必须 关闭控制套接字
。由于TorCtl事件循环,它更安全,但我必须更深入地研究TorCtl代码才能理解此事件循环。
总结您的代码成为:
from TorCtl import TorCtlimport socksimport socketsocks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)import urllib2import sqlite3from BeautifulSoup import BeautifulSoup__originalSocket = socket.socketdef newId(): ''' Clean circuit switcher Restores socket to original system value. Calls TOR control socket and closes it Replaces system socket with socksified socket ''' socket.socket = __originalSocket conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051, passphrase="123") TorCtl.Connection.send_signal(conn, "NEWNYM") conn.close() socket.socket = socks.socksocketnewId()print(urllib2.urlopen("http://www.ifconfig.me/ip").read())
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)