0

Update: I update my code by adding "host = '0.0.0.0'", but the question sill here

I create a simple flask app on AWS CE2

import json
from sutime import SUTime
from flask import Flask

app = Flask(__name__)

@app.route("/test/", methods = ["POST"])
def time_word_reg():

# this function is used to detect "time related words"(like today, tomorrow), and return the analysis result.

    raw = request.get_data()
    data = json.loads(raw)
    input_str = data['key_str']
    sutime = SUTime(mark_time_range = True)
    return json.dumps(sutime.parse(input_str), sort_keys = True, indent = 4))

if __name__ == "__main__":
    app.run(host = '0.0.0.0')
    #app.run(host = '0.0.0.0', port = 5000) # same result

About my security group setting, I open the ALL TCP port(0-65535), ALL ICMP - IP4, HTTPS(443), HTTP(80)

When I run my app, I got this:

^C(venv) ubuntu@ip-172-31-57-24:~/project/p1$ python3 app.py
 * Serving Flask app 'app'
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:5000
 * Running on http://172.31.57.24:5000
Press CTRL+C to quit

I thought my app runs successfully

However, I tried to use Postman to test it with three addresses(instance public ip, 127.0.0.1, and 172.31.57.24), but all failed with "Could not send request Error: Request timed out"

the postman error info as below:

POST http://3.84.161.242:5000/test/
Error: Invalid character in header content ["Host"]
Request Headers
Content-Type: application/json
User-Agent: PostmanRuntime/7.31.1
Accept: */*
Postman-Token: 939c1f99-8edd-40e6-9f76-ea918b72cc50
Host: 3.84.161.242:5000
Accept-Encoding: gzip, deflate, br
Connection: keep-alive

POST http://127.0.0.1:5000/test/
Error: connect ECONNREFUSED 127.0.0.1:5000
Request Headers
Content-Type: application/json
User-Agent: PostmanRuntime/7.31.1
Accept: */*
Postman-Token: e39fd4f8-7a10-42d8-a002-1437bfead04b
Host: 127.0.0.1:5000
Accept-Encoding: gzip, deflate, br
Connection: keep-alive

POST http://172.31.57.24:5000/test/
Error: connect ETIMEDOUT 172.31.57.24:5000
Request Headers
Content-Type: application/json
User-Agent: PostmanRuntime/7.31.1
Accept: */*
Postman-Token: 04c83cbc-1c8c-4efe-ab20-1fd7d999d950
Host: 172.31.57.24:5000
Accept-Encoding: gzip, deflate, br
Connection: keep-alive

What's more, I ping my instance public IP(3.84.161.242) and it works.

C:\Users\admin>ping 3.84.161.242

Pinging 3.84.161.242 with 32 bytes of data:
Reply from 3.84.161.242: bytes=32 time=188ms TTL=39
Reply from 3.84.161.242: bytes=32 time=199ms TTL=39
Reply from 3.84.161.242: bytes=32 time=187ms TTL=39
Reply from 3.84.161.242: bytes=32 time=199ms TTL=39

I don't know how the problem arises and how to fix it

P.S. please remind me of similar questions rather than closing my question. How are you sure an answer from 7 years ago can solve my problem?

Carlos
  • 167
  • 1
  • 2
  • 14

1 Answers1

0

The following answer is insecure. Never expose the debugger or development server publicly.


Change your app.run(debug = True) to:

app.run(host='0.0.0.0', debug = True)

See this question for details - https://stackoverflow.com/a/20778887/10369131.

Also, if you open all TCP ports you don't need to open 80 and 443 separately. And the only port you really need here is TCP 5000:

enter image description here

davidism
  • 121,510
  • 29
  • 395
  • 339
Anton
  • 1,793
  • 10
  • 20