服务器程序和应用程序

对于真实开发中的python web程序来说,一般会分为两部分:服务器程序和应用程序。

服务器程序负责对socket服务端进行封装,并在请求到来时,对请求的各种数据进行整理。

应用程序则负责具体的逻辑处理。为了方便应用程序的开发,就出现了众多的web框架,例如:Django、Flask、web.py等。不同的框架有不同的开发方式,但是如论如何,开发出的应用程序都要和服务器程序配合,才能为用户提供服务。

这样,服务器程序就需要为不同的框架提供不同的支持,这种混乱的局面无论对于服务器还是框架,都是不好的。对于服务器来说,需要支持不同的框架;对框架来说,只有支持它的服务器才能别开发出的应用使用。

这时候标准化就变得非常重要,我们可以设定一个标准,只要服务器程序支持这个标准,框架也支持这个标准,那么他们就可以配合使用。一旦标准确定,双方各自实现。这样,服务器就可以支持更多支持标准的框架,框架也可以使用更多支持标准的服务器。

WSGI(Web Server Gateway Interface)就是一种规范,它定义了使用Python编写的web应用程序与web服务器程序之间的接口格式,显示web应用程序与web服务器程序间的解耦。

常用的WSGI服务器有uwsgi,Gunicom。而Python标准库提供的独立WSGI服务器叫wsgiref, Django开发环境用的就是这个模块来做服务器。

1. wsgiref

使用wsgiref模块来替换自己写的web框剪的socket server部分:

 1 from wsgiref.simple_server import make_server   
 2 #将返回不同的内容部分封装成函数   
 3 def index(url):   
 4     # 读取index.html页面的内容   
 5     with open("index.html", "r", encoding="utf8") as f:   
 6         s = f.read()   
 7     # 返回字节数据   
 8     return bytes(s, encoding="utf8")   
 9      
10      
11 def home(url):   
12     with open("home.html", "r", encoding="utf8") as f:   
13         s = f.read()   
14     return bytes(s, encoding="utf8")   
15      
16      
17 def timer(url):   
18     import time   
19     with open("time.html", "r", encoding="utf8") as f:   
20         s = f.read()   
21         s = s.replace('@@time@@', time.strftime("%Y-%m-%d %H:%M:%S"))   
22     return bytes(s, encoding="utf8")   
23      
24      
25 # 定义一个url和实际要执行的函数的对应关系   
26 list1 = [   
27     ("/index/", index),   
28     ("/home/", home),   
29     ("/time/", timer),   
30 ]   
31      
32      
33 def run_server(environ, start_response):   
34     start_response('200 OK', [('Content-Type', 'text/html;charset=utf8'), ])  # 设置HTTP响应的状态码和头信息   
35     url = environ['PATH_INFO']  # 取到用户输入的url   
36     func = None   
37     for i in list1:   
38         if i[0] == url:   
39             func = i[1]   
40             break   
41     if func:   
42         response = func(url)   
43     else:   
44         response = b"404 not found!"   
45     return [response, ]   
46      
47      
48 if __name__ == '__main__':   
49     httpd = make_server('127.0.0.1', 8090, run_server)   
50     httpd.serve_forever()  
wsgiref模块版

 2. jinja2

上面的代码只是实现了一个简单的动态,完全可以从数据库中查询数据,然后去替换html中对应的内容,然后再发送给浏览器来完成渲染。

这个过程就相当于HTML模板渲染数据。

本质上就是HTML内容中利用一些特殊的符号来替换要展示的数据。

下载jinja2的命令:

pip3 install jinja2

2.1 使用jinja2渲染index.html文件

 首先建立一个名称为index.html的文件,代码如下:

 1 <!DOCTYPE html>
 2 <html lang="zh-CN">
 3 <head>
 4   <meta charset="UTF-8">
 5   <meta http-equiv="x-ua-compatible" content="IE=edge">
 6   <meta name="viewport" content="width=device-width, initial-scale=1">
 7   <title>Title</title>
 8 </head>
 9 <body>
10     <h1>姓名:{{name}}</h1>
11     <h1>爱好:</h1>
12     <ul>
13         {% for hobby in hobby_list %}
14         <li>{{hobby}}</li>
15         {% endfor %}
16     </ul>
17 </body>
18 </html>
index.html

使用jinja2渲染index.html文件代码如下:

 1 from wsgiref.simple_server import make_server  
 2 from jinja2 import Template  
 3   
 4   
 5 def index(url):  
 6     # 读取HTML文件内容  
 7     with open("index2.html", "r", encoding="utf8") as f:  
 8         data = f.read()  
 9         template = Template(data)   # 生成模板文件  
10         ret = template.render({'name': 'yang', 'hobby_list': ['读书', '运动', '写代码']})   # 把数据填充到模板中  
11     return bytes(ret, encoding="utf8")  
12   
13   
14 def home(url):  
15     with open("home.html", "r", encoding="utf8") as f:  
16         s = f.read()  
17     return bytes(s, encoding="utf8")  
18   
19   
20 # 定义一个url和实际要执行的函数的对应关系  
21 list1 = [  
22     ("/index/", index),  
23     ("/home/", home),  
24 ]  
25   
26   
27 def run_server(environ, start_response):  
28     start_response('200 OK', [('Content-Type', 'text/html;charset=utf8'), ])  # 设置HTTP响应的状态码和头信息  
29     url = environ['PATH_INFO']  # 取到用户输入的url  
30     func = None  
31     for i in list1:  
32         if i[0] == url:  
33             func = i[1]  
34             break  
35     if func:  
36         response = func(url)  
37     else:  
38         response = b"404 not found!"  
39     return [response, ]  
40   
41   
42 if __name__ == '__main__':  
43     httpd = make_server('127.0.0.1', 8090, run_server)  
44     print("我在8090等你哦...")  
45     httpd.serve_forever()
jinja2渲染index.html

现在页面中的数据都是我们自己手写的,但是还可以从数据库查询数据,来填充页面。
使用pymysql连接数据库:

1 conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="xxx", db="xxx", charset="utf8")
2 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
3 cursor.execute("select name, age, department_id from userinfo")
4 user_list = cursor.fetchall()
5 cursor.close()
6 conn.close()
pymysql连接数据库

创建一个测试的user表:

1 CREATE TABLE user(
2   id int auto_increment PRIMARY KEY,
3   name CHAR(10) NOT NULL,
4   hobby CHAR(20) NOT NULL
5 )engine=innodb DEFAULT charset=UTF8;
user表

模板的原理就是字符串替换,只要在HTML页面中遵循jinja2的语法规则编写,其内部就会按照指定的语法进行相应的替换,从而达到动态的返回内容。

 

 

猜你喜欢

转载自www.cnblogs.com/yang-wei/p/9971333.html