【从零学习python 】89. 使用WSGI搭建简单高效的Web服务器

简介: 【从零学习python 】89. 使用WSGI搭建简单高效的Web服务器
+关注继续查看

新建WSGI服务器

创建hello.py文件,用来实现WSGI应用的处理函数。

def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    print(environ)
    return ['<h1>Hello, web!</h1>'.encode('utf-8'),'hello'.encode('utf-8')]

创建server.py文件,用来启动WSGI服务器,加载application函数。

# 从wsgiref模块导入:
from wsgiref.simple_server import make_server
# 导入我们自己编写的application函数:
from hello import application
# 创建一个服务器,IP地址为空,端口是8000,处理函数是application:
httpd = make_server('', 8000, application)
print("Serving HTTP on port 8000...")
# 开始监听HTTP请求:
httpd.serve_forever()
相关文章
|
4天前
|
Docker Python Windows
1行命令搭建自己的python服务器,docker,安装 jupyter
1行命令搭建自己的python服务器,docker,安装 jupyter
30 0
|
22天前
|
机器学习/深度学习 Linux Python
服务器上后台运行python程序
服务器上后台运行python程序
31 0
|
1月前
|
Python
169 python网络编程 - Web动态服务器
169 python网络编程 - Web动态服务器
14 0
|
1月前
|
Java 应用服务中间件 API
168 python网络编程 - 服务器动态资源请求
168 python网络编程 - 服务器动态资源请求
26 0
|
1月前
|
Python
167 python网络编程 - Web静态服务器
167 python网络编程 - Web静态服务器
22 0
|
1月前
|
Python
165 python网络编程 - 单进程服务器(gevent版)
165 python网络编程 - 单进程服务器(gevent版)
19 0
|
1月前
|
Python
161 python网络编程 - 单进程服务器(epoll版)
161 python网络编程 - 单进程服务器(epoll版)
9 0
|
1月前
|
Unix Linux 调度
160 python网络编程 - 单进程服务器(select版)
160 python网络编程 - 单进程服务器(select版)
18 0
|
1月前
|
Python
159 python网络编程 - 单进程服务器(非堵塞模式)
159 python网络编程 - 单进程服务器(非堵塞模式)
13 0
|
1月前
|
Python
158 python网络编程 - 多线程服务器
158 python网络编程 - 多线程服务器
14 0
相关产品
云迁移中心
推荐文章
更多