简体   繁体   中英

using tor with urllib python

I am trying to use TOR with Urllib as given in How to change Tor identity in Python? . However I am not able to understand where shall I find the configuration files.

can anyone give a easier solution so that I can generate new ips using TOR from python?

This works for me

import socks import socket def create_connection(address, timeout=None, source_address=None): sock = socks.socksocket() sock.connect(address) return sock

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)

# patch the socket module
socket.socket = socks.socksocket
socket.create_connection = create_connection

import urllib2

print urllib2.urlopen('http://icanhazip.com').read()

import mechanize
from mechanize import Browser

br = Browser()
print br.open('http://icanhazip.com').read()

See stem's client usage tutorials for examples of making a python socket over tor. That said, please be careful about creating new identities. Doing so puts a high load on the Tor network (for more about this see here ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM