How to run Python Eve application on a different port - python

I'm trying to run this application on a different port(8080). I tried putting the port parameter but it doesn't seem to work.
I'm getting a 404 page, but when i switch port to 5000, I encounter no problem.
from eve import Eve
app = Eve()
#app.route("/hello", methods=['GET'])
def hello():
return "Hello World!"
if __name__ == '__main__':
app.run(port=8080, debug=True)

Make sure that SERVER_NAME = None (which is the default) is set in your settings file.

Related

How do I run a Flask Script from Spyder IDE?

In Spyder, I wrote this code.
Why is it not showing on my browser localhost:5000?
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return "Hello World!"
if __name__ == "__main__":
app.run(debug=True)
Instead of app.run() use app.run(debug = False), for the purpose of running.
How are you running your script? Your code SHOULD work. You have a couple of options:
Navigate to the folder your script is in within a terminal/cmd and enter the following:
python3 script.py
replacing script.py with the actual name of your script
Alternatively:
python3 script.py
If it successfully boots up the flask server it will give you the address and port its running on. By default it should be port 5000 like you have said.
But the address could be:
http://127.0.0.1:5000
localhost:5000
So try both, essentially the same but your computer might be weird.
I'm not familiar with the Spyder IDE, if it has a run button to start the script then press that and you should be able to access the server via either of the above addresses.
You could also try specifying a new port, maybe 5000 is being used?
app.run(port=8080)
Try specifying the host and port like this:
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)

How to access localhost (flask app) from within docker?

On my laptop I can start simple flask app:
import os
import io
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
s = """
This is a localhost!
"""
return (s)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=3000, debug=True)
And when do curl localhost:3000 on my laptop - I can get a good response.
But when I start a docker image and put same code and start it with same version of Python - it shows as running but when I do from within a docker curl localhost:3000 - do not get any response (it just hangs and nothing happens).
How to enable localhost (routing) inside docker?
Thanks.
Change your code to
app.run(host='127.0.0.1', port=3000, debug=True)
and check if localhost is defined in /etc/hosts.

How to host publicly visible flask server on windows

I am trying to host a flask server from my windows computer so I can access it from external devices
I am using Flask/Python and have already tried a few things but can't get it to work
Tried running it on 0.0.0.0, port 33, 5000, etc. but I still can't access it this way
from flask import Flask, request, abort
app = Flask(__name__)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=33)
When I then run the file I get:
Running on http://0.0.0.0:33/ (Press CTRL+C to quit)
But it isn't even running there, nor on any other way I can access it
I expect to be able to access my flask application and send requests to it by using my public IP address
What can I do here to make it work?
You have missed an important line in your code:
After the line
app = Flask(__name__)
You have to write the line:
#app.route('/')
We use the route() decorator to tell Flask what URL should trigger our function.
And then define a function that will tell what task to be performed in the web app hosted in the respective address.
The function might look something like this:
def hello_world():
return 'Hello, World!'
The complete code then will look like:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=33)
Hope this helps.

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.

Using requests module in flask route function

Consider the following minimal working flask app:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "I am /"
#app.route("/api")
def api():
return "I am /api"
if __name__ == "__main__":
app.run()
This happily works. But when I try to make a GET request with the "requests" module from the hello route to the api route - I never get a response in the browser when trying to access http://127.0.0.1:5000/
from flask import Flask
import requests
app = Flask(__name__)
#app.route("/")
def hello():
r = requests.get("http://127.0.0.1:5000/api")
return "I am /" # This never happens :(
#app.route("/api")
def api():
return "I am /api"
if __name__ == "__main__":
app.run()
So my questions are: Why does this happen and how can I fix this?
You are running your WSGI app with the Flask test server, which by default uses a single thread to handle requests. So when your one request thread tries to call back into the same server, it is still busy trying to handle that one request.
You'll need to enable threading:
if __name__ == "__main__":
app.run(threaded=True)
or use a more advanced WSGI server; see Deployment Options.

Categories