简单的web框架(python)

Posted yuyafeng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单的web框架(python)相关的知识,希望对你有一定的参考价值。

1、web框架介绍

Web框架(Web framework)是一种开发框架,用来支持动态网站、网络应用和网络服务的开发。这大多数的web框架提供了一套开发和部署网站的方式,也为web行为提供了一套通用的方法。web框架已经实现了很多功能,开发人员使用框架提供的方法并且完成自己的业务逻辑,就能快速开发web应用了。浏览器和服务器的是基于HTTP协议进行通信的。也可以说web框架就是在以上十几行代码基础张扩展出来的,有很多简单方便使用的方法,大大提高了开发的效率。

 

2、实现简单的web框架

基于socket写一个web应用

第一版本   

  通过HTTP协议传送过来的信息返回页面

import socket


server = socket.socket()
server.bind((127.0.0.1,8000))
server.listen(5)


while True:
    conn,addr = server.accept()
    while True:
        data = conn.recv(1024)
        ‘‘‘
        ***data***
        b‘GET /index/ HTTP/1.1\r\n
        Host: 127.0.0.1:8000\r\n
        Connection: keep-alive\r\n
        Upgrade-Insecure-Requests: 1\r\n
        User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/74.0.3729.131 Safari/537.36\r\n
        Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3\r\n
        Accept-Encoding: gzip, deflate, br\r\n
        Accept-Language: zh-CN,zh;q=0.9,und;q=0.8,en;q=0.7\r\n
        Cookie: csrftoken=kzAhOkqkoPdnQuOxI7AQTa8aOmT7g6ebPiwrI0jpQ8m04NmYLkzkFvDtD8febu41; Hm_lvt_080836300300be57b7f34f4b3e97d911=1559726342,1559732417\r\n\r\n‘
        ‘‘‘
        conn.send(bHTTP/1.1 200 OK\r\n\r\n)
        ‘‘‘
        页面显示Hello
        必须遵守http协议返回一个响应头
        ‘‘‘
        current_path = data.decode(utf-8).split(\r\n)[0].split( )[1]
        ‘‘‘
        /index/
        ‘‘‘
        if current_path == /index/:
            with open(index.html, rb) as f:
                conn.send(f.read())
        else:
            conn.send(b404)
        conn.close()

第二版本

  根据HTTP传送过来的信息返回相应的页面,通过列表保存信息模拟django中的urls,通过函数返回数据模拟django中views

import socket

server = socket.socket()
server.bind((127.0.0.1, 8000))
server.listen(5)


def index():
    return index


def login():
    return login


def error():
    return 404


urls = [
    (/index/, index),
(‘/login/‘, login) ]
while True: conn, addr = server.accept() while True: data = conn.recv(1024) ‘‘‘ ***data*** b‘GET /index/ HTTP/1.1\r\n Host: 127.0.0.1:8000\r\n Connection: keep-alive\r\n Upgrade-Insecure-Requests: 1\r\n User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36\r\n Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3\r\n Accept-Encoding: gzip, deflate, br\r\n Accept-Language: zh-CN,zh;q=0.9,und;q=0.8,en;q=0.7\r\n Cookie: csrftoken=kzAhOkqkoPdnQuOxI7AQTa8aOmT7g6ebPiwrI0jpQ8m04NmYLkzkFvDtD8febu41; Hm_lvt_080836300300be57b7f34f4b3e97d911=1559726342,1559732417\r\n\r\n‘ ‘‘‘ conn.send(bHTTP/1.1 200 OK\r\n\r\n) ‘‘‘ 页面显示Hello 必须遵守http协议返回一个响应头 ‘‘‘ current_path = data.decode(utf-8).split(\r\n)[0].split( )[1] ‘‘‘ /index/ ‘‘‘
     res = None for url in urls: if url[0] == current_path: res = url[1]() break else: res = error() conn.send(res.encode(utf-8))

第三版

  将其拆分成start.py   urls.py   views.py

import socket
import urls

server = socket.socket()
server.bind((127.0.0.1, 8000))
server.listen(5)


def error():
    return 404


