19. python从入门到精通——Web编程( 四 )


缺陷:编写程序时不方便
解决办法:WSGI
WSGI
WSGI (服务器网关接口):
是为语言定义的web服务器和web应用程序或者是框架之间的一种简单而通用的接口,它是基于设计的 。
WSGI工作原理
客户端向web服务器发起一个请求,WSGI的底层是通过进行沟通,当服务器接受到请求以后可以通过套接字把环境变量和一个函数传递给后端的,之后在应用程序中完成页面组装,然后通过回调函数返回给服务器,最后服务器再将响应
补充:

19. python从入门到精通——Web编程

文章插图
函数:回调函数是一个函数,将会在另一个函数完成执行后立即执行 。回调函数是一个作为参数传给另一个函数的函数,这个回调函数会在传给的函数内部执行 。
定义WSGI接口 语法:
(此函数的两个参数都需要从服务器获取,所以这本函数必须由WSGI服务器来调用 。)
def (,):
(,)
[]
#:自定义函数名
#:这是一个包含所有HTTP请求信息的dict对象
#:一个发送http响应的函数
示例:
def application(environ,start_response):start_response('200 OK',[('Content-Type,'text/html')])return [b'Hello,World!']
实例:通过模块实现一个简单的hello,word
目前、nginx等web服务器都符合WSGI接口的规范,在中也内置了一个WSGI服务器:模块
from wsgiref.simple_serverimport make_server#回调函数def sayHi(environ,start_response):#以元组的方式添加响应头start_response("200 OK", [('Content-Type', 'text/html; charset=utf-8')])#body部分return [b'Hello World']if __name__=='__main__':#三个参数:IP地址默认为127.0.0.1,端口号,回调函数ser = make_server('',8000,sayHi)#启动程序ser.serve_forever()
验证:终端运行此程序,然后浏览器访问127.0.0.1
运行WSGI服务
实例:创建网站课程页面,根据访问地址访问不同html页面
三个网页:
.html
明日科技body {position: relative; /* For scrollspy */}/* Keep code small in tables on account of limited space */.table code {font-size: 13px;font-weight: normal;}/* Inline code within headings retain the heading's background-color */h2 code,h3 code,h4 code {background-color: inherit;}/* Outline button for use within the docs */.btn-outline {color: #563d7c;background-color: transparent;border-color: #563d7c;}.btn-outline:hover,.btn-outline:focus,.btn-outline:active {color: #fff;background-color: #563d7c;border-color: #563d7c;}/* Inverted outline button (white on dark) */.btn-outline-inverse {color: #fff;background-color: transparent;border-color: #cdbfe3;}.btn-outline-inverse:hover,.btn-outline-inverse:focus,.btn-outline-inverse:active {color: #563d7c;text-shadow: none;background-color: #fff;border-color: #fff;}/* Bootstrap "B" icon */.bs-docs-booticon {display: block;font-weight: 500;color: #fff;text-align: center;cursor: default;background-color: #563d7c;border-radius: 15%;}.bs-docs-booticon-sm {width: 30px;height: 30px;font-size: 20px;line-height: 28px;}.bs-docs-booticon-lg {width: 144px;height: 144px;font-size: 90px;line-height: 140px;}.bs-docs-booticon-inverse {color: #563d7c;background-color: #fff;}.bs-docs-booticon-outline {background-color: transparent;border: 1px solid #cdbfe3;}/** Main navigation** Turn the `.navbar` at the top of the docs purple.*/.bs-docs-nav {margin-bottom: 0;background-color: #fff;border-bottom: 0;}.bs-home-nav .bs-nav-b {display: none;}.bs-docs-nav .navbar-brand,.bs-docs-nav .navbar-nav > li > a {font-weight: 500;color: #563d7c;}.bs-docs-nav .navbar-nav > li > a:hover,.bs-docs-nav .navbar-nav > .active > a,.bs-docs-nav .navbar-nav > .active > a:hover {color: #463265;background-color: #f9f9f9;}.bs-docs-nav .navbar-toggle .icon-bar {background-color: #563d7c;}.bs-docs-nav .navbar-header .navbar-toggle {border-color: #fff;}.bs-docs-nav .navbar-header .navbar-toggle:hover,.bs-docs-nav .navbar-header .navbar-toggle:focus {background-color: #f9f9f9;border-color: #f9f9f9;}/** Homepage** Tweaks to the custom homepage and the masthead (main jumbotron).*//* Share masthead with page headers */.bs-docs-masthead,.bs-docs-header {position: relative;padding: 30px 0;color: #cdbfe3;text-align: center;text-shadow: 0 1px 0 rgba(0,0,0,.1);background-color: #6f5499;background-image: -webkit-gradient(linear, left top, left bottom, from(#563d7c), to(#6f5499));background-image: -webkit-linear-gradient(top, #563d7c 0%, #6f5499 100%);background-image:-o-linear-gradient(top, #563d7c 0%, #6f5499 100%);background-image:linear-gradient(to bottom, #563d7c 0%, #6f5499 100%);filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#563d7c', endColorstr='#6F5499', GradientType=0);background-repeat: repeat-x;}/* Masthead (headings and download button) */.bs-docs-masthead .bs-docs-booticon {margin: 0 auto 30px;}.bs-docs-masthead h1 {font-weight: 300;line-height: 1;color: #fff;}.bs-docs-masthead .lead {margin: 0 auto 30px;font-size: 20px;color: #fff;}.bs-docs-masthead .version {margin-top: -15px;margin-bottom: 30px;color: #9783b9;}.bs-docs-masthead .btn {width: 100%;padding: 15px 30px;font-size: 20px;}@media (min-width: 480px) {.bs-docs-masthead .btn {width: auto;}}@media (min-width: 768px) {.bs-docs-masthead {padding: 80px 0;}.bs-docs-masthead h1 {font-size: 60px;}.bs-docs-masthead .lead {font-size: 24px;}}