web框架

转载:https://www.cnblogs.com/huang-yc/p/9647815.html

本文为纯属个人学习笔记,如有疏漏错误之处望前辈指正!

1. web 应用

       Web应用程序是一种可以通过Web访问的应用程序,程序的最大好处是用户很容易访问应用程序,用户只需要有浏览器即可,不需要再安装其他软件。

  应用程序有两种模式C/S、B/S。C/S是客户端/服务器端程序,也就是说这类程序一般独立运行。而B/S就是浏览器端/服务器端应用程序,这类应用程序一般借助谷歌,火狐等浏览器来运行。

  WEB应用程序一般是B/S模式。Web应用程序首先是“应用程序”,和用标准的程序语言,如java,python等编写出来的程序没有什么本质上的不同。在网络编程的意义下,浏览器是一个socket客户端,服务器是一个socket服务端。

import socket

def handle_request(client):

    request_data = client.recv(1024)
    print("request_data: ",request_data)
    client.send("HTTP/1.1 200 OK

".encode("utf8"))
    client.send("<h1 style='color:red'>Hello, web! </h1>".encode("utf8"))

def main():

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('localhost',8000))
    sock.listen(5)

    while True:
        print("the server is waiting for client-connection....")
        connection, address = sock.accept()
        handle_request(connection)
        connection.close()

if __name__ == '__main__':

    main()

2.web  框架

       Web框架(Web framework)是一种开发框架,用来支持动态网站、网络应用和网络服务的开发。这大多数的web框架提供了一套开发和部署网站的方式,也为web行为提供了一套通用的方法。

  web框架已经实现了很多功能,开发人员使用框架提供的方法并且完成自己的业务逻辑,就能快速开发web应用了。浏览器和服务器的是基于HTTP协议进行通信的。也可以说web框架就是在以上十几行代码基础张扩展出来的,有很多简单方便使用的方法,大大提高了开发的效率。

3.wsgi 模块

       最简单的Web应用就是先把HTML用文件保存好,用一个现成的HTTP服务器软件,接收用户请求,从文件中读取HTML,返回。

  如果要动态生成HTML,就需要把上述步骤自己来实现。不过,接受HTTP请求、解析HTTP请求、发送HTTP响应都是苦力活,如果我们自己来写这些底层代码,还没开始写动态HTML呢,就得花个把月去读HTTP规范。

  正确的做法是底层代码由专门的服务器软件实现,我们用Python专注于生成HTML文档。因为我们不希望接触到TCP连接、HTTP原始请求和响应格式,所以,需要一个统一的接口协议来实现这样的服务器软件,让我们专心用Python编写Web业务。这个接口就是WSGI:Web Server Gateway Interface。而wsgiref模块就是python基于wsgi协议开发的服务模块。

Python 3.x
from wsgiref.simple_server import make_server


def RunServer(environ, start_response):
     #environ客户发来的所有数据
    start_response('200 OK', [('Content-Type', 'text/html')])
    #start_response 封装要返回给客户的数据,响应头状态
    return [b'<h1>Hello,web!</h1>']
    #返回给用户的内容
    #b: 字符串转字节(Python2.x中无bite.

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

运行该程序,在网页中输入:127.0.0.1:8000,即可打印:

ps:字符串转字节的三种方式:

①b'ffffff";

②bytes('ffffff', encoding = 'utf8');

③'ffffff'.encoding('utf8');

示例1:

from wsgiref.simple_server import make_server

def handle_index():
    return [b'<h1>Hello,index!</h1>']

def handle_date():
    return [b'<h1>Hello,date !</h1>']

def RunServer(environ, start_response):


    start_response('200 OK', [('Content-Type', 'text/html')])
    current_url = environ['PATH_INFO']
    if current_url =='/index':
        return handle_index()
    elif current_url == '/date':
        return handle_date()
    else:
        return [b'<h1>404!</h1>']



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

在网页中输入:127.0.0.1:8000/date,即可打印

------有很多URL怎么办?

 示例2:

将URL存放于字典中:

from wsgiref.simple_server import make_server

def handle_index():
    return [b'<h1>Hello,index!</h1>']

def handle_date():
    return [b'<h1>Hello,date !</h1>']

URL_DICT = {
    "/index": handle_index,
    "/date": handle_date,       #可用正则表达式写虚拟的
}
def RunServer(environ, start_response):


    start_response('200 OK', [('Content-Type', 'text/html')])
    current_url = environ['PATH_INFO']
    func = None
    if current_url in URL_DICT:
        func =URL_DICT[current_url]
    if func:
        return func()
    else:
        return [b'<h1>404!</h1>']



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

示例3:

可将需要打印的信息存放于HTML中,通过读取HTML文件获取打印信息:

from wsgiref.simple_server import make_server

def handle_index():
    f = open("index.html", mode='rb')
    data = f.read()
    f.close()
    return [data, ]

def handle_date():
    return [b'<h1>Hello,date !</h1>']

URL_DICT = {
    "/index": handle_index,
    "/date": handle_date,
}
def RunServer(environ, start_response):


    start_response('200 OK', [('Content-Type', 'text/html')])
    current_url = environ['PATH_INFO']
    func = None
    if current_url in URL_DICT:
        func =URL_DICT[current_url]
    if func:
        return func()
    else:
        return [b'<h1>404!</h1>']



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

index.html文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>INDEX</h1>
</body>
</html>

通过以上代码,当需要处理大量的重复的操作时,我们可以采取模板,或者将某一类文件放在同一个文件夹中,比如将模板文件放在Model文件夹中,将处理函数放在Controler文件夹中,将数据库文件等放在View文件夹中,这样有了MVC框架。

web框架  
  MVC框架 Model View Controler
  数据库 模板文件 业务处理
MTV框架 Model Template View
  数据库 模板文件 业务处理

注: Django是标准的MTV框架

原文地址:https://www.cnblogs.com/bltstop/p/11166895.html