簡體   English   中英

Urllib2在python中使用Tor

[英]Urllib2 using tor in python

我正在嘗試使用Python編寫的搜尋器來搜尋網站,並希望將Tor與Python集成在一起,這意味着我想使用Tor來匿名地搜尋網站。

我在stackoverflow上找到了一些答案,但是它們都不適合我。


這是我在Python中使用Tor和襪子Urllib2中找到的第一個解決方案

import socks
import socket
import urllib2    
socks.setdefaultproxy(socks.PROXY_TYPE_HTTP, "127.0.0.1", 9050)
socket.socket = socks.socksocket
print urllib2.urlopen('http://my-ip.herokuapp.com').read()

但我得到以下錯誤

(501, 'Tor is not an HTTP Proxy')

然后, 如何將SOCKS 4/5代理與urllib2一起使用?

import socks
import socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 8080)
socket.socket = socks.socksocket
import urllib2
print urllib2.urlopen('http://www.google.com').read()

我得到以下錯誤

<urlopen error [Errno 111] Connection refused>

那么, Python urllib對TOR的投票最高

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

我的測試網址是“ http://almien.co.uk/m/tools/net/ip/ ”,以上代碼將運行2分鍾,並以以下錯誤結束

  File "/usr/lib/python2.7/dist-packages/socks.py", line 369, in connect
    self.__negotiatesocks5(destpair[0],destpair[1])
  File "/usr/lib/python2.7/dist-packages/socks.py", line 236, in __negotiatesocks5
    raise Socks5Error(ord(resp[1]),_generalerrors[ord(resp[1])])
IndexError: tuple index out of range

有人評論說最新的端口是91509050 ,所以我再次嘗試9150 ,並得到以下錯誤

urllib2.URLError: <urlopen error [Errno 111] Connection refused>

UPDATE

在我的機器上添加tor信息。

root@xxxxxxx:~# tor
Apr 22 14:14:39.818 [notice] Tor v0.2.4.20 (git-0d50b03673670de6) running on Linux with Libevent 2.0.21-stable and OpenSSL 1.0.1f.
Apr 22 14:14:39.818 [notice] Tor can't help you if you use it wrong! Learn how to be safe at https://www.torproject.org/download/download#warning
Apr 22 14:14:39.818 [notice] Read configuration file "/etc/tor/torrc".
Apr 22 14:14:39.820 [notice] Opening Socks listener on 127.0.0.1:9050
Apr 22 14:14:39.000 [notice] Parsing GEOIP IPv4 file /usr/share/tor/geoip.
Apr 22 14:14:39.000 [notice] Parsing GEOIP IPv6 file /usr/share/tor/geoip6.
Apr 22 14:14:39.000 [warn] You are running Tor as root. You don't need to, and you probably shouldn't.
Apr 22 14:14:39.000 [warn] OpenSSL version from headers does not match the version we're running with. If you get weird crashes, that might be why. (Compiled with 1000105f: OpenSSL 1.0.1e 11 Feb 2013; running with 1000106f: OpenSSL 1.0.1f 6 Jan 2014).
Apr 22 14:14:40.000 [notice] Bootstrapped 5%: Connecting to directory server.

然后啟動tor:

import socket
import urllib

import socks  # SocksiPy module
import stem.process

SOCKS_PORT = 9050

# Set socks proxy and wrap the urllib module

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

# Perform DNS resolution through the socket

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

socket.getaddrinfo = getaddrinfo

print urllib.urlopen('http://my-ip.herokuapp.com').read()

基於使用詞干的to_russia_with_love代碼。 如果您還想從python啟動tor,則應檢查一下stem

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM