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 !
socks
(isn't defined) – Smalleyssl
. Of course you need to adjust your function to cater HTTP and HTTPS – Smalleyssl
? I tested with python2.7 and it works – Smalleyimport ssl
, and placesslsock = ssl.wrap_socket(sock)
andreturn sslsock
– Smalley