Tor doesnt work with urllib2

2024/10/6 1:37:18

I am trying to use tor for anonymous access through privoxy as a proxy using urllib2.

System info: Ubuntu 14.04, recently upgraded from 13.10 through dist-upgrade.

This is a piece of code I am using for test purposes:

import urllib2def 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('https://check.torproject.org')

The above outputs a page with a sorry, but you don't use Tor message.

As for my configurations:

/etc/tor/torrc

ControlPort 9051
## If you enable the controlport, be sure to enable one of these
## authentication methods, to prevent attackers from accessing it.
#HashedControlPassword 16:872860B76453A77D60CA2BB8C1A7042072093276A3D701AD684053EC4C
HashedControlPassword 16:B3B0BA631D41D6FE601488FC7A9A2E80AB7815580C1C78F2865F30F63B

/etc/privoxy/config

forward-socks5 / localhost:9050 .

$ sudo netstat -ntap | grep tor outputs:

tcp        0      0 127.0.0.1:9050          0.0.0.0:*               LISTEN      1045/tor        
tcp        0      0 127.0.0.1:9051          0.0.0.0:*               LISTEN      1045/tor        
tcp        0      0 10.0.0.94:56736         85.17.190.83:9002       ESTABLISHED 1045/tor        
tcp        0      0 10.0.0.94:60558         50.7.110.118:9001       ESTABLISHED 1045/tor        
tcp        0      0 10.0.0.94:43206         62.210.236.135:443      ESTABLISHED 1045/tor   

$ sudo netstat -ntap | grep privoxy outputs:

tcp        0      0 127.0.0.1:8118          0.0.0.0:*               LISTEN      887/privoxy 

And yet, all requests are received at the server with my actual

Edit:

Below code, which is using the requests library, gives the same output:

import requestsdef req(url):proxies = {"http": "http://127.0.0.1:8118"}return requests.get(url, proxies=proxies).textprint req('https://check.torproject.org')

Tor log doesn't indicate any problem:

Apr 22 15:44:34.000 [notice] Bootstrapped 100%: Done.
Apr 22 15:45:03.000 [notice] Catching signal TERM, exiting cleanly.
Apr 22 16:00:57.000 [notice] Tor 0.2.4.20 (git-0d50b03673670de6) opening log file.
Apr 22 16:00:57.000 [notice] Parsing GEOIP IPv4 file /usr/share/tor/geoip.
Apr 22 16:00:57.000 [notice] Parsing GEOIP IPv6 file /usr/share/tor/geoip6.
Apr 22 16:00:57.000 [warn] OpenSSL version from headers does not match the version we're running with. If you get weird crashes, that might be why. (Compiled with 1000105f: OpenSSL 1.0.1e 11 Feb 2013; running wi$
Apr 22 16:00:58.000 [notice] We now have enough directory information to build circuits.
Apr 22 16:00:58.000 [notice] Bootstrapped 80%: Connecting to the Tor network.
Apr 22 16:00:59.000 [notice] Bootstrapped 85%: Finishing handshake with first hop.
Apr 22 16:00:59.000 [notice] Bootstrapped 90%: Establishing a Tor circuit.
Apr 22 16:01:00.000 [notice] Tor has successfully opened a circuit. Looks like client functionality is working.
Apr 22 16:01:00.000 [notice] Bootstrapped 100%: Done.
Answer

Maybe this answer is related.

In a nutshell, you're using 127.0.0.1:8118 as a proxy url, and http as your protocol, but the protocol should be https instead.

https://en.xdnf.cn/q/70424.html

Related Q&A

Python Selenium Chrome disable prompt for Trying to download multiple files

I am currently running a Python automator which needs to download multiple files within the same session using Selenium Chromedriver.The problem is that when the browser attempts to download the second…

Label outliers in a boxplot - Python

I am analysing extreme weather events. My Dataframe is called df and looks like this:| Date | Qm | |------------|--------------| | 1993-01-…

Matplotlib how to draw vertical line between two Y points

I have 2 y points for each x points. I can draw the plot with this code:import matplotlib.pyplot as pltx = [0, 2, 4, 6] y = [(1, 5), (1, 3), (2, 4), (2, 7)]plt.plot(x, [i for (i,j) in y], rs, markersiz…

Cythonizing fails because of unknown type name uint64_t

This may be a newbie problem. I cant cythonize a simple helloworld.pyx tutorial script while the exact same code works on linux:print("hello world")Here is the setup.py script: from distutils…

How to save changes in read-only Jupyter Notebook

I have opened a python Jupyter notebook but did not notice that it was in read-only, Not Trusted mode. How to save my changes now?Things that I have tried and did not help:File -> Make a Copy File …

How can I invoke an SQLAlchemy query with limit of 1?

I have code like this:thing = thing.query.filter_by(id=thing_id).limit(1).all()[0]all()[0] feels a bit messy and redundant in the limit(1) case. Is there a more terse (and/or otherwise optimal) way to …

How to correctly create Python feature branch releases in development? (pip and PEP-440)

I develop a Python library using Gitflow development principle and have a CI stage for unit testing and package upload to a (private) PyPI. I want to consume the uploaded package for testing purposes b…

How do I replace NA with NaN in a Pandas DataFrame?

Some columns in my DataFrame have instances of <NA> which are of type pandas._libs.missing.NAType. Id like to replace them with NaN using np.nan. I have seen questions where the instances of <…

concatenation of two or more base64 strings in python

Im tring to concatenate two strings encoded to base64 but it doesnt really work, just prints the first string in concatanation:q = base64.b64encode("StringA") print q # prints an encoded stri…

How to find shared library used by a python module?

I am debugging a python program based on pygtk and I want to make sure that the program is using the right shared library.pygtk is a GTK+ wrapper for python. I have already compiled GTK+ using jhbuild …