簡易WSGI實現

1.使用python內置WSGI server

# WSGI server in Python
from wsgiref.simple_server import make_server
def application (environ, start_response):
    status = '200 OK'
    response_headers = [
        ('Content-Type', 'text/plain')]
    start_response(status, response_headers)
    return ["welcome to gevent lesson"]
# Instantiate the WSGI server.
# It will receive the request, pass it to the application
# and send the application's response to the client
httpd = make_server(
    'localhost', # The host name.
    8080, # A port number where to wait for the request.
    application # Our application object name, in this case a function.
    )

2.Flask內置WSGI測試和gevent wsgi結合Flask app

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
    return 'Hello World!'
if __name__ == '__main__':
    app.run()
from flask import  Flask
import gevent.pywsgi
import gevent
app = Flask(__name__)
@app.route('/')
def handle():
    return 'welcome to gevent lesson!'
gevent_server = gevent.pywsgi.WSGIServer(('', 5000), app)
gevent_server.serve_forever()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章