python之web框架(3):WSGI之web应用完善

1.上篇的web框架太low,只能实现回应固定页面。现在将它进行完善。首先将wsgi和web服务器进行分离,并给予它回复静态页面的能力。

  • web_server.py
#!/usr/bin/env python3
# coding:utf-8from test_frame import app
from socket import *
from multiprocessing import Processclass MyWebServer(object):def start_response(self, status, head_list):self.response_head = 'HTTP/1.1 ' + status + ' \r\n'self.response_head = self.response_head.encode()# print(self.response_head)def deal(self, conn):recv_data = conn.recv(1024).decode('utf-8')recv_data_head = recv_data.splitlines()[0]# print('------recv_data_head: ', recv_data_head)request_method, request_path, http_version = recv_data_head.split()request_path = request_path.split('?')[0]  # 去掉url中的?和之后的参数env = {'request_method': request_method, 'request_path': request_path}# 这里是wsgi接口调用的地方response_body = app(env, self.start_response)response_data = self.response_head + b'\r\n' + response_bodyconn.send(response_data)# print('response_data = ', response_data)def __init__(self):self.s = socket(AF_INET, SOCK_STREAM)self.s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)self.s.bind(('', 8000))self.s.listen(1023)self.response_head = ''def start(self):while 1:conn, user_info = self.s.accept()print(user_info, '接入')p = Process(target=self.deal, args=(conn,))p.start()conn.close()  # 进程会复制出一个新的conn,所以这里的conn需要关闭s = MyWebServer()
s.start()
  • test_frame.py
def app(env, start_response):file_name = env['request_path']if file_name == '/':file_name = '/index.html'try:f = open('.' + file_name, 'rb')except IOError:status = '404 error'head_list = [("name", "wanghui")]start_response(status, head_list)return b'<h1>File not found</h1>'status = '200 OK'head_list = [("name", "wanghui")]start_response(status, head_list)read_data = f.read()f.close()return read_data

2.框架已经提供了静态页面的能力。下面对框架进一步完善。

  • web_server.py
#!/usr/bin/env python3
# coding:utf-8from testframe import app
from socket import *
from multiprocessing import Processclass MyWebServer(object):def start_response(self, status, head_list):self.response_head = 'HTTP/1.1 ' + status + ' \r\n'self.response_head = self.response_head.encode()def deal(self, conn):recv_data = conn.recv(1024).decode('utf-8')recv_data_head = recv_data.splitlines()[0]request_method, request_path, http_version = recv_data_head.split()request_path = request_path.split('?')[0]  # 去掉url中的?和之后的参数env = {'request_method': request_method, 'request_path': request_path}# 这里是wsgi接口调用的地方response_body = self.app(env, self.start_response)response_data = self.response_head + b'\r\n' + response_bodyconn.send(response_data)conn.close()def __init__(self, app, port=8000):self.s = socket(AF_INET, SOCK_STREAM)self.s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)self.s.bind(('', port))self.s.listen(1023)self.response_head = ''self.app = appdef start(self):while 1:conn, user_info = self.s.accept()print(user_info, '接入')p = Process(target=self.deal, args=(conn,))p.start()conn.close()  # 进程会复制出一个新的conn,所以这里的conn需要关闭s = MyWebServer(app)
s.start()
  • test_frame.py
import timeclass Application(object):def __init__(self, url_list):self.url_list = url_listdef __call__(self, env, start_response):file_name = env['request_path']if file_name == '/':file_name = '/index.html'try:f = open('.' + file_name, 'rb')except IOError:get_name = url_list.get(file_name, 'say_error')return eval(get_name)(start_response)status = '200 OK'head_list = [("name", "wanghui")]start_response(status, head_list)read_data = f.read()f.close()return read_datadef say_error(start_response):status = '404 error'head_list = [("name", "wanghui")]start_response(status, head_list)return b'<h1>File not found</h1>'def say_time(start_response):status = '200 OK'head_list = [("name", "wanghui")]start_response(status, head_list)return time.ctime().encode()def say_hello(start_response):status = '200 OK'head_list = [("name", "wanghui")]start_response(status, head_list)return b'<h1>hello world</b>'url_list = {'/time.py': 'say_time','/error.py': 'say_error','/hello.py': 'say_hello',}
app = Application(url_list)
  • 此时如果访问http://localhost/time.py,则会动态的将当前时间返回给客户。
  • 不过功能还不够完善,像不支持长连接,还不能支持外部py文件动态解析。

转载于:https://www.cnblogs.com/PrettyTom/p/6759969.html

