AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Delphi-PRAXiS - Lounge Delphi-News aus aller Welt How to use gevent and Flask on Windows
Thema durchsuchen
Ansicht
Themen-Optionen

How to use gevent and Flask on Windows

Ein Thema von DP News-Robot · begonnen am 21. Mär 2019
Antwort Antwort
Benutzerbild von DP News-Robot
DP News-Robot

Registriert seit: 4. Jun 2010
14.962 Beiträge
 
#1

How to use gevent and Flask on Windows

  Alt 21. Mär 2019, 22:21
Flask

Flask is one of most popular python framework for web solution. It is a non-opinionated framework, an I really like it. Flask contains also a nice and useful development web server with automatic reloading. However, when you have to deploy a Flask application you cannot rely on the development web server, you need a production ready system. More info about Flask here

gevent

gevent is a coroutine-based Python networking library that uses greenlet to provide a high-level synchronous API on top of libev event loop. More info about gevent here.

Being WSGI compliant, gevent can be used to serve your Flask application in a professional way. But How should be configured these two guys to work together?

Serving Flask from gevent

Let's say that you have the following Flask application (file: flask_app.py):

# file: flask_app.pyfrom flask import Flaskapp = Flask(__name__)@app.route('/')def index(): return "Hello World"@app.route('/home')def home(): return "This is the home page"
To serve this simple application you should, as usual, use the following command line.

C:\DEV>set FLASK_APP=flask_app.pyC:\DEV>flask run * Running on http://localhost:5000/
To serve this Flask app using gevent, you can use the following snippet (file: gevent_app.py).

# file: gevent_app.pyfrom flask_app import appfrom gevent.pywsgi import WSGIServerhttp_server = WSGIServer(('0.0.0.0', 80), app)http_server.serve_forever()
This snippet creates an instance of the WSGI server provided by gevent and configures the instance to binds to all the available network interfaces ('0.0.0.0') on port 80.

Running the usual python gevent_app.py you have a production ready web service.

Using HTTPS

What about service your service through HTTPS? Quite simple. You have just to configure gevent' WSGI to use the proper certificates. The certificates can be self-signed (the browser will identify your site as "Not Secure") or provided by one of the certified certification authorities.

# file: gevent_app_https.pyfrom flask_app import appfrom gevent.pywsgi import WSGIServerhttp_server = WSGIServer(('0.0.0.0', 443), app, keyfile='privkey.pem', certfile='cacert.pem')http_server.serve_forever()
Running the usual python gevent_app_https.py you have a production ready HTTPS web service.



Weiterlesen...
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 17:22 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz