您当前的位置: 首页 >  服务器

Python编程:WSGI服务器的参考实现wsgiref模块

彭世瑜 发布时间:2018-06-19 20:44:37 ,浏览量:2

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

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

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

Web服务器
WSGI
Python应用程序

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

python3下示例

# WSGI服务器的参考实现

# 【应用程序】
# 处理函数
def application(environ, start_response):
    start_response("200 OK", [('Content-Type', 'text/html')])
    body = "hello world %s"% (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()

参考

  1. WSGI接口- 廖雪峰博客
  2. WGSI简易教程
关注
打赏
1688896170
查看更多评论

彭世瑜

暂无认证

  • 2浏览

    0关注

    2727博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.1504s