سؤال

I am trying to use stem to have a small script run through Tor. I can't seem to get stem to work. Here is my code:

import urllib.request
import re
from stem.connection import connect_port
from stem import Signal
from stem.control import Controller

controller = connect_port(port=9151)

def change():

    controller.authenticate()
    controller.signal(Signal.NEWNYM)

def getIp():
    print (urllib.request.urlopen("http://my-ip.heroku.com").read(30).decode('utf-8'))

def connectTor():
    controller = connect_port(port=9151)
    controller.connect()
    getIp()
    if not controller:
        sys.exit(1)  
        print("nope")

def disconnect():
    controller.close()

if __name__ == '__main__':
    connectTor()
    getIP()
    change()
    getIp()
    disconnect()

Basically, all of the IPs that display are the same, when in theory, they should all be different. What can I do to make this code work?

هل كانت مفيدة؟

المحلول

To use Tor you need to direct traffic through its SocksPort (Tor acts as a local socks proxy). In your code above you don't have anything attempting to make urllib go through Tor.

For examples see Stem's client usage tutorials. I'm not sure offhand if SocksiPy or PycURL have Python 3.x counterparts. If not then you'll need to find an alternative.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top