第一篇.WEB框架

一.基于wsgiref定义自己的web框架

socket不用再自己做了,别人给我封装好了,就用这个模块wsgiref模块,后台数据会发生动态变化就是动态网页

1.1目录

2.2相关文件

template下的四个html文件

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table border="1">
    <thead>
    <tr>
        <th>id</th>
        <th>name</th>
        <th>password</th>
    </tr>
    </thead>
    <tbody>
    {%for user in user_list%}
    <tr>
        <td>{{user.id}}</td>
        <td>{{user.name}}</td>
        <td>{{user.password}}</td>
    </tr>
    {%endfor%}
    </tbody>
</table>
</body>
</html>
相关代码

test.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{{user.name}}
{{user.age}}
</body>
</html>
相关代码

time.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
@@time@@
</body>
</html>
相关代码

user.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table border="1">
    <thead>
    <tr>
        <th>id</th>
        <th>name</th>
        <th>password</th>
    </tr>
    </thead>
    <tbody>
    {%for user in user_list%}
    <tr>
        <td>{{user.id}}</td>
        <td>{{user.name}}</td>
        <td>{{user.password}}</td>
    </tr>
    {%endfor%}
    </tbody>
</table>
</body>
</html>
相关代码

url.py

from views import *
urls=[
    ('/index',index),
    ('/time',time),
    ('/test',test),
    ('/user',user),
]
相关代码

views.py

import datetime
from jinja2 import Template
import pymysql

def index(env):
    with open('templates/index.html','r') as f:
        data=f.read()
    return data

def time(env):
    ctime=datetime.datetime.now().strftime('%Y-%m-%d %X')
    with open('templates/time.html','r') as f:
        data=f.read()
        data=data.replace('@@time@@',ctime)
    return data

def error(env):
    return '404'

def test(env):
    with open('templates/test.html','r') as f:
        data=f.read()
    tem=Template(data)
    response=tem.render(user={'name':'lqz','age':18})
    return response

def user(env):
        conn=pymysql.connect(host='127.0.0.1',port=3306,user='root',db='wml',password="123")
        cur=conn.cursor(pymysql.cursors.DictCursor)#游标
        rows=cur.execute('select * from user')
        print(rows)
        dic=cur.fetchall()
        print(dic)
        with open('templates/user.html','r') as f:
            data=f.read()
        tem=Template(data)
        response=tem.render(user_list=dic)
        return response
相关代码

wsgirefServer.py

from wsgiref.simple_server import make_server
from url import urls
from views import error

def run(env,response):
    print(env)#env是一个字典
    response('200 OK',[('Content-type','text/html')])
    position=env['PATH_INFO']
    func=None
    for url in urls:
        if position==url[0]:
            func=url[1]
            break
    if func:
        response=func(env)
    else:
        response=error(env)
    return [response.encode('utf-8'),]

if __name__=='__main__':
    ser=make_server('127.0.0.1',8003,run)
    ser.serve_forever()
相关代码

二.python中三大主流框架分析

a :socket
b:路由跟视图函数匹配关系
c:模板渲染

django: a: 用了别人的wsgiref   b:自己写的   c:自己写的
flask: a:用了别人的            b:自己写的   c:用了别人的:jinja2
tornado: a 自己写的(支持高并发) b:自己写的   c:自己写的 

 

posted @ 2019-03-07 16:11  王苗鲁  阅读(106)  评论(0编辑  收藏  举报