I am trying to get the To Russia With Love tutoial from the Stem project working.
from io import StringIO
import socket
import urllib3
import time
import s
Answer posted by @J.F.Sebastian gives error on Python 3.4, and it can be fixed, but here is working code WITH PyCurl surely like in stem example.
import pycurl
import stem.process
import io
SOCKS_PORT = 9150
print("Starting Tor:\n")
def print_bootstrap_lines(line):
if "Bootstrapped " in line:
print(line)
tor_process = stem.process.launch_tor_with_config(
config = {
'SocksPort': str(SOCKS_PORT),
#'ExitNodes': '{au}',
},
init_msg_handler = print_bootstrap_lines,
)
output = io.BytesIO()
curl = pycurl.Curl()
curl.setopt( pycurl.URL, 'http://www.example.com' )
curl.setopt( pycurl.PROXY, 'localhost' )
curl.setopt( pycurl.PROXYPORT, SOCKS_PORT )
curl.setopt( pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5 )
curl.setopt( pycurl.WRITEFUNCTION, output.write)
curl.setopt(pycurl.HTTPHEADER, ['X-Requested-With: XMLHttpRequest', 'referer: http://www.meendo.net/?partner=13026'])
#curl.set_option(pycurl.USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36")
curl.setopt(pycurl.FOLLOWLOCATION, 1)
curl.perform()
print("RESULT : " + output.getvalue().decode('ascii'))
tor_process.kill() # stops tor
output.close() # free used memory