Python 3.2 : urllib, SSL and TOR through socket : error with fileno function
Asked Answered
T

0

2

I have an error, when trying to connect in https over socksipy with the below code.

I followed the example here : using tor as a SOCKS5 proxy with python urllib2 or mechanize

Or this one : Python urllib over TOR?

Edit : this code is actually working when I am using HTTP, but not with HTTPS

I have imported socks from the Socksipy python module.

Here is the code :

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

Calling method :

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

Here is the error :

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'

Have you any ideas ? even bad ones ? Thank you very much !

Tonometer answered 14/10, 2014 at 8:48 Comment(8)
can you show what modules you have imported? what is that socks (isn't defined)Smalley
I have edited the code in the question. The socks library is from Socksipy module of pythonTonometer
think it works only for HTTP proxy. take a look at this, think you can try wrapping it with ssl. Of course you need to adjust your function to cater HTTP and HTTPSSmalley
Is there a way to h@ck the ssl file, module, by adding the missing function fileno ? I've tried but i don't know how with what I should fill this function.Tonometer
any luck if wrapping the connection with ssl? I tested with python2.7 and it worksSmalley
Can you explain or show me what you mean by wrapping the connection please ?Tonometer
Have you read the link I posted on previous comment? if you import ssl, and place sslsock = ssl.wrap_socket(sock) and return sslsockSmalley
I'm curently trying it, I will notice you as soon as it's finished (or I soon as i'm stuck ;) )Tonometer

© 2022 - 2024 — McMap. All rights reserved.