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可以配置缓存 ...
随机推荐
- JAVA编程之——反射Reflect
说到反射,首先要说一下Java中的类和对象. 在Java中万事万物皆对象(有两个 例外,一个是普通数据类型,另一个是静态的东西,静态的东西不是对象的,是属于类的). 在Java中,类也是对象,类是ja ...
- Donald Ervin Knuth:最年轻的图灵奖高德纳
高德纳(Donald Ervin Knuth,1938年),美国著名计算机科学家,斯坦福大学电脑系荣誉教授.高德纳教授被誉为现代计算机科学的鼻祖,在计算机科学及数学领域发表了多部 具广泛影响的论文和著 ...
- 实验一 《网络对抗技术》逆向及Bof技术
- Cookie详解、ASP.NET核心知识(7)
无状态的http协议 1.回顾http协议 Http协议是请求响应式的,有请求才有响应,是无状态的,不会记得上次和网页“发生了什么”. 关于http协议的这种特点,黑兔在前面的这三篇博文中进行了详细的 ...
- JS三种消息框的使用
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- python pachong zhuanzai
come from:http://www.cnblogs.com/dyllove98/archive/2013/07/12/3187186.html 先来说一下我们学校的网站: http://jwxt ...
- ClientDataset 三层 var and out arguments must match parameter
将Delphi升级到10.1.2后,从客户端传ClientDataset的Delta数据到服务端程序时,出现var and out arguments must match parameter错 ...
- github 优秀的开源项目
https://github.com/wlcaption/AndroidMarket---- 这是手机应用商店,包含应用的下载,用户中心等内容 https://github.com/wlcaption ...
- pip离线安装
pip freeze > requirements.txt pip download <packages> pip install --no-index --find-links=& ...
- python网络编程-线程队列queue
一:线程queu作用 Python中,queue是线程间最常用的交换数据的形式. 队列两个作用:一个是解耦,一个是提高效率 二:语法 1)队列的类 class queue.Queue(maxsize= ...