码迷,mamicode.com
首页 > Web开发 > 详细

WEB框架的本质

时间:2017-06-03 15:10:46      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:logout   页面   解耦   color   content   部分   body   inf   odi   

 1 #!/usr/bin/env python
 2 #coding:utf-8
 3   
 4 import socket
 5   
 6 def handle_request(client):
 7     buf = client.recv(1024)
 8     client.send("HTTP/1.1 200 OK\r\n\r\n")
 9     client.send("Hello, Seven")
10   
11 def main():
12     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
13     sock.bind((localhost,8000))
14     sock.listen(5)
15   
16     while True:
17         connection, address = sock.accept()
18         handle_request(connection)
19         connection.close()
20   
21 if __name__ == __main__:
22     main()

上述通过socket来实现了其本质,而对于真实开发中的python web程序来说,一般会分为两部分:服务器程序和应用程序服务器程序负责对socket服务器进行封装,并在请求到来时,对请求的各种数据进行整理。应用程序则负责具体的逻辑处理。为了方便应用程序的开发,就出现了众多的Web框架,例如:Django、Flask、web.py 等。不同的框架有不同的开发方式,但是无论如何,开发出的应用程序都要和服务器程序配合,才能为用户提供服务。这样,服务器程序就需要为不同的框架提供不同的支持。这样混乱的局面无论对于服务器还是框架,都是不好的。对服务器来说,需要支持各种不同框架,对框架来说,只有支持它的服务器才能被开发出的应用使用。这时候,标准化就变得尤为重要。我们可以设立一个标准,只要服务器程序支持这个标准,框架也支持这个标准,那么他们就可以配合使用。一旦标准确定,双方各自实现。这样,服务器可以支持更多支持标准的框架,框架也可以使用更多支持标准的服务器。

WSGI(Web Server Gateway Interface)是一种规范,它定义了使用python编写的web app与web server之间接口格式,实现web app与web server间的解耦。

 1 from wsgiref.simple_server import make_server
 2  
 3  
 4 def RunServer(environ, start_response):
 5     start_response(200 OK, [(Content-Type, text/html)])
 6     return [bytes(<h1>Hello, web!</h1>, encoding=utf-8), ]
 7  
 8  
 9 if __name__ == __main__:#封装服务器
10     httpd = make_server(‘‘, 8000, RunServer)
11     print("Serving HTTP on port 8000...")
12     httpd.serve_forever()

 上述代码可能在运行的时候会出错,使用下面的就不会出错:

 1 #!/usr/bin/env python
 2 #coding:utf-8
 3 
 4 from wsgiref.simple_server import make_server
 5 
 6 
 7 def RunServer(environ, start_response):
 8     start_response(200 OK, [(Content-Type, text/html)])
 9 
10     #return [bytes(‘<h1>Hello, web!</h1>‘, ‘utf-8‘), ]   报错地段,改成如下就好
11     body=<h1>Hello, web!</h1>
12     return [body.encode(utf-8)]
13 ‘‘‘
14     # 获取url,PATH_INFO可以通过断点来查询获得
15     userurl = environ[‘PATH_INFO‘]
16     # 根据url的不同返回不同的结果
17     if userurl == ‘/index‘:
18         return ‘<h1>Hello, web!</h1>‘
19     elif userurl == ‘/login/‘:
20         return ‘<h1>index</h1>‘
21     elif userurl == ‘/logout/‘:
22         return ‘<h1>logout</h1>‘
23     else:
24         return ‘<h1>404 not found</h1>‘
25 ‘‘‘
26 
27     #return [‘hello world‘]
28 
29 
30 if __name__ == __main__:
31     httpd = make_server(‘‘, 8000, RunServer)
32     print("Serving HTTP on port 8000...")
33     httpd.serve_forever()

不同的版本:

 1 #!/usr/bin/env python
 2 #coding:utf-8
 3 
 4 from wsgiref.simple_server import make_server
 5 
 6 class DemoApp:
 7     def __call__(self, environ, start_response):
 8         response_headers = [(Content-type,text/plain)]
 9         start_response(200 OK, response_headers)
10         return ["Hello World"]
11 
12 
13 if __name__ == __main__:
14     httpd = make_server(‘‘, 1000, DemoApp()) # Don‘t forget instantiate a class.
15     #                                    ^^
16     print("Serving on port 1000")
17     httpd.serve_forever()

 


 

初步理解一个WEB框架(自定义):

 1 #!/usr/bin/env python
 2 #coding=utf-8
 3 
 4 from wsgiref.simple_server import make_server
 5 
 6 def index():
 7     return index
 8 
 9 def login():
10 
11     #file打开html文件
12     #将html文件读取到内存
13     html = ‘‘‘<p>username:<input /></p><p>password:<input /></p>‘‘‘
14     return html
15 
16 url=(
17     (/index/,index),
18     (/manage/,index),
19     (/login/,login),
20 )
21 
22 
23 
24 
25 def RunServer(environ, start_response):
26     start_response(200 OK, [(Content-Type, text/html)])
27 
28     #return [bytes(‘<h1>Hello, web!</h1>‘, ‘utf-8‘), ]
29     #body=‘<h1>Hello, web!</h1>‘
30     #return [body.encode(‘utf-8‘)]
31 
32     # 获取url,PATH_INFO可以通过断点来查询获得
33     userurl = environ[PATH_INFO]
34 
35     func=None
36     for item in url:
37         if item[0] == userurl:
38             func= item[1]
39             break
40     if func:
41         result= func()
42     else:
43         result= 404
44 
45     return result
46 
47 
48 ‘‘‘
49     # 根据url的不同返回不同的结果
50     if userurl == ‘/index‘:
51         return ‘<h1>Hello, web!</h1>‘
52     elif userurl == ‘/login/‘:
53         return ‘<h1>index</h1>‘
54     elif userurl == ‘/logout/‘:
55         return ‘<h1>logout</h1>‘
56     else:
57         return ‘<h1>404 not found</h1>‘
58 
59 
60     #return [‘hello world‘]
61 ‘‘‘
62 
63 if __name__ == __main__:
64     httpd = make_server(‘‘, 8000, RunServer)
65     print("Serving HTTP on port 8000...")
66     httpd.serve_forever()

 

mvc框架的使用:(c:业务逻辑处理,m:对数据库的处理,v:html),主要就是代码的归类,代码的放置原则。说多了就是扯淡

 在django里面是使用mtv模型:

  • M 代表模型(Model),即数据存取层。 该层处理与数据相关的所有事务: 如何存取、如何验证有效性、包含哪些行为以及数据之间的关系等。
  • T 代表模板(Template),即表现层。 该层处理与表现相关的决定: 如何在页面或其他类型文档中进行显示。
  • V 代表视图(View),即业务逻辑层。 该层包含存取模型及调取恰当模板的相关逻辑。 你可以把它看作模型与模板之间的桥梁。

WEB框架的本质

标签:logout   页面   解耦   color   content   部分   body   inf   odi   

原文地址:http://www.cnblogs.com/bill2014/p/6936177.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!