简易WSGI实现

本文介绍了如何使用Python内置的WSGI服务器以及gevent库来实现高性能的Web应用。首先,通过一个简单的示例展示了WSGI服务器的基本用法,接着结合Flask框架和gevent的WSGIServer,演示了如何创建并运行一个支持异步IO的Web应用程序。

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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值