while True:
    conn, addr = server.accept()
    while True:
        data = conn.recv(1024)
        ‘‘‘
        ***data***
        b‘GET /index/ HTTP/1.1\r\n
        Host: 127.0.0.1:8000\r\n
        Connection: keep-alive\r\n
        Upgrade-Insecure-Requests: 1\r\n
        User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36\r\n
        Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3\r\n
        Accept-Encoding: gzip, deflate, br\r\n
        Accept-Language: zh-CN,zh;q=0.9,und;q=0.8,en;q=0.7\r\n
        Cookie: csrftoken=kzAhOkqkoPdnQuOxI7AQTa8aOmT7g6ebPiwrI0jpQ8m04NmYLkzkFvDtD8febu41; Hm_lvt_080836300300be57b7f34f4b3e97d911=1559726342,1559732417\r\n\r\n‘
        ‘‘‘
        conn.send(bHTTP/1.1 200 OK\r\n\r\n)
        ‘‘‘
        页面显示Hello
        必须遵守http协议返回一个响应头
        ‘‘‘
        current_path = data.decode(utf-8).split(\r\n)[0].split( )[1]
        ‘‘‘
        /index/
        ‘‘‘
        res = None
        for url in urls.urls:
            if url[0] == current_path:
                res = url[1]()
                break
            else:
                res = error()
        conn.send(res.encode(utf-8))
import views
urls = [
    (/index/, views.index),
    (/login/, views.login)
]
def index():
    return index


def login():
    return login

高级版

  使用wsgiref.simple_server的make_server配置web应用

from wsgiref.simple_server import make_server
from urls import *


def run(env, response):
    ‘‘‘
    :param env: 请求相关的信息,一个处理好的字符串
    :param response:  响应相关信息
    :return:
    ‘‘‘
    response(200 OK, [(username, jason), (password, 123)])  # 固定写法 后面列表里面一个个元祖会以响应头kv键值对的形式返回给客户端
    current_path = env.get(PATH_INFO)
    func = None
    for url in urls:
        if current_path == url[0]:
            func = url[1]
            break
    if func:
        res = func(env)
    else:
        res = error(env)
    return [res.encode(utf-8)]


if __name__ == __main__:
    server = make_server(127.0.0.1, 8080, run)
    server.serve_forever()
def index(env):
    return index


def login(env):
    return login


def error(env):
    return 404
from views import *
urls = [
    (/index/, index),
    (/login/, login)
]

终极版

  使用jinja2对模板进行替换,对数据库进行连接

from wsgiref.simple_server import make_server
from urls import *


def run(env, response):
    ‘‘‘
    :param env: 请求相关的信息,一个处理好的字符串
    :param response:  响应相关信息
    :return:
    ‘‘‘
    response(200 OK, [(username, jason), (password, 123)])  # 固定写法 后面列表里面一个个元祖会以响应头kv键值对的形式返回给客户端
    current_path = env.get(PATH_INFO)
    func = None
    for url in urls:
        if current_path == url[0]:
            func = url[1]
            break
    if func:
        res = func(env)
    else:
        res = error(env)
    return [res.encode(utf-8)]


if __name__ == __main__:
    server = make_server(127.0.0.1, 8080, run)
    server.serve_forever()
import time
from jinja2 import Template
import pymysql


def index(env):
    return index


def login(env):
    return login


def get_time(env):
    # 先获取当前时间
    current_time = time.strftime(%Y-%m-%d %X)
    # 打开html文件读取内容返回给客户端
    with open(rtemplates/get_time.html, r, encoding=utf-8) as f:
        data = f.read()
    # 因为是以r模式打开的文件,所有获取到的内容就是一堆字符串
    res = data.replace(@@[email protected]@, current_time)  # 字符串的替换
    return res


def get_user(env):
    with open(rtemplates/get_user.html, r, encoding=utf-8) as f:
        data = f.read()
    tmp = Template(data)
    # 将字典传递给前端页面 前端通过变量名user_dic就可以获取到该字典
    return tmp.render(user_dic=name: "jason", password: 123)


def get_db(env):
    # 连接数据库 获取数据 渲染到前端页面
    conn = pymysql.connect(
        host=127.0.0.1,
        port=3306,
        user=root,
        password=123,
        database=blog,
        charset=utf8,
        autocommit=True
    )
    cursor = conn.cursor(pymysql.cursors.DictCursor)
    cursor.execute(select * from userinfo)
    user_dict = cursor.fetchall()  # [,,,]
    with open(rtemplates/get_db.html, r, encoding=utf-8) as f:
        data = f.read()
    tmp = Template(data)
    return tmp.render(user_dict=user_dict)


def error(env):
    return 404 error
from views import *

urls = [
    (/index,index),
    (/login,login),
    (/get_time,get_time),
    (/get_user,get_user),
    (/get_db,get_db),
]
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
@@[email protected]@
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p> user_dic </p>
<p> user_dic.name </p>
<p> user_dic[password] </p>
<p> user_dic.get(name) </p>
</body>
</html>

 

以上是关于简单的web框架(python)的主要内容,如果未能解决你的问题,请参考以下文章

python的web开发框架都有哪些

Python简单Web框架web.py实例hello world

从零构建一个简单的 Python Web框架

用python做个简单web介面选啥框架最简单

我的第一个python web开发框架——一个简单的小外包

python web开发用啥框架