基于python的web编程基础

Posted xiekejian

tags:

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

 

一、框架分类

     1、sockect+业务处理

      该类框架有:tornado

2、只有业务逻辑,不包含socket,而是引用第三方组件(socket),这个第三方组件(socket)要遵循的协议就是:wsgi

框架有:django、bottle、flask  主要是逻辑处理、第三方的模块(包含了socket)

3、python中内置了wsgiref,它包含了socket

4、WEB框架原理

二、框架讲解

第一步:

from wsgiref.simple_server import make_server
def new():
    return \'new\'

def index():
    return \'index\'

#用户请求所有内容封装在变量:environ
#返回给用户所有信息变量:start_response
def RunServer(environ, start_response):
    start_response(\'200 OK\', [(\'Content-Type\', \'text/html\')])
    #获取用户URL
    url =  environ[\'PATH_INFO\']
    if url == "/new":
        ret = new()
    elif url == "/index":
        ret = index()
    else:
        ret = "404"
    return ret


if __name__ == \'__main__\':
    httpd = make_server(\'\', 8000, RunServer)
    print("Serving HTTP on port 8000...")
    httpd.serve_forever()

运行结果

 

 

 

 改进版

from wsgiref.simple_server import make_server
def new():
    return \'new\'

def index():
    return \'index\'

URLS = {
    "/new": new,
    "/index": index,
}

#用户请求所有内容封装在变量:environ
#返回给用户所有信息变量:start_response
def RunServer(environ, start_response):
    start_response(\'200 OK\', [(\'Content-Type\', \'text/html\')])
    #获取用户URL
    url =  environ[\'PATH_INFO\']
    # if url == "/new":
    #     ret = new()
    # elif url == "/index":
    #     ret = index()
    # else:
    #     ret = "404"
    # return ret
    if url in URLS.keys():
        func_name = URLS[url]
        ret = func_name()
    else:
        ret = "404"
    return ret


if __name__ == \'__main__\':
    httpd = make_server(\'\', 8000, RunServer)
    print("Serving HTTP on port 8000...")
    httpd.serve_forever()

再进化

from wsgiref.simple_server import make_server
def new():
    return \'<html><head></head><body><h1 style="color:red;">asdf</h1></body></html>\'

def index():
    return \'index\'

URLS = {
    "/new": new,
    "/index": index,
}

#用户请求所有内容封装在变量:environ
#返回给用户所有信息变量:start_response
def RunServer(environ, start_response):
    start_response(\'200 OK\', [(\'Content-Type\', \'text/html\')])
    #获取用户URL
    url =  environ[\'PATH_INFO\']
    # if url == "/new":
    #     ret = new()
    # elif url == "/index":
    #     ret = index()
    # else:
    #     ret = "404"
    # return ret
    if url in URLS.keys():
        func_name = URLS[url]
        ret = func_name()
    else:
        ret = "404"
    return ret


if __name__ == \'__main__\':
    httpd = make_server(\'\', 8000, RunServer)
    print("Serving HTTP on port 8000...")
    httpd.serve_forever()
运行结果:

 再向前进化一步

结构:

from wsgiref.simple_server import make_server
def new():
    f = open(\'new.html\',\'r\')
    data = f.read()
    return data

def index():
    return \'index\'

URLS = {
    "/new": new,
    "/index": index,
}

#用户请求所有内容封装在变量:environ
#返回给用户所有信息变量:start_response
def RunServer(environ, start_response):
    start_response(\'200 OK\', [(\'Content-Type\', \'text/html\')])
    #获取用户URL
    url =  environ[\'PATH_INFO\']
    # if url == "/new":
    #     ret = new()
    # elif url == "/index":
    #     ret = index()
    # else:
    #     ret = "404"
    # return ret
    if url in URLS.keys():
        func_name = URLS[url]
        ret = func_name()
    else:
        ret = "404"
    return ret


if __name__ == \'__main__\':
    httpd = make_server(\'\', 8000, RunServer)
    print("Serving HTTP on port 8000...")
    httpd.serve_forever()

运行结果:

 

再向前进化

V-- new.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
  <h1>hello you</h1>
</body>
</html>

C -- controller.py
def new():
    f = open(\'new.html\',\'r\')
    data = f.read()
    return data

def index():
    return \'index\'

def home():
    return \'home\'

路由:urls
import  controller

URLS = {
    "/new": controller.new,
    "/index": controller.index,
    "/home": controller.home,
}

启动程序:start.py
from wsgiref.simple_server import make_server
from urls import URLS

#返回给用户所有信息变量:start_response
def RunServer(environ, start_response):
    start_response(\'200 OK\', [(\'Content-Type\', \'text/html\')])
    #获取用户URL
    url =  environ[\'PATH_INFO\']
    if url in URLS.keys():
        func_name = URLS[url]
        ret = func_name()
    else:
        ret = "404"
    return ret


if __name__ == \'__main__\':
    httpd = make_server(\'\', 8000, RunServer)
    httpd.serve_forever()

进一步升级:模板

 

看代码:

import os
import time
def new():
    f = open(os.path.join(\'views\',\'new.html\'),\'r\')
    data = f.read()
    new_data = data.replace(\'{{itme}}\',str(time.time()))
    return new_data

def index():
    f = open(os.path.join(\'views\', \'index.html\'), \'r\')
    data = f.read()
    return data

def home():
    f = open(os.path.join(\'views\', \'home.html\'), \'r\')
    data = f.read()
    return data

 下面引入模块语言:第三方组件jingjia2    安装:pip3 install jingjia2

4、初识tornado框架

自定义:

 

二、web编程之cookie

          1、原理 

                     在浏览器端保存的键值对,特性:每次HTTP请求都会携带;

          2、服务端tornado操作cookie

                    A、查看所有cookie所有对象:self.cookies
                    B、获取cookie对象信息值:self.get_cookie(\'k1\')
                    C、设置cookies对象信息:self.set_cookie(\'k1\',\'999\')

         3、在浏览器上使用javascript

                  A、document.cookie 查找所有cookie对象         

                  B、从上图看,cookie就是一个字符串,因此可采用截取或循环的方式进行

 

          4、带签名的cookie原理

          

5、基于cookiet用户验证

方式一:

方式二:

 

三、web编程之session

session信息保存在服务端,要想使用session信息,还要依赖于cookie对象。

 

以上是关于基于python的web编程基础的主要内容,如果未能解决你的问题,请参考以下文章

麦子学院:Python Flask快速构建高性能大型企业级web网站项目实战

编程基础之Python6Python基础知识

JSP编程专题1之JSP基础

[vscode]--HTML代码片段(基础版,reactvuejquery)

Python该怎么入门?

学习参考《Flask Web开发:基于Python的Web应用开发实战(第2版)》中文PDF+源代码