How to route urllib requests through the TOR network? [duplicate]
Asked Answered
G

3

13

How to route urllib requests through the TOR network?

Grassquit answered 2/4, 2009 at 19:57 Comment(3)
What have you tried? TOR should be largely transparent to you. Try using urrlib2; post your code and error messages.Cover
I have no code or error messages - I am asking how to do it.Grassquit
@Lobe: Tor anonymizes your requests -- it conceals you from the web site. It doesn't do anything to the basic method of making HTTP requests -- that's why there's no documentation. Nothing changes except no you're anonymous.Cover
N
12

This works for me (using urllib2, haven't tried urllib):

def req(url):
    proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"})
    opener = urllib2.build_opener(proxy_support) 
    opener.addheaders = [('User-agent', 'Mozilla/5.0')]
    return opener.open(url).read()

print req('http://google.com')
Naranjo answered 24/7, 2010 at 1:4 Comment(1)
S
6

Tor works as a proxy, right? So ask yourself "How do I use proxies in urllib?"

Now, when I look at the docs, first thing I see is

urllib.urlopen(url[, data[, proxies]])

which seems pretty suggestive to me...

Switzer answered 2/4, 2009 at 20:15 Comment(5)
I tried that, didnt work: >>> urllib.urlopen('google.com',proxies={'http':'127.0.0.1:9051'}) <addinfourl at 61446104 whose fp = <socket._fileobject object at 0x03A82C30>> >>> _.read()Grassquit
But Tor works for you from other apps? Can you use any proxies with liburl?Switzer
Doesn't tor works on 127.0.0.1:8118?, I think 9051 is the control port.Naranjo
No, TOR runs on 9050 by default, 9051 is control, and 8118 is for some extra privacy-helping proxies (edit: privoxy I think).Anglicist
Oh yea, privoxy and polipo uses 8118 as default port.Naranjo
U
2

I managed to do an urlib.request for an onion url I found a solution based on this post: Python 3.2 : urllib, SSL and TOR through socket : error with fileno function

here is the modified code:

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()))
Unfavorable answered 12/12, 2014 at 5:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.