• 技术文章 >Python技术 >Python基础教程

    Python中WSGI的使用

    小妮浅浅小妮浅浅2021-05-25 09:38:31原创2603

    1、WSGI是Python的Web开发的基石,有两个存在目的:

    描述 Web 服务器如何与 Web 应用程序交互(将客户端请求传给应用程序);

    描述 Web 应用程序如何处理请求和如何返回数据给服务器。

    2、由于Python内置的标准库里有一个WSGI库wsgiref,我们基于他来写一个体现WSGI目的的例子:

    from wsgiref.simple_server import make_server
     
    def application(environ, start_response):
        status = '200 OK'
        response_headers = [('Content-type', 'text/html')]
        start_response(status, response_headers)
        body = '<h1>Hello, {name} !!!</h1>'.format(name=environ['PATH_INFO'][1:] or 'WSGI')
        return [body.encode('utf-8')]
     
    app = make_server('', 8000, application)
    app.serve_forever()

    以上就是Python中WSGI的使用,希望对大家有所帮助。更多Python学习推荐:python教学

    本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

    专题推荐:python中wsgi
    品易云
    上一篇:Python3.8有哪些新特性? 下一篇:Python中实现WSGI的框架

    相关文章推荐

    • 深入了解WSGI与Werkzeug• python uwsgi是什么

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网