python之web框架(3):WSGI之web应用完善
python之web框架(3):WSGI之web应用完善
1.上篇的web框架太low,只能实现回应固定页面。现在将它进行完善。首先将wsgi和web服务器进行分离,并给予它回复静态页面的能力。
- web_server.py
#!/usr/bin/env python3
# coding:utf-8
from test_frame import app
from socket import *
from multiprocessing import Process
class 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_body
conn.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-8
from testframe import app
from socket import *
from multiprocessing import Process
class 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_body
conn.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 = app
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(app)
s.start()
- test_frame.py
import time
class Application(object):
def __init__(self, url_list):
self.url_list = url_list
def __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_data
def 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文件动态解析。
python之web框架(3):WSGI之web应用完善的更多相关文章
- python web框架 django wsgi 理论
django wsgi python有个自带的wsgi模块 可以写自定义web框架 用wsgi在内部创建socket对象就可以了 自己只写处理函数就可以了django只是web框架 他也不负责写soc ...
- Python web框架开发 - WSGI协议
浏览器进行http请求的时候,不单单会请求静态资源,还可能需要请求动态页面. 那么什么是静态资源,什么是动态页面呢? 静态资源 : 例如html文件.图片文件.css.js文件等,都可以算是静态资源 ...
- [py]彻底细究web框架的wsgi+逻辑处理模块
wsgi逻辑结构初探 参考: 这里图很精彩,wsgi写的不错 web框架 = wsgi+逻辑处理app 接收请求,返回对应的内容 python wsgiref实现了wsgi规范. from wsgir ...
- Python 之WEB框架
wsgi模块实现socketPython web框架: - 自己实现socket 代表:Tornado - 基于wsgi(一种规范,统一接口) 代表: Django 自己开发web框架(基于wsgi) ...
- Python Web 应用:WSGI基础
在Django,Flask,Bottle和其他一切Python web 框架底层的是Web Server Gateway Interface,简称WSGI.WSGI对Python来说就像 Servle ...
- Python全栈开发-web框架之django
一:web框架 什么是web框架? Web应用框架(Web application framework)是一种开发框架,用来支持动态网站.网络应用程序及网络服务的开发.这种框架有助于减轻网页开发时共通 ...
- Python学习 - 编写一个简单的web框架(一)
自己动手写一个web框架,因为我是菜鸟,对于python的一些内建函数不是清楚,所以在写这篇文章之前需要一些python和WSGI的预备知识,这是一系列文章.这一篇只实现了如何处理url. 参考这篇文 ...
- python运维开发(十七)----jQuery续(示例)web框架django
内容目录: jQuery示例 前端插件 web框架 Django框架 jQuery示例 dom事件绑定,dom绑定在form表单提交按钮地方都会绑定一个onclick事件,所有查看网站的人都能看到代码 ...
- python django基础一web框架的本质
web框架的本质就是一个socket服务端,而浏览器就是一个socker客户端,基于请求做出相应,客户端先请求,服务器做出对应响应 按照http协议的请求发送,服务器按照http协议来相应,这样的通信 ...
- python web框架 django 工程 创建 目录介绍
# 创建Django工程django-admin startproject [工程名称] 默认创建django 项目都会自带这些东西 django setting 配置文件 django可以配置缓存 ...
随机推荐
- 转:UINavigationBar返回上一级出现nested pop animation can result in corrupted navigation bar
[self.navigationController popViewControllerAnimated:NO]; 出现上面的错误是因为pop的时候要确保先让本页面加载完成,即如果在viewDidLo ...
- 秋色园QBlog技术原理解析:性能优化篇:缓存总有失效时,构造持续的缓存方案(十四)
转载自:http://www.cyqdata.com/qblog/article-detail-38993 文章回顾: 1: 秋色园QBlog技术原理解析:开篇:整体认识(一) --介绍整体文件夹和文 ...
- c++虚函数&重写
虚函数是C++中实现多态的一种方法,父类A的一个函数声明为虚函数,在子类B中覆盖定义之后,当在调用的时候使用A*a=new B(),此时调用对应的那个虚函数的名字,则会执行B中的函数.当父类中没有定义 ...
- Git为某个域名设置代理
打开Git 配置文件 vi ~/.gitconfig 添加如下配置: [http "https://github.com/"] proxy = http://127.0.0.1:1 ...
- js生成接口请求参数签名加密
js生成接口请求参数签名加密 定义规则:将所有参数字段按首字母排序, 拼接成key1 = value1 & key2 = value2的格式,再在末尾拼接上key = appSecret, 再 ...
- Intellij IDEA 2017 控制台打印换行
Intellij IDEA 2017 控制台打印的内容超过屏幕宽度了,请问怎么自动换行? 记得重启idea
- python scrapy 基本操作演示代码
# -*- coding: utf-8 -*- import scrapy # from quotetutorial.items import QuoteItem from quotetutorial ...
- 使用jQuery仿淘宝商城多格焦点图滚动切换效果
1.效果及功能说明 图片滚动切换特效,高仿2012淘宝商城首页多格子焦点图切换,鼠标滑过焦点图片各个格子区域聚光灯效果展示 2.实现原理 在显示div的下面有一个按钮条在鼠标触及到按钮的时候会改变那妞 ...
- TED_Topic6:How to raise a black son in America
By Clint Smith As kids, we all get advice from parents and teachers that seems strange, even confusi ...
- Hadoop2.6.0在CentOS 7中的集群搭建
我这边给出我的集群环境是由一台主节点master和三台从节点slave组成: master 192.168.1.2 slave1 192.168.1.3 slave2 ...