I have Tor running on a remote server (Ubuntu) on port 9150 with the control port on 9151. I've confirmed both are running via netstat -ant.
Here is my code which is eliciting the SOCKS5Error: 0x01: General SOCKS server failure
error.
import socks
import socket
socks.set_default_proxy(socks.SOCKS5, server_ip, 9150)
socket.socket = socks.socksocket
I can make requests from any library and successfully get responses back with a tor ip address.
However the following is what causes the error:
from stem import Signal
from stem.control import Controller
with Controller.from_port(port = 9151) as controller:
controller.authenticate(password)
controller.signal(Signal.NEWNYM)
If I run the above without setting up the proxy using socks (first snippet), I can issue signals with no trouble.
I saw this error happens when you try to open a new connection to port 9051, while an old connection is still open. I solved the problem in this way.
From your personal password you can create a hash with the command
tor --hash password My_Personal_Password
and the resulting string has the format
16:CA850F5648.........
This must be inserted in the file /etc/tor/torrc
under:
HashedControlPassword 16: CA850F5648 .........
You can't open a new controller once you've connected to Tor. Try opening a controller right at the top of your script. Then both the Tor connection and signaller use the same controller object.
This seems to work with Python3:
Your first snippet is proxying traffic over tor, but Stem's Controller.from_port() method uses the socket module too. As such Stem attempts to connect to your local control port, gets proxied through a tor exit node, then can't connect.