title: How to route TCP traffic through Tor proxy?
body: <p>I would like to create a TCP connection using python library <strong>socket</strong>. This traffic should be redirected through Tor network but <strong>socks.SOCKS5Error: 0x01: General SOCKS server failure</strong> is given.</p>
  <p>The code below can connect to Tor proxy and gives a new Tor IP.</p>
  <pre><code>from stem.control import Controller
  from stem import Signal
  import socket
  import socks
  
  if __name__ == &quot;__main__&quot;:
          with Controller.from_port(port=9051) as controller:
              # Creatting TOR connection
              controller.authenticate(password='password')
              controller.signal(Signal.NEWNYM)
      
              socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, &quot;192.168.1.148&quot;, 9050)
              socket.socket = socks.socksocket
              
              # Creatting socket connection
              new_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
              host = '192.168.1.148'
              port = 50007
      
              new_socket.connect((host, port))
              new_socket.sendall('Hello world')
              print(new_socket.recv(1024))
  </code></pre>
  <p>This is the error given:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;/usr/lib/python3.10/site-packages/socks.py&quot;, line 809, in connect
      negotiate(self, dest_addr, dest_port)
    File &quot;/usr/lib/python3.10/site-packages/socks.py&quot;, line 443, in _negotiate_SOCKS5
      self.proxy_peername, self.proxy_sockname = self._SOCKS5_request(
    File &quot;/usr/lib/python3.10/site-packages/socks.py&quot;, line 533, in _SOCKS5_request
      raise SOCKS5Error(&quot;{:#04x}: {}&quot;.format(status, error))
  socks.SOCKS5Error: 0x01: General SOCKS server failure
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File &quot;/home/pi/keylogger-project/./tor_proxy_connection.py&quot;, line 74, in &lt;module&gt;
      tor.__testing_socket_availability__()
    File &quot;/home/pi/keylogger-project/./tor_proxy_connection.py&quot;, line 66, in __testing_socket_availability__
      self.socket.connect((host, port))
    File &quot;/usr/lib/python3.10/site-packages/socks.py&quot;, line 47, in wrapper
      return function(*args, **kwargs)
    File &quot;/usr/lib/python3.10/site-packages/socks.py&quot;, line 814, in connect
      raise GeneralProxyError(&quot;Socket error&quot;, error)
  socks.GeneralProxyError: Socket error: 0x01: General SOCKS server failure
  </code></pre>
  <p>Server side is a simple <strong>nc -lvp 50007</strong></p>
  <p>Regards</p>