This question already has answers here:
Are a WSGI server and HTTP server required to serve a Flask app?
(3 answers)
How do I get Flask to run on port 80?
(15 answers)
Closed 4 years ago.
I tried to launche Flask in hosting using:
if __name__ == "__main__":
app.run('0.0.0.0', 8000)
But hoster has 8080 port as closed, it is possible to run Flask with default port 80?
I tried it gives me this error:
Use a production WSGI server instead.
* Debug mode: off
Traceback (most recent call last):
File "__init__.py", line 461, in <module>
app.run('0.0.0.0', 80)
File "/home/o/oliwin4/project/public_html/myenv/local/lib/python2.7/site-packages/flask/app.py", line 943, in run
run_simple(host, port, self, **options)
File "/home/o/oliwin4/project/public_html/myenv/local/lib/python2.7/site-packages/werkzeug/serving.py", line 814, in run_simple
inner()
File "/home/o/oliwin4/project/public_html/myenv/local/lib/python2.7/site-packages/werkzeug/serving.py", line 774, in inner
fd=fd)
File "/home/o/oliwin4/project/public_html/myenv/local/lib/python2.7/site-packages/werkzeug/serving.py", line 660, in make_server
passthrough_errors, ssl_context, fd=fd)
File "/home/o/oliwin4/project/public_html/myenv/local/lib/python2.7/site-packages/werkzeug/serving.py", line 577, in __init__
self.address_family), handler)
File "/usr/lib/python2.7/SocketServer.py", line 419, in __init__
self.server_bind()
File "/usr/lib/python2.7/BaseHTTPServer.py", line 108, in server_bind
SocketServer.TCPServer.server_bind(self)
File "/usr/lib/python2.7/SocketServer.py", line 430, in server_bind
self.socket.bind(self.server_address)
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 1] Operation not permitted
So, hoster said tht 80 port is open.
The Flask documentation states that:
While lightweight and easy to use, Flask’s built-in server is not
suitable for production as it doesn’t scale well. Some of the options
available for properly running Flask in production are documented
here.
The documentation also states several methods of using WSGI servers to deploy your web application. WSGI (Web Server Gateway Interface) is a standard that forwards requests from web servers to web applications written in Python. You'll want to use a WSGI server to run your website on your host, since it sounds like you're trying to deploy a Flask app to a production setting.
To answer your original question, the Flask library intentionally throws an error when you try to run it on port 80, because they specifically say that you should not use Flask's built-in server for production use. That's this part of your stacktrace:
Use a production WSGI server instead.
* Debug mode: off
and
socket.error: [Errno 1] Operation not permitted
For the lazy, or if the above link goes dead, here is an example using Gunicorn (copied directly from the above documentation link)
Gunicorn
Gunicorn ‘Green Unicorn’ is a WSGI HTTP Server for UNIX. It’s a
pre-fork worker model ported from Ruby’s Unicorn project. It supports
both eventlet and greenlet. Running a Flask application on this server
is quite simple:
gunicorn myproject:app
Gunicorn provides many command-line options – see gunicorn -h. For
example, to run a Flask application with 4 worker processes (-w 4)
binding to localhost port 4000 (-b 127.0.0.1:4000):
gunicorn -w 4 -b 127.0.0.1:4000 myproject:app
Related
I have a python web application that is using FastAPI. It works locally, but when I deploy it to a free linux Azure App Service (using GitHub Actions) and try to load the site it says "Internal Server Error". When I pull up the application logs I see the following error message
2023-02-06T23:44:30.765055894Z [2023-02-06 23:44:30 +0000] [90] [ERROR] Error handling request /
2023-02-06T23:44:30.765101490Z Traceback (most recent call last):
2023-02-06T23:44:30.765109589Z File "/opt/python/3.10.9/lib/python3.10/site-packages/gunicorn/workers/sync.py", line 136, in handle
2023-02-06T23:44:30.765116389Z self.handle_request(listener, req, client, addr)
2023-02-06T23:44:30.765122088Z File "/opt/python/3.10.9/lib/python3.10/site-packages/gunicorn/workers/sync.py", line 179, in handle_request
2023-02-06T23:44:30.765128688Z respiter = self.wsgi(environ, resp.start_response)
2023-02-06T23:44:30.765134688Z TypeError: FastAPI.__call__() missing 1 required positional argument: 'send'
Any suggestions on how to fix this issue?
I was able to resolve this issue by adding the following custom startup command in the Azure App Service Configuration General Settings
python -m uvicorn app:app --host 0.0.0.0
As h4z3 pointed out, gunicorn is wsgi and fastapi is asgi so I had to change the startup command to use uvicorn. Additional details can be found in the Azure docs here: https://learn.microsoft.com/en-us/azure/app-service/configure-language-python#example-startup-commands
I deployed a web app on GPU enabled ACI (Azure Container Instance) using Gunicorn + Flask + Docker. This app runs a couple of pytorch models (one of them being easyOCR and the other being YOLOv5).
The app was working fine, but then started to throw exception for all incoming requests. similar to the following
File "/usr/local/lib/python3.7/site-packages/werkzeug/wrappers/request.py", line 540, in json
return self.get_json()
File "/usr/local/lib/python3.7/site-packages/werkzeug/wrappers/request.py", line 575, in get_json
data = self.get_data(cache=cache)
File "/usr/local/lib/python3.7/site-packages/werkzeug/wrappers/request.py", line 405, in get_data
rv = self.stream.read()
File "/usr/local/lib/python3.7/site-packages/gunicorn/http/body.py", line 215, in read
data = self.reader.read(1024)
File "/usr/local/lib/python3.7/site-packages/gunicorn/http/body.py", line 130, in read
data = self.unreader.read()
File "/usr/local/lib/python3.7/site-packages/gunicorn/http/unreader.py", line 37, in read
d = self.chunk()
File "/usr/local/lib/python3.7/site-packages/gunicorn/http/unreader.py", line 64, in chunk
return self.sock.recv(self.mxchunk)
File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 203, in handle_abort
sys.exit(1)
SystemExit: 1
gunicorn parameters
gunicorn wsgi:app --bind 0.0.0.0:443 --log-level=info --workers=3 --reload --timeout 120
ACI specs
4 Cores
8 GB RAM
1 GPU Tesla K80
Linux environment
Followed this blog to create the app.
I tried adjusting the timeout parameter following many other posts such as this but didn't resolve the issue.
What caused this error and how do I fix this?
Thank you #amro_ghoneim for updating the resolution in the comments. Posting it as an answer to help other community members.
To get rid of those exceptions, change worker type to gevent.
To pause your application code for extended periods of time make use of gevent worker or -k gevent on the command line.
In order to add gevent worker, add below commands in your config file:
pip install gevent
gunicon .... --worker-class gevent
Reference :
Gunicorn worker timeout error - Stack Overflow
I am using Pythonista app on iOS and I cant make Flask module work even with the most basic code.
my code is :
from flask import Flask
Flaskapp = Flask(__name__)
#Flaskapp.route('/')
def helloWorld():
return 'Woala'
Flaskapp.run(debug=True)
but keep receiving:
* Serving Flask app "app" (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
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
Traceback (most recent call last):
File "/private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/app.py", line 9, in <module>
Flaskapp.run(debug=True)
File "/private/var/mobile/Containers/Shared/AppGroup/C4F03A05-D634-4F19-865F-6072E8D6DC38/Pythonista3/Documents/site-packages-3/flask/app.py", line 990, in run
run_simple(host, port, self, **options)
File "/private/var/mobile/Containers/Shared/AppGroup/C4F03A05-D634-4F19-865F-6072E8D6DC38/Pythonista3/Documents/site-packages-3/werkzeug/serving.py", line 1007, in run_simple
run_with_reloader(inner, extra_files, reloader_interval, reloader_type)
File "/private/var/mobile/Containers/Shared/AppGroup/C4F03A05-D634-4F19-865F-6072E8D6DC38/Pythonista3/Documents/site-packages-3/werkzeug/_reloader.py", line 332, in run_with_reloader
sys.exit(reloader.restart_with_reloader())
File "/private/var/mobile/Containers/Shared/AppGroup/C4F03A05-D634-4F19-865F-6072E8D6DC38/Pythonista3/Documents/site-packages-3/werkzeug/_reloader.py", line 176, in restart_with_reloader
exit_code = subprocess.call(args, env=new_environ, close_fds=False)
File "/var/containers/Bundle/Application/E373BC34-F149-4EF5-9D0E-5CD807AB0771/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/subprocess.py", line 268, in call
with Popen(*popenargs, **kwargs) as p:
File "/var/containers/Bundle/Application/E373BC34-F149-4EF5-9D0E-5CD807AB0771/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/subprocess.py", line 708, in __init__
restore_signals, start_new_session)
File "/var/containers/Bundle/Application/E373BC34-F149-4EF5-9D0E-5CD807AB0771/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/subprocess.py", line 1261, in _execute_child
restore_signals, start_new_session, preexec_fn)
PermissionError: [Errno 1] Operation not permitted
I have already installed flask trough pip in launch_stash.py
I received solution on another forum by user augusto, so I believe its worth sharing it here as well, as it works perfectly.
“The only way to use debug is:
if __name__ == '__main__':
app.run(use_reloader=False, debug=True)”
https://forum.omz-software.com/topic/5758/pythonista-flask-error-errno-1
The iOS sandbox has limitations that will prevent you from creating subprocesses
The flask development server is attempting to create a subprocess for detecting changes to your code that will then reload the server when you make changes. You can disable this by passing "--no-reload" flask run --no-reload. This would mean that you would have to manually restart your development server when you make changes
Change Pythonista’s default interpreter to Python 2.7.
I am trying to deploy a flask app on google cloud app engine. It runs smooth in my virtual environment locally but I get an 502 error running it in the cloud.
Now I am trying to debug my code on the cloud server, using debug mode and SSH into my instance. Using docker exec -it [ID] /bin/bash I am able to to access the root of my application. Now I upon running python app.py I get the following error:
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
Traceback (most recent call last):
File "app.py", line 479, in <module>
app.run(port=8080)
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 941, in run
run_simple(host, port, self, **options)
File "/usr/local/lib/python3.6/site-packages/werkzeug/serving.py", line 814, in run_simple
inner()
File "/usr/local/lib/python3.6/site-packages/werkzeug/serving.py", line 774, in inner
fd=fd)
File "/usr/local/lib/python3.6/site-packages/werkzeug/serving.py", line 660, in make_server
passthrough_errors, ssl_context, fd=fd)
File "/usr/local/lib/python3.6/site-packages/werkzeug/serving.py", line 577, in __init__
self.address_family), handler)
File "/usr/local/lib/python3.6/socketserver.py", line 453, in __init__
self.server_bind()
File "/usr/local/lib/python3.6/http/server.py", line 136, in server_bind
socketserver.TCPServer.server_bind(self)
File "/usr/local/lib/python3.6/socketserver.py", line 467, in server_bind
self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use
I've been trying to kill those processes listed when I run:
ps -fA | grep python
However, this does not solve the problem of the address being in use. Also changing the port in the app.run() does not solve the issue for me.
I had a similar problem, it was caused by the Flask app also being run when the module was loaded, because I had
if __name__ == "__main__":
app.run()
at the bottom. Note that the recent requirement to name your sever file "main.py" could cause this bug to emerge.
I think the problem is that you don't need to specialize the port for cloud. Google Cloud finds the port to run your app on its own. So instead of app.run(port=8080) just write app.run()
While I was not able to figure out how to "free" the running address, I solved the problem by starting another flask process by running it on a different port like so:
flask run --port=80
This question already has answers here:
How to kill a process running on particular port in Linux?
(34 answers)
Closed 6 years ago.
I have tried everything from last 2 days. But nothing really helped me out.
Background : I followed this to install flask app on production but I need to change serverName in vhost file so I forgot to kill an already running flask app/process and edited same vhost file to point to other location with few changes.
Problem : Now after apache restart, I continue getting following error when I access modified serverName.
mod_wsgi (pid=1685): Target WSGI script '/var/www/html/machine/machine.wsgi' cannot be loaded as Python module., referer: http://dev.badiyajobs.com/
mod_wsgi (pid=1685): Exception occurred processing WSGI script '/var/www/html/machine/machine.wsgi'., referer: http://dev.badiyajobs.com/
Traceback (most recent call last):
File "/var/www/html/machine/machine.wsgi", line$
from run import app as application
File "/var/www/html/machine/assessment/run.py",$
app.run()
File "/usr/local/lib/python2.7/dist-packages/fl$
run_simple(host, port, self, **options)
File "/usr/local/lib/python2.7/dist-packages/we$
inner()
File "/usr/local/lib/python2.7/dist-packages/we$
fd=fd)
File "/usr/local/lib/python2.7/dist-packages/we$
passthrough_errors, ssl_context, fd=fd)
File "/usr/local/lib/python2.7/dist-packages/we$
HTTPServer.__init__(self, (host, int(port)), $
File "/usr/lib/python2.7/SocketServer.py", line$
self.server_bind()
File "/usr/lib/python2.7/BaseHTTPServer.py", li$
SocketServer.TCPServer.server_bind(self)
File "/usr/lib/python2.7/SocketServer.py", line$
[self.socket.bind(self.server_address)
File "/usr/lib/python2.7/socket.py", line 224, $
return getattr(self._sock,name)(*args)
error: [Errno 98] Address already in use
Anybody please suggest how to get rid of already running app. I have tried lots of things regarding killing processes, but noting actually worked.
The easiest way to kill all processes that are listening on that port would be to use the fuser(1) command. For example, to see all of the processes listening for http requests on port 80 (run as root or use sudo):
fuser 80/tcp
If you want to kill them, then just add '-k' option
Source