2

I'm following the tutorial To Russia With Love, and as part of this I want to change the identity everytime I run the code. I've looked at multiple questions and tutorials and from the looks of it my torrc-defaults file is correct as it has: ControlPort 9151 CookieAuthentication 1

I wanted to edit the torrc file first but if i touch it Tor won't run anymore, so i read somewhere to look at torrc instead. The torrc file alone only has:

# This file was generated by Tor; if you edit it, comments will not be preserved
# The old torrc file was renamed to torrc.orig.1 or similar, and Tor will ignore it

I don't want to set a password so my understanding from reading the stem documents is that setting the Authentication is enough. I have the following code:

import socks  # SocksiPy module
import socket
import requests

SOCKS_PORT = 9150

# Set socks proxy and wrap the urllib module

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', SOCKS_PORT)
socket.socket = socks.socksocket

def getaddrinfo(*args):
  return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]

socket.getaddrinfo = getaddrinfo

url = "http://google.com"

print requests.get(url).elapsed.total_seconds()

from stem import Signal
from stem.control import Controller

with Controller.from_port(port = 9151) as controller:
    controller.authenticate()
    controller.signal(Signal.NEWNYM)

But when I run it i get the error: socks.SOCKS5Error: 0x01: General SOCKS server failure

I'm not sure what to do, i've been reading about this for hours and haven't been able to solve the problem. Any hints would be great.

  • From the tutorial you linked: "Be aware that the following example routes all socket connections through Tor, so this'll break our ability to connect to Tor's control port." You're trying to connect to your control port over tor. – cacahuatl Nov 29 '16 at 21:21
  • Do you have a solution then? should I not use sockets? how am i supposed to go about doing this? – April Nov 29 '16 at 21:31
  • try the pycurl version of it? – cacahuatl Nov 30 '16 at 1:58
1

Try removing this. Because once the Socket is set the controller cannot change it.

from stem import Signal
from stem.control import Controller

with Controller.from_port(port = 9151) as controller:
    controller.authenticate()
    controller.signal(Signal.NEWNYM)

Or if you want to use it for changing identity every time you run the python file use it at the beginning after the imports like shown below:

import socks  # SocksiPy module
import socket
import requests

from stem import Signal
from stem.control import Controller

with Controller.from_port(port = 9151) as controller:
    controller.authenticate()
    controller.signal(Signal.NEWNYM)

SOCKS_PORT = 9150

# Set socks proxy and wrap the urllib module

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', SOCKS_PORT)
socket.socket = socks.socksocket

def getaddrinfo(*args):
  return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]

socket.getaddrinfo = getaddrinfo

url = "http://google.com"

print requests.get(url).elapsed.total_seconds()

Source: GitHub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.