当前位置: 移动技术网 > IT编程>脚本编程>Python > python基于WSGI服务器程序实现helllo

python基于WSGI服务器程序实现helllo

2020年07月07日  | 移动技术网IT编程  | 我要评论

WSGI简介

WSGI全称为Web Server Gateway Interface,开始于2003年。Web服务器是连接用户浏览器与Python服务器端程序的中间节点,在网站中起着重要作用。WSGI是将Python程序连接到Web服务器的通用协议。WSGI的接口可分为两个,一个是与Web服务器的接口,另一个是与服务器端程序的接口。下面用python一个本地网站演示枯燥的理论

例子:用python实现WSGI 服务器端程序

实现代码:

def application(environ, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return (b'<b>Hello,World!</b>',)


from wsgiref.simple_server import make_server

server = make_server('', 8080, application)
server.serve_forever()

实现原理:
特别简短,理解起来有点困难。application是一个函数,包含了所有http请求都会经过这里,本次该函数没有复杂处理,只是同通过start_response返回状态码,并return出固定的http消息体。最后我们运行程序在浏览器输入localhost:8080即可看到效果。
实验效果:
在这里插入图片描述

本文地址:https://blog.csdn.net/m0_37149062/article/details/107165403

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网