django 动态

main函数

import socket


def f1(request):
    """
    处理用户请求,并返回相应的内容
    :param request:用户请求的所有信息
    :return:
    """
    f = open('bilibili.html', 'rb')
    data = f.read()
    f.close()
    return data


def f2(request):
    f = open('article.tpl', 'r', encoding='utf-8')
    data = f.read()
    f.close()
    import time
    ctime = time.time()
    data = data.replace('@@sw@@', str(ctime))
    return bytes(data, encoding='utf-8')


def f3(_):
    import pymysql
    # 创建连接
    conn = pymysql.connect(host="127.0.0.1", port=3305, user='root', passwd='020804', db='s8day127db')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    cursor.execute("select user_id,user_email,email_code from mail")
    user_list = cursor.fetchall()
    cursor.close()
    conn.close()

    content_list = []
    for row in user_list:
        tp = "<tr><td>%s</td><td>%s</td><td>%s</td></tr>" % (row['user_id'], row['user_email'], row['email_code'])
        content_list.append(tp)
    content = "".join(content_list)
    f = open('userlist.html', 'r', encoding='utf-8')
    template = f.read()
    f.close()
    data = template.replace("@@content@@", content)
    return bytes(data, encoding='utf-8')
def f4(request):
    import pymysql
    # 创建连接
    conn = pymysql.connect(host="127.0.0.1", port=3305, user='root', passwd='020804', db='s8day127db')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    cursor.execute("select user_id,user_email,email_code from mail")
    user_list = cursor.fetchall()
    cursor.close()
    conn.close()

    f = open('hostlist.html', 'r', encoding='utf-8')
    data = f.read()
    f.close()

    from jinja2 import Template
    template = Template(data)
    data = template.render(user_list = user_list)

    return data.encode('utf-8')

routers = [
    ('/xxx', f1),  # 存url
    ('/ooo', f2),  # 存函数名
    ('/userlist.htm', f3), #读取数据库
    ('/host', f4),  # 存函数名
]


def run():
    sock = socket.socket()
    sock.bind(('127.0.0.1', 8080))
    sock.listen(5)

    while True:
        conn, addr = sock.accept()  # hang住
        # 有人来连接
        # 获取用户发送的数据
        data = conn.recv(8096)
        # print(data)
        data = str(data, encoding='utf-8')
        headers, bodys = data.split("\r\n\r\n")
        temp_list = headers.split("\r\n")
        method, url, protocal = temp_list[0].split(' ')  # 通过空格分割
        conn.send(b"HTTP/1.1 200 OK\r\n\r\n")
        func_name = None
        for item in routers:
            if item[0] == url:
                func_name = item[1]
                break
        if func_name:
            response = func_name(data)
        else:
            response = b'404'

        conn.send(response)
        conn.close()


if __name__ == '__main__':
    run()

hostlist.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
  <table border='1'>
      <thead>
        <tr>
          <th>user_id</th>
          <th>user_email</th>
          <th>email_code</th>
        </tr>
      </thead>
      <tbody>
         {% for row in user_list %}
            <tr>
                <td>{{row.user_id}}</td>
                <td>{{row.user_email}}</td>
                <td>{{row.email_code}}</td>
            </tr>
         {%endfor%}
      </tbody>
  </table>
</body>
</html>

userlist.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
  <table border='1'>
      <thead>
        <tr>
          <th>user_id</th>
          <th>user_email</th>
          <th>email_code</th>
        </tr>
      </thead>
      <tbody>
        @@content@@
      </tbody>
  </table>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值