搭建 python web 服务器

命令1:html

python -m http.server 8080

命令2:python

python -m SimpleHTTPServer 8080

解释:8080是端口号,此命令在什么文件夹下面运行,就会自动把这个文件夹设置成web的根目录web

如今测试一下是否成功:bash

文件名:hello.py服务器

def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    #body = '<h1>Hello, %s!</h1>' % (environ['PATH_INFO'][1:] or 'web')
    body = '<h1>Hello, web</h1>'
    return [body.encode('utf-8')]

文件名:server.pyapp

# 从wsgiref模块导入:
from wsgiref.simple_server import make_server
# 导入咱们本身编写的application函数:
from hello import application

# 建立一个服务器,IP地址为空,端口是8000,处理函数是application:
httpd = make_server('', 8089, application)
print('Serving HTTP on port 8089...')
# 开始监听HTTP请求:
httpd.serve_forever()

将以上2个文件放在根目录下执行,能够看到 Hello, web 就算设置成功了!函数