web框架的前生 socket写网站

web应用的一个本质

  1. socket网络编程 -> cs架构 协议:tcp/udp 传输层
    2 web应用 -> bc架构 协议:Http协议 应用层

tips:
bytes 将字符串转为二进制
str 字节转为字符串

1:socket写网站
2: 路由系统:url-> 函数
3:模板引擎渲染

​ 1自己定义的规则

​ 2使用第三方的工具(jinja2)


def f1():
    fp = open("f1.html",'r',encoding="utf-8")
    data = fp.read()
    import time
    ctime = time.time()
    data = data.replace("@@content@@",str(ctime))
    return  bytes(data,encoding="utf-8")

def f2():
    fp = open("index.html",'r',encoding="utf-8")
    data = fp.read()
    return  bytes(data,encoding="utf-8")


def f3():
    import pymysql
    conn = pymysql.connect(host="127.0.0.1",user="root",password="123",database="t1")
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    sql = "select * from user"
    cursor.execute(sql)

    users = cursor.fetchall()
    print(users)
    user_list = []
    for info in users:
        res = "<tr><td>%s</td><td>%s</td><td>%s</td></tr>" % (info["id"],info["name"],info["age"])
        user_list.append(res)
    print(user_list)
    s = "".join(user_list)

    fp = open("user.html",'r',encoding="utf-8")
    data = fp.read()
    data = data.replace("@@content@@",s)
    return bytes(data,encoding="utf-8")


def f4():
    import pymysql
    conn = pymysql.connect(host="127.0.0.1", user="root", password="123", database="t1")
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    sql = "select * from user"
    cursor.execute(sql)

    users = cursor.fetchall()
    print(users)
    fp = open("user2.html","r",encoding="utf-8")
    data = fp.read()
    ### jinja2 模板引擎
    from jinja2 import Template
    tmplate = Template(data)
    data = tmplate.render(users=users)
    return bytes(data, encoding="utf-8")



routers = (
    ("/xxx",f1),
    ("/ooo",f2),
    ("/qqq",f3),
    ("/kkk",f4),

)

def run():
    import socket

    soc = socket.socket()
    soc.bind(("127.0.0.1", 8008))
    soc.listen(5)

    while True:
        conn, addr = soc.accept()
        data = conn.recv(8090)
        # print(data)
        # data转为字符转类型
        data_str = str(data)
        header_list = data_str.split("\r\n\r\n")
        headers = header_list[0]


        print(headers)
        url = headers.split("\r\n")[0].split(" ")[1]
        print(url)


        function_name = None
        for iterm in routers:
            if iterm[0] == url:
                function_name = iterm[1]
                break
        if function_name:
            res = function_name()
        else:
            res = bytes("not fond 404 ",encoding="utf-8")
        conn.send(bytes("HTTP/1.1 200 ok\r\n\r\n", encoding="utf-8"))
        conn.send(res)
        conn.close()

if __name__ == '__main__':
    run()
    

前端如何接渲染的数据

# 非模板引擎
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table border="1px">
    <thead>
        <tr>
            <th>ID</th>
            <th>age</th>
            <th>name</th>
        </tr>
    </thead>
    <tbody>
        @@content@@
    </tbody>
</table>
</body>
</html>

## 模板引擎

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户</title>
</head>
<body>
<table>
    <thead>
        <tr>
            <td>ID</td>
            <td>age</td>
            <td>name</td>
        </tr>
    </thead>
    <tbody>
        {% for item in users %}
        <tr>
            <td>{{ item.id }}</td>
            <td>{{ item.name }}</td>
            <td>{{ item.age }}</td>
        </tr>
        {% endfor %}
    </tbody>
</table>
</body>
</html>

由此引入框架的概念

web框架的分类

第一个维度

1.django -- socket(manage.py)引用第三方,(wsgiref/uwsgi(上线之后)),其余都自己写:路由,模板引擎渲染

2.flask --socket,模板引擎 引入第三方,自己写了路由系统

3.tornado(龙卷风),三者都是自己的

第二个维度分类

1.django (功能丰富)大而全  重武器  内部组件多

 **  Orm,admin,中间件,form,session,缓存,信号,csrf  **

2.flask        ~短小精悍  可扩展强 
3.Tornado  ~短小精悍,异步非阻塞

posted @ 2021-04-26 23:07  enazede  阅读(80)  评论(0编辑  收藏  举报