I am proxying traffic over tor, so letter on when I need to call localhost port, it gives failure error.

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

def getVideoTrend(self):

    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "", 9050)
    socket.socket = socks.socksocket
    socket.create_connection = self.create_connection
    #some code

SaveEntity contains:

hostname = 'localhost'
port = 27017
self.client = MongoClient(hostname, port)

which gives

  File "radurl.py", line 110, in getVideoTrend
  File "/root/Documents/scraping/radurls/mongo_operation.py", line 14, in __init__
    self.client = MongoClient(hostname, port)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/mongo_client.py", line 377, in __init__
    raise ConnectionFailure(str(e))
pymongo.errors.ConnectionFailure: 0x01: General SOCKS server failure

I understand the reason behind this error, that mongo attempts to connect to local control port, gets proxied through a tor exit node.

Is there any solution to this prob?

Or I can terminate tor session after before Mongo operation?

2 Answers 2


While Tor has a builtin SOCKS proxy, it doesn't have common features of a SOCKS proxy like creating rules based on the target host or overriding how to resolve a host.

No you can't turn off Tor from the SOCKS proxy because Tor is the SOCKS proxy.

In your case, you'd have to create two separate connections functions, one for localhost that doesn't use a SOCKS proxy and others that use Tor for scraping websites.


You actually can use an intermediate socks agent on for example, and there your magic goes : you're passing all the localhost stuff directly, leaving the rest for Tor.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .