Python 3.2:urllib,SSL和TOR通过套接字:fileno函数错误

时间:2014-10-14 08:48:59

标签: python sockets ssl urllib tor

我在使用以下代码尝试使用https over socksipy进行连接时出错。

我按照这里的示例:using tor as a SOCKS5 proxy with python urllib2 or mechanize

或者这一个:Python urllib over TOR?

编辑:当我使用HTTP时,此代码实际上正在工作,而不是使用HTTPS

我从Socksipy python模块导入了袜子。

以下是代码:

import socks
import socket

#This function has no DNS resolve 
#it need to use the real ip adress to connect instead of www.google.fr
def create_connection_fixed_dns_leak(address, timeout=None, source_address=None):
    sock = socks.socksocket()
    sock.connect(address)
    return sock

# MUST BE SET BEFORE IMPORTING URLLIB
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)

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

# END MUST BE SET BEFORE IMPORTING URLLIB
import urllib.request
import urllib.error

def getURL(pUrl, headers = {}, pNbMaxRetry = 12):       
    req = urllib.request.Request( pUrl )
    for headerKey, headerValue in headers:
        req.add_header( headerKey, headerValue )

    resp = urllib.request.urlopen(req)

    return resp

通话方式:

googleAdr = "https://173.194.65.94"
pageSocket = getURL( googleAdr )
pageHtml = pageSocket.read()
pageSocket.close()
page = BeautifulSoup( pageHtml )
print(" Page calendar :" + page .prettify() )

以下是错误:

File "/home/turf/connectSafe.py", line 101, in getURL
    resp = urllib.request.urlopen(req)
  File "/usr/lib/python3.2/urllib/request.py", line 139, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.2/urllib/request.py", line 370, in open
    response = self._open(req, data)
  File "/usr/lib/python3.2/urllib/request.py", line 388, in _open
    '_open', req)
  File "/usr/lib/python3.2/urllib/request.py", line 348, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.2/urllib/request.py", line 1176, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/usr/lib/python3.2/urllib/request.py", line 1140, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "/usr/lib/python3.2/http/client.py", line 970, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python3.2/http/client.py", line 1008, in _send_request
    self.endheaders(body)
  File "/usr/lib/python3.2/http/client.py", line 966, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python3.2/http/client.py", line 811, in _send_output
    self.send(msg)
  File "/usr/lib/python3.2/http/client.py", line 749, in send
    self.connect()
  File "/usr/lib/python3.2/http/client.py", line 1111, in connect
    server_hostname=server_hostname)
  File "/usr/lib/python3.2/ssl.py", line 189, in wrap_socket
    _context=self)
  File "/usr/lib/python3.2/ssl.py", line 243, in __init__
    fileno=sock.fileno()
  TypeError: __init__() got an unexpected keyword argument 'fileno'

你有什么想法吗?甚至坏的? 非常感谢你!

0 个答案:

没有答案