Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9191963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:48:49+00:00 2026-06-17T20:48:49+00:00

My goal is to use python’s mechanize with a tor SOCKS proxy. I am

  • 0

My goal is to use python’s mechanize with a tor SOCKS proxy.

I am not using a GUI with the following Ubuntu version:
Description: Ubuntu 12.04.1 LTS
Release: 12.04
Codename: precise

Tor is installed and is listening on port 9050 according to the nmap scan:

    Starting Nmap 5.21 ( http://nmap.org ) at 2013-01-22 00:50 UTC
    Nmap scan report for localhost (127.0.0.1)
    Host is up (0.000011s latency).
    Not shown: 996 closed ports
    PORT     STATE SERVICE
    22/tcp   open  ssh
    80/tcp   open  http
    3306/tcp open  mysql
    9050/tcp open  tor-socks

I also thought it reasonable to see if I could telnet to port 9050, which I can:

    telnet 127.0.0.1 9050
    Trying 127.0.0.1...
    Connected to 127.0.0.1.
    Escape character is '^]'.
    quit
    Connection closed by foreign host.

I had high hopes for the suggestion in this post to get tor working with urllib2:
How can I use a SOCKS 4/5 proxy with urllib2?

So I tried the following script in python:

    import socks
    import socket
    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)
    socket.socket = socks.socksocket
    import urllib2
    print urllib2.urlopen('http://icanhazip.com').read()

The script just hangs with no response.

I thought that since mechanize seems to be related to urllib2 that the following script might work:

    import socks
    import socket
    import mechanize
    from mechanize import Browser
    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)
    socket.socket = socks.socksocket
    br = Browser()
    print br.open('http://icanhazip.com').read()

I get the same result as above with the urllib2 script.

I am very new to python and networking, so I need a second opinion on how to make the python urllib2 use tor as a SOCKS on a non-GUI Ubuntu server.

I ran this script and received an expected response. I did not use the tor proxy:

    In [1]: import urllib2

    In [2]: print urllib2.urlopen('http://icanhazip.com').read()
    xxxx:xxxx:xxxx:512:13b2:ccd5:ff04:c5f4

Thanks.

I found something that works! I have no idea why it works, but it does. I found it here:
Python urllib over TOR?

    import socks
    import socket
    def create_connection(address, timeout=None, source_address=None):
        sock = socks.socksocket()
        sock.connect(address)
        return sock

    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)

    # patch the socket module
    socket.socket = socks.socksocket
    socket.create_connection = create_connection

    import urllib2

    print urllib2.urlopen('http://icanhazip.com').read()

    import mechanize
    from mechanize import Browser

    br = Browser()
    print br.open('http://icanhazip.com').read()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:48:50+00:00Added an answer on June 17, 2026 at 8:48 pm

    See end of question.

    import socks
    import socket
    def create_connection(address, timeout=None, source_address=None):
        sock = socks.socksocket()
        sock.connect(address)
        return sock
    
    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)
    
    # patch the socket module
    socket.socket = socks.socksocket
    socket.create_connection = create_connection
    
    import urllib2
    
    print urllib2.urlopen('http://icanhazip.com').read()
    
    import mechanize
    from mechanize import Browser
    
    br = Browser()
    print br.open('http://icanhazip.com').read()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Goal: I'm trying to use python interactively in my c++ code using Boost::Python library.
The project has a goal: use interface centric design. Basically we declare classes of
Goal To use a CREATE TYPE statement in HSQLDB 2.0.0 to create a user-defined
My goal is to use the result of an MD5 result to index a
My goal is to use a script that will install an executable file on
I've recently compiled Clang and LLVM on Windows. My goal is to use it
I'm trying to use custom meta boxes in wordpress. My goal at the moment
My ultimate goal is to load controls as plugins, for use as DocumentContent in
Okay, so my goal is to build a easy to use protocol for sending
I would like to use the python logging module to log all of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.