zoukankan      html  css  js  c++  java
  • python web的进化历程

      对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端。

    阶段1

    socket服务端和客户端都自己编写

    实现访问8080端口,返回一个'hello world'

    #!/usr/bin/env python
    #encoding: utf-8
    #@2017-03-30
    """最简单的web框架"""
     
    import socket
    
    def handle_request(client):
        """应用程序,web开发者自定义部分"""
        buf = client.recv(1024)
        client.send('HTTP/1.1 200 OK1
    
    ')
        client.send("Hello, world!")
        
    def server():
        """服务端程序,web开发者共用部分
        本质:对socket进行封装"""
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.bind(('0.0.0.0', 8080))
        sock.listen(5)
        
        while True:
            connection, address = sock.accept()
            handle_request(connection) # 阻塞
            connection.close()
    
    if __name__ == '__main__':
        server()
    View Code

    阶段2

    WSGI(Web Server Gateway Interface)是一种规范,它定义了使用python编写的web app与web server之间接口格式,实现web app与web server间的解耦。python标准库提供的独立WSGI服务器称为wsgiref

    实现访问8000端口,返回一个'hello world'

    #!/usr/bin/env python
    #coding:utf-8
    
    # 封装后的服务程序
    from wsgiref.simple_server import make_server
    
    def RunServer(environ, start_response):
        start_response('200 OK', [('Content-Type', 'text/html')])
        return 'Hello, world!'
    
    if __name__ == '__main__':
        httpd = make_server('0.0.0.0', 8000, RunServer)
        print "Serving HTTP on port 8000..."
        httpd.serve_forever()
    View Code

    阶段3

    一些功能模块化,逐渐有了django的影子

    demo:点击下载

    main.py作为程序入口

    #!/usr/bin/env python
    #coding:utf-8
    
    # 封装后的服务程序
    from wsgiref.simple_server import make_server
    from urls import url
    
    def RunServer(environ, start_response):
        start_response('200 OK', [('Content-Type', 'text/html')])
        # 获取用户URL
        user_url = environ['PATH_INFO']
        
        # 根据URL不同返回不同的结果
        for item in url:
            if item[0] == user_url:
                return item[1]()
        else:
            return '<h1>404 not found</h1>'
    
    if __name__ == '__main__':
        httpd = make_server('0.0.0.0', 8000, RunServer)
        print "Serving HTTP on port 8000..."
        httpd.serve_forever()
    View Code

    views.py方法函数

    #!/usr/bin/env python
    #coding:utf-8
    
    def index():
        return 'index'
    
    def login():
        return 'login'
    
    def logout():
        return 'logout'
    
    url = (
        ('/index/', index),
        ('/login/', login),
        ('/logout/', logout),
    )
    View Code

    url到方法函数的映射urls.py

    #encoding: utf-8
    from views import *
    
    """指定URL到处理函数的映射"""
    url = (
        ('/index/', index),
        ('/login/', login),
        ('/logout/', logout),
    )
    View Code
  • 相关阅读:
    几种常用的曲线
    0188. Best Time to Buy and Sell Stock IV (H)
    0074. Search a 2D Matrix (M)
    0189. Rotate Array (E)
    0148. Sort List (M)
    0859. Buddy Strings (E)
    0316. Remove Duplicate Letters (M)
    0452. Minimum Number of Arrows to Burst Balloons (M)
    0449. Serialize and Deserialize BST (M)
    0704. Binary Search (E)
  • 原文地址:https://www.cnblogs.com/hiyang/p/6664349.html
Copyright © 2011-2022 走看看