-1

Issue: Trying to access my webservice from Internet, call reaches the service but service returns 404, even if I try my local IP adress same issue when I use 127.0.0.1:5000 it works

from flask import Flask, jsonify

app = Flask(__name__)
courses= [{'name':"Dirty Corporate Dairy",
           'course_id': "0",
           'Description': "Story about ",
           'Price':"120"},
          {'name': "Dirty Corporate Dairy 2",
           'course_id': "1",
           'Description': "Story about ",
           'Price':"1200"},
          {'name': "Dirty Corporate Dairy3",
           'course_id': "2",
           'Description': "Story about ",
           'Price':"12000"}
          ]


if __name__ == '__main__':
    from waitress import serve
    app.run(host='192.168.0.113',debug=True,port=80)

@app.route('/')
def index():
    return "Welcome To the Course API"

@app.route("/courses", methods=['Get'])
def get():
    return jsonify({'Courses': courses})

@app.route("/courses/<int:course_id>", methods=['Get'])
def get_course(course_id):
    return jsonify({'Courses': courses[course_id]})

@app.route("/courses", methods=['Post'])
def create():
    course={'name': "Dirty Corporate Dairy7",
           'course_id': "6",
           'Description': "Story about",
           'Price':"120000000"}
    courses.append(course)
    return jsonify({'Created': course})

@app.route("/courses/<int:course_id>", methods=['Put'])
def update(course_id):
    courses[course_id]["Description"] = "New Updated Description"
    return jsonify({'Courses': courses[course_id]})

if __name__ == "__main__":
    app.run(debug=True)

Log:

  • Serving Flask app "main" (lazy loading)

  • Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.

  • Debug mode: on

  • Restarting with stat

  • Debugger is active!

  • Debugger PIN: 186-690-898

  • Running on http://192.168.0.113:80/ (Press CTRL+C to quit)

    192.168.0.113 - - [05/Apr/2021 18:44:32] "←[33mGET / HTTP/1.1←[0m" 404 - 192.168.0.113 - - [05/Apr/2021 18:44:40] "←[33mGET / HTTP/1.1←[0m" 404 - 157.48.141.253 - - [05/Apr/2021 18:47:41] "←[33mGET /courses HTTP/1.1←[0m" 404 -

  • Flask's built-in server is not suitable for production (hosting your application outside of your local development environment). Please see [this SO thread](https://stackoverflow.com/questions/12269537/is-the-server-bundled-with-flask-safe-to-use-in-production) to read more. – superhawk610 Apr 05 '21 at 19:19

2 Answers2

0

By default and for security purpose, Flask only listen requests from local origin (127.0.0.1) (its a security feature). To make the service available from other ip, you need to expose it with the --host option, like here.

Romain
  • 334
  • 4
  • 10
0
  1. 192.168.0.113 is an ip address on your local (home) network (see this documentation) and by default your home network is not accessible to the internet. This means that your program is only accessible within your home network. Someone outside of your home network cannot get to it.
  2. To be able to access your program via the internet, you need a service that will essentially 'put this local ip' on the web i.e. something that will provide a tunnel to your machine. One service that does this and that is widely used is https://ngrok.io. Basically, the service will give you a public ip that is mapped to your local ip
NoCommandLine
  • 5,044
  • 2
  • 4
  • 15