我的目标是使用python的机械化和tor SOCKS代理。
我没有使用具有以下Ubuntu版本的GUI: 描述:Ubuntu 12.04.1 LTS 发布:12.04 代号:精确
根据nmap扫描安装了Tor并正在侦听端口9050:
Starting Nmap 5.21 ( http://nmap.org ) at 2013-01-22 00:50 UTC
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000011s latency).
Not shown: 996 closed ports
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
3306/tcp open mysql
9050/tcp open tor-socks
我还认为我是否可以telnet到端口9050是合理的,我可以:
telnet 127.0.0.1 9050
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
quit
Connection closed by foreign host.
我对这篇文章中有关使用urllib2工作的建议寄予厚望: How can I use a SOCKS 4/5 proxy with urllib2?
所以我在python中尝试了以下脚本:
import socks
import socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)
socket.socket = socks.socksocket
import urllib2
print urllib2.urlopen('http://icanhazip.com').read()
脚本挂起时没有响应。
我认为,由于机械化似乎与urllib2有关,因此以下脚本可能有效:
import socks
import socket
import mechanize
from mechanize import Browser
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)
socket.socket = socks.socksocket
br = Browser()
print br.open('http://icanhazip.com').read()
我使用urllib2脚本得到与上面相同的结果。
我是python和网络的新手,所以我需要关于如何使python urllib2在非GUI Ubuntu服务器上使用tor作为SOCKS的第二意见。
我运行了这个脚本并收到了预期的响应。我没有使用tor代理:
In [1]: import urllib2
In [2]: print urllib2.urlopen('http://icanhazip.com').read()
xxxx:xxxx:xxxx:512:13b2:ccd5:ff04:c5f4
感谢。
我找到了有用的东西!我不知道它为什么会起作用,但确实如此。我在这里找到了: Python urllib over 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
print urllib2.urlopen('http://icanhazip.com').read()
import mechanize
from mechanize import Browser
br = Browser()
print br.open('http://icanhazip.com').read()
答案 0 :(得分:8)
见问题结尾。
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()
答案 1 :(得分:1)
上述解决方案对我没有用。我在Ubuntu 14.04上。每当我尝试运行上面的脚本时,它都会抛出以下错误。
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 404, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 422, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1214, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1184, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error ((1, 'general SOCKS server failure'),)>
使用nmap命令检查tor是否正在运行。
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00026s latency).
Not shown: 993 closed ports
PORT STATE SERVICE
22/tcp open ssh
139/tcp open netbios-ssn
445/tcp open microsoft-ds
631/tcp open ipp
902/tcp open iss-realsecure
3306/tcp open mysql
9050/tcp open tor-socks
安装Vidalia解决了这个问题。显然,socks代理不允许连接通过它。希望这可以帮助遇到同样问题的人。