如何通过TOR网络路由urllib请求?
答案 0 :(得分:12)
答案 1 :(得分:5)
Tor作为代理,对吗?所以问自己“如何在urllib中使用代理?”
现在,当我查看文档时,我首先看到的是
urllib.urlopen(url[, data[, proxies]])
对我来说似乎很有启发......
答案 2 :(得分:1)
我设法为洋葱网址做了urllib.request我根据这篇文章发现了一个解决方案:Python 3.2 : urllib, SSL and TOR through socket : error with fileno function
这是修改后的代码:
import socks
import socket
# This function has no DNS resolve
# it need to use the real ip adress to connect instead of www.google.com
def create_connection_fixed_dns_leak(address, timeout=None, source_address=None):
sock = socks.socksocket()
sock.connect(address)
return sock
# MUST BE SET BEFORE IMPORTING URLLIB
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)
# patch the socket module
socket.socket = socks.socksocket
socket.create_connection = create_connection_fixed_dns_leak
from urllib import request
if __name__ == "__main__":
for proxy in request.getproxies():
print(str(proxy))
url = 'http://url_of_hidden_service.onion:port'
req = request.Request(url)
res = request.urlopen(req)
print(str(res.read()))