-1

I am new to python and trying to create a simple API. below is the code for the same.

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"


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

I have saved this as code as "hello-world.py". When I am trying to run this file in command prompt using python command

python hello-world.py

Command executed in command prompt

Here I have no error but web page is not getting displayed. Below is the error on web page.

http://localhost:5000/

This site can’t be reached localhost refused to connect. Search Google for localhost 5000 ERR_CONNECTION_REFUSED

1st Issue - Even after running the code properly, why the web page is not displayed

Now, I enter python command in my command prompt and then I try running file again python hello-world.py. Here I am getting the below error

>>>  File "<stdin>", line 1
    python hello-world.py
               ^
SyntaxError: invalid syntax

2nd Issue - Why I am getting an error while trying run the hello-world.py

Please guide me on how to resolve this issue.

Jared Smith
  • 19,721
  • 5
  • 45
  • 83
kbsudhir
  • 415
  • 1
  • 6
  • 15
  • 3
    OS-level firewall? And you are getting `SyntaxError` because you are not supposed to execute shell commands in the Python REPL – DeepSpace Jun 28 '18 at 18:29

2 Answers2

0

1st issue - firewall or some other services may be blocking your port.

2nd Issue - you are running in interactive terminal. Please execute the command in command prompt.

Arjun k
  • 29
  • 7
0

From what you're describing, I think you're trying to use the Flask Quickstart. If so, have you done the following command in your command prompt enviroment?

C:\path\to\app>set FLASK_APP=hello.py

At which point if you run your code and it's correctly structured you should see

Running on http://127.0.0.1:5000/

Because you don't see the "Running" on your prompt I would assume this hasn't been done. If this doesn't work, I would just double check that Flask is installed and that Python sees it, and that you're following the steps in the tutorial correctly.

Edit: Also, I would recommend removing the hyphen in hello-world.py. It may help you avoid errors here and elsewhere in python coding. See the responses to this question.

StuJ
  • 26
  • 7