The following page does not open using urllib in Python:
As shown below, I've tried it in Python 2 and Python 3, and have tried using the SSL monkey-patch fix described here. Any other suggestions?
Python 2 Code and Error
import urllib
urllib.urlopen('https://efactssc-public.flcourts.org/casedocuments/2019/1464/2019-1464_brief_137452_supp20initial20brief2dmerits.pdf')
Here is the Python 2 error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\urllib.py", line 87, in urlopen
return opener.open(url)
File "C:\Python27\lib\urllib.py", line 215, in open
return getattr(self, name)(url)
File "C:\Python27\lib\urllib.py", line 445, in open_https
h.endheaders(data)
File "C:\Python27\lib\httplib.py", line 1065, in endheaders
self._send_output(message_body)
File "C:\Python27\lib\httplib.py", line 892, in _send_output
self.send(msg)
File "C:\Python27\lib\httplib.py", line 854, in send
self.connect()
File "C:\Python27\lib\httplib.py", line 1290, in connect
server_hostname=server_hostname)
File "C:\Python27\lib\ssl.py", line 369, in wrap_socket
_context=self)
File "C:\Python27\lib\ssl.py", line 599, in __init__
self.do_handshake()
File "C:\Python27\lib\ssl.py", line 828, in do_handshake
self._sslobj.do_handshake()
IOError: [Errno socket error] EOF occurred in violation of protocol (_ssl.c:727)
Python 3 Code / Error
I got a similar error running the following code in Python 3:
import urllib.request
urllib.request.urlopen('https://efactssc-public.flcourts.org/casedocuments/2019/1464/2019-1464_brief_137452_supp20initial20brief2dmerits.pdf')
Error:
Traceback (most recent call last):
File "c:\Python38\lib\urllib\request.py", line 1319, in do_open
h.request(req.get_method(), req.selector, req.data, headers,
File "c:\Python38\lib\http\client.py", line 1230, in request
self._send_request(method, url, body, headers, encode_chunked)
File "c:\Python38\lib\http\client.py", line 1276, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "c:\Python38\lib\http\client.py", line 1225, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "c:\Python38\lib\http\client.py", line 1004, in _send_output
self.send(msg)
File "c:\Python38\lib\http\client.py", line 944, in send
self.connect()
File "c:\Python38\lib\http\client.py", line 1399, in connect
self.sock = self._context.wrap_socket(self.sock,
File "c:\Python38\lib\ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "c:\Python38\lib\ssl.py", line 1040, in _create
self.do_handshake()
File "c:\Python38\lib\ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
OSError: [Errno 0] Error
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\Python38\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "c:\Python38\lib\urllib\request.py", line 525, in open
response = self._open(req, data)
File "c:\Python38\lib\urllib\request.py", line 542, in _open
result = self._call_chain(self.handle_open, protocol, protocol +
File "c:\Python38\lib\urllib\request.py", line 502, in _call_chain
result = func(*args)
File "c:\Python38\lib\urllib\request.py", line 1362, in https_open
return self.do_open(http.client.HTTPSConnection, req,
File "c:\Python38\lib\urllib\request.py", line 1322, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 0] Error>
SSL Fix Does Not Work
Another similar issue suggested monkey patching SSL with the code below, but that doesn't work in this case. The code below raises the same error (python 2):
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
import urllib
urllib.urlopen('https://efactssc-public.flcourts.org/casedocuments/2019/1464/2019-1464_brief_137452_supp20initial20brief2dmerits.pdf')