Why does localhost:5000 not work in Flask Python? - python

I'm using flask app factory pattern like and have this helloworld.py file
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return 'This is the home page'
if __name__=="__name__":
app.run(debug=True)
Then I run the app in Terminal :
python helloworld.py
(venv) C:\Users\Jayalakshmi.S1\myproject>python helloworld.py
(venv) C:\Users\Jayalakshmi.S1\myproject>
But when I go to http://localhost:5000 it doesn't work. It says:
Can’t reach this page
Make sure the web address http://127.0.0.1:5000 is correct
What could be wrong?

The problem is that you wrote if __name__=="__name__": instead of if __name__=="__main__":.
Since that will never be true, your app.run never happens. That's why when you run the script, it just returns immediately, instead of printing out something like * Running on http://127.0.0.1:5000/ and then waiting.
You also almost always want to run Flask this way:
set FLASK_APP=helloworld.py
flask run
… instead of:
python helloworld.py

Your if condition is wrong. You should mention the main module which you're running...
if __name__=="__main__":
app.run(debug=True)

Related

Python Flask app route is not working well

I tried to find a solution for my problem in other questions but I couldn't.
I downloaded the python flask and made my first flask app and it ran fine.
Here is the code:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def index():
return "Hello, world!"
When I ran my second file where I had added an app.route ("/ david") and followed the same procedure again, refreshed it and nothing changed.
That is to say, I was going to / david and I get an URL error
Here is my second file
from flask import Flask
app = Flask(__name__)
#app.route("/")
def index():
return "Hello, world!"
#app.route("/david")
def david():
return "Hello, David!"
I tried the same with other files which have some added routes and the result is the same as the first file
Thanks for your answers, I hope to solve my problem.
You did not run the app. What you did is just create a structure for flask, but did not start the server.
Just add:
app.run()
To the bottom of the file and it will work. It will with start the flask server at http://localhost:5000.
By default, flask runs on port 5000.
It can be changed by:
app.run(host="0.0.0.0", port=xxxx)
0.0.0.0 means it accepts request from anywhere on the port specified.
Make sure you have all the permissions and nothing else is running if you want it to run on port 80.
Hope this helps. Good luck.
I had the same issue. Try first by restarting your IDE; this worked for me. If that doesn't work, try clearing your ports for Windows:
Open Task manager
Click on the “Processe” tab
Enable the "PID" column: View -> Select Columns -> Check the box for PID
Find the PID (in your case, 5000 - flask default port) and click “END PROCESS"

Flaskserver running but not routing?

Hey I ran the flask basic code as follows -
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello, World!'
Then I ran the server as stated in docs and it ran fine.
But now when i got 127.0.0.1:5000/ nothing happens. The browser keeps circling as if refreshing the page but doesn't route.
Its my first python/flask code so I am not sure what I am doing wrong.
EDIT- By docs I mean quickstart documentation of flask. I know its fine cz i get this -
Serving Flask app "hello"
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Just added app.run() to the same code and executed which is working fine. Can you try it?
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello, World!'
app.run()
I'm assuming that you're using this Quickstart - A Minimal Application.
That said let's make some points clear:
When you said
I know its fine cz i get this - Serving Flask app "hello"
You do not use the extension .py, which can cause some problems if you have another file with the same name in this directory. So make sure your FLASK_APP variable is correct.
This should work for you, but if the problem persist enable the Debug Mode adding
FLASK_ENV=development to your environment variable and see which error appears for you.
Hoping this solve your problem.

Python script with flask server takes too long to exit on Windows

I'm new to Python.
As a part of a project I'm trying to deploy a Flask server locally, through the Windows command line.
My Python version is 3.6.0.
The code:
from flask import Flask
app = Flask(__name__)
#app.route('/') def index():
return '<h1>Hello World!</h1>'
if __name__ == "__main__":
app.run()
The problem:
It's about killing the script as it runs. Launching this script with python deploy.py and hitting CTRL+C shuts it off.
BUT - if I hit access that '/' route via the browser once or more, and a moment later try to kill the script in the same manner, then it would take about 10 seconds of nothing until it responds and is finally killed.
Why is this happening? How can I shut the server off immediately each time for continuous and quick development?
Thanks!!
Well if your goal is continuous and quick development, then you can change flask's configuration.
Best solution for your problem would be setting the DEBUG setting to True. If DEBUG is set to True, then flask will automatically reload the server on code changes.
There are a few ways to do this but the easiest one(because you said you are a beginner) is to pass the debug argument to app.run()
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return '<h1>Hello World!</h1>'
if __name__ == "__main__":
app.run(debug=True)

How to run python script via url using Flask

What I want to do is run a python script(has nothing to do with html) via a url and display the result in the browser. The output can be as simple as an addition answer.
Whatever research I am doing shows I need Apache, cgi, Flask etc.
What I wanted to know is, can I do the aforementioned without using Apache or anything like that. Can I only use Flask to do the above task?
You don't need Apache
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World"
if __name__ == "__main__":
app.run()
Run with:
python my_file.py
And check localhost:5000, you should see "Hello World"

flask 'hello world' not working

I copy pasted the flask's 'hello world' app from their website and am trying to run it. I get an error message in Chrome saying
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
Here is the 'hello world' app straight from flasks website
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.debug = True
app.run()
What I have tried:
-temporarily disabling Avast!
-disabling windows firewall
-ensuring that the flask module is installed
This was working a couple days ago actually...
I don't know why but when I change
app.run()
to
app.run(port=4996)
it starts working. No idea why the default port is throwing an error. Oh well.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return 'Hello World'
if __name__ == '__name__':
app.run()
app.run(port=5000)
For Windows machines you can use the command in cmd:
set FLASK_APP=python_file.py
flask run
Some other process is running on port 5000. It may be you still have an old Flask process running, with broken code. Or a different web server altogether is running on that port. Shut down that process, or run on a different port.
You can switch to using a different port with the port argument to app.run():
app.run(port=8080)
If you can't figure out what process is still bound to port 5000, use the Windows Resource Monitor or run netstat -a -b from a command line. See How can you find out which process is listening on a port on Windows?
I think you are trying to copy the route generated through your flask program in cmd by pressing ctrl+c which quits your running flask program . i was also doing the same.just try to type the route generated by your flask program on your browser . it will definitely resolve your problem.
Where your python file store is, use cmd and then go on your file store directory, then
set FLASK_APP=filename.py
After this your flask run cmd will work.
from flask import Flask
app = Flask(__name__) # creating app
#app.route('/', methods['GET']) #routing it to the home page
def home(): #function
return "hello world"
app.run(port=5000, debug=true) #function call by the app
Add port and use methods whatever your need is USE GET in your case and try to remove your cache and run the this code it will definitely work.

Categories