前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[1169]python编程WSGI服务器wsgiref模块

[1169]python编程WSGI服务器wsgiref模块

作者头像
周小董
发布2022-11-02 18:15:08
4020
发布2022-11-02 18:15:08
举报
文章被收录于专栏:python前行者python前行者

WSGI的全称是Web Server Gateway Interface,Web服务器网关接口

具体的来说,WSGI是一个规范,定义了Web服务器如何与Python应用程序进行交互

WSGI 相当于是Web服务器和Python应用程序之间的桥梁

image.png
image.png

使用python内置的模块实现一个服务器

python3下示例

代码语言:javascript
复制
# WSGI服务器的参考实现

# 【应用程序】
# 处理函数
def application(environ, start_response):
    start_response("200 OK", [('Content-Type', 'text/html')])
    body = "<h1>hello world %s<h1>"% (environ["PATH_INFO"][1:] or "web")
    return [ body.encode()]


# 【服务器】
from wsgiref.simple_server import make_server

# 创建一个服务器,是application
server = make_server("localhost", 9999, application)

print("服务启动,按Ctrl+C终止... http://localhost:9999/")

# 开始监听HTTP请求
server.serve_forever()

参考:https://blog.csdn.net/mouday/article/details/80738858 https://www.xncoding.com/2016/04/22/python/wsgi.html

解决python3中自定义wsgi函数,make_server函数报错的问题: http://startmvc.com/article_20687.html

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-10-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档