LarsaSolidor
LarsaSolidor

Reputation: 133

Unable To Get HTTPS URLs (requests package)

When I try to follow the guide here: https://automatetheboringstuff.com/chapter11/ my script fails:

import requests

res = requests.get('https://automatetheboringstuff.com/files/rj.txt')
type(res)
res.raise_for_status()

requests is installed.

I am given the following error messages after a very long wait, which only appear when using HTTPS URLs; the same thing occurs on two Windows 10 64bit machines with Python 3.6.3 64bit and Python 3.6.4 64bit:

"C:\Program Files\Python36\python.exe" "C:/Users/user.name/Google Drive/Automation/RoHSWebScraper/main.py"
Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\site-packages\urllib3\contrib\pyopenssl.py", line 441, in wrap_socket
    cnx.do_handshake()
  File "C:\Program Files\Python36\lib\site-packages\OpenSSL\SSL.py", line 1716, in do_handshake
    self._raise_ssl_error(self._ssl, result)
  File "C:\Program Files\Python36\lib\site-packages\OpenSSL\SSL.py", line 1449, in _raise_ssl_error
    raise SysCallError(-1, "Unexpected EOF")
OpenSSL.SSL.SysCallError: (-1, 'Unexpected EOF')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\site-packages\urllib3\connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "C:\Program Files\Python36\lib\site-packages\urllib3\connectionpool.py", line 346, in _make_request
    self._validate_conn(conn)
  File "C:\Program Files\Python36\lib\site-packages\urllib3\connectionpool.py", line 850, in _validate_conn
    conn.connect()
  File "C:\Program Files\Python36\lib\site-packages\urllib3\connection.py", line 326, in connect
    ssl_context=context)
  File "C:\Program Files\Python36\lib\site-packages\urllib3\util\ssl_.py", line 329, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "C:\Program Files\Python36\lib\site-packages\urllib3\contrib\pyopenssl.py", line 448, in wrap_socket
    raise ssl.SSLError('bad handshake: %r' % e)
ssl.SSLError: ("bad handshake: SysCallError(-1, 'Unexpected EOF')",)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\site-packages\requests\adapters.py", line 440, in send
    timeout=timeout
  File "C:\Program Files\Python36\lib\site-packages\urllib3\connectionpool.py", line 639, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "C:\Program Files\Python36\lib\site-packages\urllib3\util\retry.py", line 388, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='automatetheboringstuff.com', port=443): Max retries exceeded with url: /files/rj.txt (Caused by SSLError(SSLError("bad handshake: SysCallError(-1, 'Unexpected EOF')",),))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/user.name/Google Drive/Automation/RoHSWebScraper/main.py", line 3, in <module>
    res = requests.get('https://automatetheboringstuff.com/files/rj.txt', verify=False)
  File "C:\Program Files\Python36\lib\site-packages\requests\api.py", line 72, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Program Files\Python36\lib\site-packages\requests\api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Program Files\Python36\lib\site-packages\requests\sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Program Files\Python36\lib\site-packages\requests\sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "C:\Program Files\Python36\lib\site-packages\requests\adapters.py", line 506, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='automatetheboringstuff.com', port=443): Max retries exceeded with url: /files/rj.txt (Caused by SSLError(SSLError("bad handshake: SysCallError(-1, 'Unexpected EOF')",),))

Process finished with exit code 1

Can anyone help me with this infuriating problem!!?

Upvotes: 2

Views: 801

Answers (2)

LarsaSolidor
LarsaSolidor

Reputation: 133

So it turns out the computers on my corporate network are using proxy servers, which was preventing my HTTP and HTTPS requests from connecting properly.

I followed the answer from Lelouchzqy here to determine what my HTTP and HTTPS proxy servers were.

I then followed the answer from Roland Smith here to tell requests which proxies to use.

Hopefully this will help someone in the future if they have the same issue!

Upvotes: 0

Ajax1234
Ajax1234

Reputation: 71471

You can try urllib:

Python2:

import urllib
data = urllib.urlopen('https://automatetheboringstuff.com/files/rj.txt').read()

Python3:

import urllib.requests
data = urllib.requests.urlopen('https://automatetheboringstuff.com/files/rj.txt').read()

Upvotes: 1

Related Questions