python之web框架(3):WSGI之web应用完善相关推荐

  1. python十大框架_python 十大web框架排名总结

    0 引言 python在web开发方面有着广泛的应用.鉴于各种各样的框架,对于开发者来说如何选择将成为一个问题.为此,我特此对比较常见的几种框架从性能.使用感受以及应用情况进行一个粗略的分析. 1 D ...

  2. python有哪些web框架_python五大主流web框架

    Django Python框架虽然说是百花齐放,但仍然有那么一家是最大的,它就是Django.要说Django是Python框架里最好的,有人同意也有人 坚决反对,但说Django的文档最完善.市场占 ...

  3. python web框架 多线程_python 简单web框架: Bottle

    基本映射 映射使用在根据不同URLs请求来产生相对应的返回内容.Bottle使用route() 修饰器来实现映射. 1 2 3 4 5 from bottle import route, run@ro ...

  4. Django:Web框架,WSGI,WSGI实现浏览器与服务器通信,路由route,WSGI实现页面访问

    case1: import socketdef handle_request(conn): # 处理数据返回buff = conn.recv(1024)print(buff)conn.send(&qu ...

  5. Python之简易Web框架搭建

    Python之简易Web框架搭建 Web框架介绍 WSGI协议 Web框架开发 项目结构 MyWebServer.py 之前的静态服务器代码 WSGI协议的要求 更新代码 framework.py 返 ...

  6. python主流web框架识别

    想学习web框架,又想熟悉python,问题来了,有没有极简的数据来支撑快速开发,特来研究 不能去研究几十个,没时间,研究主流的即可 Django.Tornado.Flask.Twisted. 所谓网 ...

  7. 用于快速Web开发的5大Python Web框架

    用于快速Web开发的5大Python Web框架 我们将讨论用于快速Web开发的5大Python Web框架.开发这些框架是为了简化网站开发过程.Web框架基本上是Web开发的软件框架.Web框架是一 ...

  8. python框架django书籍_有Python基础,刚接触web框架的Django初学者。

    本文面向:有Python基础,刚接触web框架的Django初学者. 环境:windows7 python3.5.1 pycharm Django 1.10版 pip3 一.Django简介 百度百科 ...

  9. Python学习笔记:Day5 编写web框架

    前言 最近在学习深度学习,已经跑出了几个模型,但Pyhton的基础不够扎实,因此,开始补习Python了,大家都推荐廖雪峰的课程,因此,开始了学习,但光学有没有用,还要和大家讨论一下,因此,写下这些帖 ...

  10. python程序框架的描述_简单介绍Python下自己编写web框架的一些要点

    在正式开始Web开发前,我们需要编写一个Web框架. 为什么不选择一个现成的Web框架而是自己从头开发呢?我们来考察一下现有的流行的Web框架: Django:一站式开发框架,但不利于定制化: web ...

最新文章

  1. sed awk 笔记(二)
  2. vue调试工具vue-devtools安装及使用
  3. 数组的一些与遍历相关的方法总结
  4. java匿名内部类的使用场景_java匿名内部类的使用场景
  5. java主界面设置背景图片_java 窗体设置背景图片问题?(附上登陆界面代码,我想加个背景图片,求大神帮忙改改)...
  6. mysql学习--基本使用
  7. 【数据结构与算法】之深入解析“K个一组翻转链表”的求解思路与算法示例
  8. Android listview addHeaderView 和 addFooterView 详解
  9. python getopterror_python3 getopt用法
  10. “父母双学霸, 生娃是学渣”的科学解释是什么?
  11. [转载] java中final,finally,finalize三者的作用和区别
  12. 【Unity3D】GUI控件
  13. Theano介绍及简单应用
  14. ImageNet数据集的0到999Label对应的类别分别是什么
  15. 在這個神奇的國度找個正常點兒的DNS都很困難
  16. java -jar -xx_java 启动方式 java -jar xx.jar
  17. opencv-python——调用摄像头录制并保存视频
  18. 微信小程序获取urlScheme地址Python版
  19. 基于机智云物联网平台的太阳能热水器控制系统
  20. VINS-mono 论文解读:IMU预积分+Marg边缘化

热门文章

  1. 拿工资,要做差不多的事
  2. OpenJDK8编码代码三合一:x86/Arm/Mips
  3. 亲身经历:如何判断一个字符在a/z之前?
  4. 雄伟到惊世骇俗的黄羊山超级相控阵雷达
  5. Bridge(桥模式)
  6. python定时任务启动与停止_对Python定时任务的启动和停止方法详解
  7. java 字符串索引从0开始_Java程序从指定的索引中搜索子字符串
  8. ipad无法充电怎么办_哈尔滨Ipad死机了维修费用价目表_京宏通讯器材维修培训学校...
  9. 打游戏的计算机,玩游戏还得台式机!高性能游戏台式电脑推荐
  10. python连接pymysql主机目标无响应_Python 解析pymysql模块操作数据库的方法