2

I'm trying to route requests in a python script through tor. Here's the code:

#!/usr/bin/env python3

import socket
import socks
from urllib import request

socks.set_default_proxy(socks.SOCKS5, '127.0.0.1', 9050)
socket.socket = socks.socksocket
ip = request.urlopen('https://api.ipify.org/').read()
print(ip)

When I try to run it as a user (just "./script.py"), it crashes with the following error:

urllib.error.URLError: <urlopen error Socket error: 0x01: General SOCKS server failure>

But if I run the script with sudo ("sudo ./script.py"), it works as expected and prints a tor IP. How can I get it to work without sudo?

Edit 1: I think tor installation is ok, because it works fine with other languages (for example, I can perform requests from a Go script. Also, I can get my python script to work with tor when passing proxies dict to requests.get() (as suggested in a comment below). This solution is acceptable, but I am still wondering, what's wrong with my script.

Edit 2: I'm running Linux Mint 18.3 64-bit. Python and Python3 are pre-installed. Tor was installed via repository (sudo apt-get install tor). I tried installing PySocks globally (sudo pip3 install PySocks) and only for current user (pip3 install --user PySocks).

Igor
  • 600
  • 6
  • 13
  • 1
    Are you doing anything else with sockets? Or is the exact program you posted generating that error *every* time you run it? In any case, I'd advise to avoid overriding `socket.socket` and see this answer: https://stackoverflow.com/questions/43682909/connect-to-onion-websites-on-tor-using-python/43823166#43823166 – drew010 Jan 19 '18 at 03:19
  • @drew010, It is the exact script that generating error every time. And it's quite strange, because a lot of sources around the internet say it should work. But the answer you gave link to works, so thank you! – Igor Jan 19 '18 at 11:58
  • The only reason I can think for Mint that it might not have been working without sudo is due to issues with AppArmor if it's running. It's still odd but monkey patching socks.socket will most likely cause problems later if you ever want your program to do other socket access besides Tor (e.g. control port, talk to some other service not over Tor or a different proxy etc). – drew010 Jan 19 '18 at 16:29
  • Have you tried `sudo apt-get install python3-pysocks`? That's what I did (though I'm using Debian, not Mint) and it worked. – Dmitry Grigoryev Jan 24 '18 at 15:54

2 Answers2

1

Did you try looking at : https://tor.stackexchange.com/questions/7101/general-socks-server-failure-while-using-tor-proxy? This seems to be what you are looking for, and they indicate this can be resolved using an intermediate connection on 127.0.0.2. Take a look!

Joe Moisan
  • 23
  • 7
  • Yes, I looked at it, and it may work, but I'd like to solve the problem, not just find a temporary workaround. – Igor Jan 18 '18 at 21:03
1

I have tried your code on my machine (after installing the right packages) and it works as it should. Something should be wrong with the way Tor is installed/run in your system. Feel free to add details about your installation to your question and I'll try to take a look at it.

Dmitry Grigoryev
  • 3,156
  • 1
  • 25
  • 53
  • It says "no module named urllib2". I found [this question](https://stackoverflow.com/questions/2792650/import-error-no-module-name-urllib2), it seems like there's no urllib2 in Python3 – Igor Jan 18 '18 at 22:07
  • Right, I was using Python 2 by mistake. Sorry. – Dmitry Grigoryev Jan 18 '18 at 22:18