标签:
首先我们明白了其本质就是socket,如果从socket开始开发应用程序那么效率就太低了,正确的做法是底层的socket处理代码由专门的服务器软件实现,而对于真实开发中的python web程序来说也是一般会分为两个部分:服务器程序和应用程序,服务器程序负责对socket服务器进行封装,并在请求到来时,对请求的各种数据进行整理,应用程序则负责具体的逻辑处理。
WSGI(Web Server Gateway Interface)是一种规范,它定义了使用python编写的web app与web server之间接口格式,实现web app与web server间的解耦。
import socket def handle_request(client): #接收客户端信息并进行处理 buf = client.recv(1024) client.send("HTTP/1.1 200 OK\r\n\r\n".encode()) client.send("Hello, Seven".encode()) def main(): # 实例socket对象 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 绑定端口 sock.bind((‘localhost‘,8888)) # 设置监听客户端数量 sock.listen(5) while True: # 获取客户端socket对象和端口 connection, address = sock.accept() handle_request(connection) connection.close() if __name__ == ‘__main__‘: main()
python标准库提供的独立WSGI服务器称为wsgiref
from wsgiref.simple_server import make_server def RunServer(environ, start_response): start_response(‘200 OK‘, [(‘Content-Type‘, ‘text/html‘)]) return [‘<h1>Hello, web!</h1>‘.encode(‘utf-8‘)] #py3 # return ‘<h1>Hello, web!</h1>‘ py2 if __name__ == ‘__main__‘: httpd = make_server(‘‘, 8888, RunServer) print("Serving HTTP on port 8888...") httpd.serve_forever()
通过python标准库提供的wsgiref模块开发一个自己的web框架
from wsgiref.simple_server import make_server def index(): return [‘index‘.encode("utf-8")] def login(): return [‘login‘.encode("utf-8")] def routers(): # 路由函数 urlpatterns = ( (‘/index/‘,index), (‘/login/‘,login), ) return urlpatterns def RunServer(environ, start_response): # 响应头 start_response(‘200 OK‘, [(‘Content-Type‘, ‘text/html‘)]) # 获取客户端url信息 url = environ[‘PATH_INFO‘] # 执行路由函数,获取路由元组 urlpatterns = routers() func = None for item in urlpatterns: # 判断当前url,对应获取函数名 if item[0] == url: func = item[1] break if func: return func() else: return [‘404 not found‘.encode("utf-8")] if __name__ == ‘__main__‘: httpd = make_server(‘‘, 8888, RunServer) print("Serving HTTP on port 8888...") httpd.serve_forever()
在上一步骤中,返回到浏览器给客户看的只是一个简单的字符串,而在真实的生活场景里,我们看到更多的是一个复杂的HTML规则字符串,所以我们一般将要返回给客户的HTML写在指定文件中,然后返回
from wsgiref.simple_server import make_server def index(): # return ‘index‘ f = open(‘views/index.html‘) data = [f.read().encode()] return data def login(): # return ‘login‘ f = open(‘views/login.html‘) data = [f.read().encode()] return data def routers(): urlpatterns = ( (‘/index/‘, index), (‘/login/‘, login), ) return urlpatterns def run_server(environ, start_response): start_response(‘200 OK‘, [(‘Content-Type‘, ‘text/html‘)]) url = environ[‘PATH_INFO‘] urlpatterns = routers() func = None for item in urlpatterns: if item[0] == url: func = item[1] break if func: return func() else: return [‘404 not found‘.encode()] if __name__ == ‘__main__‘: httpd = make_server(‘‘, 8888, run_server) print("Serving HTTP on port 8888...") httpd.serve_forever()
可能说,你对这还不满意,因为只是一个静态页面,根本就没有动态效果,好,那怎么给客户返回动态内容??
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <h1>{{name}}</h1> {% for item in user_list %} <li>{{item}}</li> {% endfor %} </body> </html>
from wsgiref.simple_server import make_server from jinja2 import Template def index(): # return ‘index‘ # template = Template(‘Hello {{ name }}!‘) # result = template.render(name=‘John Doe‘) f = open(‘views\index.html‘) result = f.read() template = Template(result) data = template.render(name=‘John Doe‘, user_list=[‘alex‘, ‘eric‘]) return [data.encode(‘utf-8‘)] def login(): # return ‘login‘ f = open(‘views/login.html‘) data = [f.read().encode("utf-8")] return data def routers(): urlpatterns = ( (‘/index/‘, index), (‘/login/‘, login), ) return urlpatterns def run_server(environ, start_response): start_response(‘200 OK‘, [(‘Content-Type‘, ‘text/html‘)]) url = environ[‘PATH_INFO‘] urlpatterns = routers() func = None for item in urlpatterns: if item[0] == url: func = item[1] break if func: return func() else: return [‘404 not found‘.encode("utf-8")] if __name__ == ‘__main__‘: httpd = make_server(‘‘, 8888, run_server) print("Serving HTTP on port 8888...") httpd.serve_forever()
一个web框架应该包括路由系统和模板引擎等基本的东西,让我们看看一个高仿真自定义web框架:
from wsgiref.simple_server import make_server import time def new(): f = open(‘s1.html‘, ‘r‘) data = f.read() f.close() # 模拟模板引擎处理数据,将html里文件的item替换成其他数据 # 这里仅仅是用字符串的替换来最简单的说明下原理 new_data = data.replace("{{item}}", str(time.time())) return new_data def index(): f = open(‘index.html‘, ‘r‘) data = f.read() f.close() return data def home(): return ‘home‘ URLS = { # 定义一个字典,简单模拟路由系统 "/new": new, # 一个url对应一个函数处理 "/index": index, "/home": home, } def application(environ, start_response): start_response(‘200 OK‘, [(‘Content-Type‘, ‘text/html‘)]) url = environ[‘PATH_INFO‘] if url in URLS.keys(): # 不同的url执行不同的函数 func_name = URLS[url] ret = func_name() else: ret = "404" return [ret.encode(‘utf-8‘)] # 最后将结果返回 httpd = make_server(‘‘, 8000, application) httpd.serve_forever()
标签:
原文地址:http://www.cnblogs.com/xinsiwei18/p/5815797.html