WSGI

# 客户端函数,environ包含所有HTTP请求的dict对象,start_response则是作为http请求响应的函数
def application(environ,start_response):
    # http响应的header
    start_response('200 OK',[('Content-Type','text/html')])
    # http响应的body
    return [b'<h1>Hello,web,goodbye,web!</h1>']

#启动WSGI进行测试一下,打开浏览器,输入:http://localhost:8000
# 导入python标准库中的wagiref模块中的simple_server中的make_serve方法
from wsgiref.simple_server import make_server
# 导入application函数
from main import application
# 用make_server方法创建一个简单的服务器,ip地址为本机,端口号为8000
# 并处理导入的application函数
httpd=make_server('127.0.0.1',8000,application)
# 输出的提示内容
print('Serving HTTP on port 8000...')
# 使用aerve_forever()监听http请求
httpd.serve_forever()

# 更加动态的获取用户名
# 客户端函数,environ包含所有HTTP请求的dict对象,start_response则是作为http请求响应的函数
def application(environ,start_response):
    # http响应的header
    start_response('200 OK',[('Content-Type','text/html')])
    # http响应的body
    body='<h1>Hello,%s</h1>'%(environ['PATH_INFO'][1:] or 'web')
    return [body.encode('utf-8')]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WSGI是Python Web Server Gateway Interface的缩写,是Python应用程序或框架和Web服务器之间的一种接口,用于在Web服务器上运行Python Web应用程序。在Windows上使用WSGI,可以通过以下步骤实现: 1.安装mod_wsgi模块:可以从官方网站(https://www.lfd.uci.edu/~gohlke/pythonlibs/#mod_wsgi)下载对应版本的mod_wsgi模块,然后使用pip进行安装。 2.配置Apache服务器:在httpd.conf文件中添加以下内容: ```apache LoadModule wsgi_module "C:/Python27/Lib/site-packages/mod_wsgi/server/mod_wsgi.pyd" WSGIScriptAlias /myapp "C:/path/to/myapp.wsgi" <Directory "C:/path/to"> Order allow,deny Allow from all </Directory> ``` 其中,第一行是加载mod_wsgi模块,第二行是指定WSGI应用程序的URL路径和WSGI文件的路径,第三至五行是指定WSGI文件所在目录的访问权限。 3.编写WSGI应用程序:创建一个名为myapp.wsgi的文件,编写WSGI应用程序的代码,例如: ```python def application(environ, start_response): status = '200 OK' output = 'Hello World!' response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))] start_response(status, response_headers) return [output] ``` 其中,application函数是WSGI应用程序的入口,environ是一个包含HTTP请求信息的字典,start_response是一个回调函数,用于发送HTTP响应头,返回值是HTTP响应体。 4.启动Apache服务器:在命令行中输入httpd命令启动Apache服务器,然后在浏览器中访问http://localhost/myapp即可看到Hello World!的输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值