对于所有的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 OK\1\n\r\n')
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()

阶段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()

阶段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()

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),
)

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

#encoding: utf-8
from views import * """指定URL到处理函数的映射"""
url = (
('/index/', index),
('/login/', login),
('/logout/', logout),
)

python web的进化历程的更多相关文章

  1. 我的第一个python web开发框架(1)——前言

    由于之前经验不是很丰富,写的C#系统太过复杂,所以一直想重写,但学的越多越觉得自己懂的越少,越觉的底气不足.所以一直不敢动手,在内心深处对自己讲,要静下心来认真学习,继续沉淀沉淀.这两年多以来找各种机 ...

  2. Redis的Python实践,以及四中常用应用场景详解——学习董伟明老师的《Python Web开发实践》

    首先,简单介绍:Redis是一个基于内存的键值对存储系统,常用作数据库.缓存和消息代理. 支持:字符串,字典,列表,集合,有序集合,位图(bitmaps),地理位置,HyperLogLog等多种数据结 ...

  3. Python Web 方向(一)

    Python Web 方向(一) --------Django站点创建 文章地址:http://www.cnblogs.com/likeli/p/5821744.html Python版本:2.7 推 ...

  4. 浅谈五大Python Web框架

    转载:http://feilong.me/2011/01/talk-about-Python-web-framework 说到Web Framework,Ruby的世界Rails一统江湖,而Pytho ...

  5. nginx上部署python web

    nginx上部署python web http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

  6. 全面解读python web 程序的9种部署方式

    转载自鲁塔弗的博客,本文地址http://lutaf.com/141.htm  python有很多web 开发框架,代码写完了,部署上线是个大事,通常来说,web应用一般是三层结构 web serve ...

  7. Python Web 开发的十个框架【转载】

    Python 是一门动态.面向对象语言.其最初就是作为一门面向对象语言设计的,并且在后期又加入了一些更高级的特性.除了语言本身的设计目的之外,Python标准 库也是值得大家称赞的,Python甚至还 ...

  8. tornado 学习笔记2 Python web主流框架

    2.1 Django 官方网址:https://www.djangoproject.com/ 简介:Django is a high-level Python Web framework that e ...

  9. python web框架——扩展Django&tornado

    一 Django自定义分页 目的:自定义分页功能,并把它写成模块(注意其中涉及到的python基础知识) models.py文件 # Create your models here. class Us ...

随机推荐

  1. 关于STM32F103系列从大容量向中容量移植的若干问题

    一.把STM32F103大容量移植到STM32F103C8T6上的步骤: 1.换启动文件 startup_stm32f10x_cl.s           ——互联型的器件 包括:STM32F105x ...

  2. TensorFlow 模型优化工具包  —  训练后整型量化

    模型优化工具包是一套先进的技术工具包,可协助新手和高级开发者优化待部署和执行的机器学习模型.自推出该工具包以来,  我们一直努力降低机器学习模型量化的复杂性 (https://www.tensorfl ...

  3. VS2015 Visual Assist X 破解版安装教程

    前言 此方法适合VS2010~VS2015版本. 安装包下载方法:公众号[视觉IMAX]后台回复「VA助手」,即可得到安装包下载链接. 工作以来,一直在使用VS2015,之前一直根据的网上飘云阁的破解 ...

  4. 【SQL SERVER】索引

    在做开发过程中经常会接触数据库索引,不只是DBA才需要知道索引知识,了解索引可以让我们写出更高质量代码. 索引概述 聚集索引 非聚集索引 唯一索引 筛选索引 非聚集索引包含列 索引概述 索引的存在主要 ...

  5. 基于STM32F030F4P9和STM32 CUBEMX 输出PWM波形

    STM32F030F4P9定时器功能比较丰富,在此记录项目中使用其自动输出PWM波形(频率:50HZ).CubeMX配置定时器如下图说明. 在此定时器基础时钟为48MHZ,配置中不做分频处理,预分频系 ...

  6. Jenkins集成时报错 hudson.remoting.Channel$CallSiteStackTrace: Remote call to JNLP4-connect connection from xxx.xxx.xxx.xxx/xxx.xxx.xxx.xxx:32034

    Started by user test Running as SYSTEM Building remotely on home_windows (mbhCloud_UI_Test) in works ...

  7. XSS之绕过WAF总结

    来源<XSS跨站脚本攻击剖析与防御>&<WEB前端技术揭秘> 一.一般测试方法 步骤: 0.总则:见框就插 1.在输入框随便输入一些简单的字符,如 aaa,方便后续查找 ...

  8. 1063 Set Similarity (25分)

    Given two sets of integers, the similarity of the sets is defined to be /, where N​c​​ is the number ...

  9. ceph概述

                                                                            ceph概述   基础知识 什么是分布式文件系统 •   ...

  10. 彻底理解Java对象与引用

    本文是最近在复习Java的时候读到的来自iteye博客的一篇文章,个人觉得写的很好,但貌似楼主许久没有维护该博客,害怕将来想回顾的时候找不到此文章,故在此重写文章,并修改了一些排版,有助于以后的学习查 ...