Getting a Connection error 10054 on a ssl flask server on aws-ec2

Viewed 281

I am running a very simple flask app on an aws server:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello World!'

if __name__ == '__main__':
   app.run (host='0.0.0.0', port=5000, debug=True, 
            ssl_context = ('ec2-xx-xxx-x-xx-cert.pem','ec2-xx-xxx-x-xx-key.pem'))

The curl command correctly returns

>> curl  https://ec2-xx-xxx-x-xx.compute-1.amazonaws.com:5000/

StatusCode        : 200
StatusDescription : OK
Content           : Hello World!
RawContent        : HTTP/1.0 200 OK
                    Content-Length: 12
                    Content-Type: text/html; charset=utf-8
                    Date: Thu, 15 Jul 2021 16:14:47 GMT
                    Server: Werkzeug/1.0.1 Python/3.9.5

                    Hello World!
Forms             : {}
Headers           : {[Content-Length, 12], [Content-Type, text/html; charset=utf-8], [Date, Thu, 15 Jul
                    2021 16:14:47 GMT], [Server, Werkzeug/1.0.1 Python/3.9.5]}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 12

The same get request from python, however, errors out (from the same machine where the curl command was issued earlier):

In [1]: import requests
In [2]: requests.get('https://ec2-xx-xxx-x-xx.compute-1.amazonaws.com:5000/')

---------------------------------------------------------------------------
ConnectionResetError                      Traceback (most recent call last)
~\Anaconda3\envs\scra\lib\site-packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    698             # Make the request on the httplib connection object.
--> 699             httplib_response = self._make_request(

[...]

ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

During handling of the above exception, another exception occurred:

ProtocolError                             Traceback (most recent call last)
~\Anaconda3\envs\scra\lib\site-packages\requests\adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    438             if not chunked:
--> 439                 resp = conn.urlopen(
    440                     method=request.method,

ProtocolError: ('Connection aborted.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))

During handling of the above exception, another exception occurred:

ConnectionError                           Traceback (most recent call last)
<ipython-input-10-bcf6807df4ad> in <module>
----> 1 response = requests.get('https://ec2-xx-xxx-x-xx.compute-1.amazonaws.com:5000/', verify=False)

~\Anaconda3\envs\scra\lib\site-packages\requests\api.py in get(url, params, **kwargs)
     74
     75     kwargs.setdefault('allow_redirects', True)
---> 76     return request('get', url, params=params, **kwargs)
[...]

ConnectionError: ('Connection aborted.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))

In [3]:                                                                                                           

I have checked many of the previously asked questions on this ConnectionResetError 10054 but none of the suggestions seem to apply. In particular, the introduction of headers = {'User-agent': 'your bot 0.1'} or verify=False does not change the outcome.

Any clues?

EDIT

I did try changing port number (8080), and serving the app with gunicorn, both with and without ssl, but the behavior remains unchanged.

0 Answers
Related