WSGI原理
web_server:
import socket
import time
import multiprocessing
import re
import mini_frame class WSGIServer(object):
def __init__(self):
# 1.创建socket对象
self.tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 2.设置重复使用地址
self.tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# 3.绑定端口
self.tcp_server_socket.bind(("", 7890))
# 4.设置监听状态
self.tcp_server_socket.listen(128) def clinet_server(self,new_client_socket):
# 1.接受消息
request = new_client_socket.recv(1024).decode("utf-8")
lines = request.splitlines()
# 2.匹配请求网页
request_name = re.match(r"[^/]+(/[^ ]*)",lines[0])
file_name = request_name.group(1)
if file_name == "/":
file_name = "/index.html" # 返回数据给浏览器
if not file_name.endswith(".py"):
# 3.打开文件
try:
f = open("./html" + file_name,"rb")
except Exception as ret:
pass
else:
html_content = f.read() # 4.创建header和body
response_body = html_content
response_header = "HTTP/1.1 200 ok\r\n"
response_header += "Content-Length:%d\r\n" % len(response_body)
response_header += "\r\n"
response = response_header.encode("utf-8") + response_body
# 5.发送
new_client_socket.send(response)
finally:
f.close()
else:
# 如果是.py结尾,则认为是动态页面请求/
env = dict();
env['PATH_INFO'] = file_name
# application(字典, 方法名) 固定用法
body = mini_frame.application(env, self.start_respones_header)
# 拼装header头
header = "HTTP/1.1 %s\r\n" % self.status
for temp in self.headers:
header += "%s:%s\r\n" % (temp[0], temp[1])
header += "\r\n"
# 拼装返回数据
response = header + body
# 发送数据
new_client_socket.send(response.encode("utf-8")) # 6.关闭socket
# new_client_socket.close() def start_respones_header(self, status, headers):
"""接受并保存application传过来的值"""
self.status = status
self.headers = [("server","mini_web v1.0")]
self.headers += headers def run_forever(self):
"""运行"""
while True:
# 5.接收客户地址,创建新socket
new_client_socket,client_addr = self.tcp_server_socket.accept()
# 6.为新客户端服务
p = multiprocessing.Process(target=self.clinet_server,args=(new_client_socket,))
p.start()
# 7.关闭新客户端
new_client_socket.close()
# 7.关闭socket
self.tcp_server_socket.close() def main():
wsgi_server = WSGIServer()
wsgi_server.run_forever() if __name__ == '__main__':
main()
mini_frame.py:
def login():
"""模拟登陆页面"""
return "---login---welcome to python--->time:%s" % time.ctime() def index():
"""模拟主页"""
return "----index----" def application(env, start_respones):
# 调用传过来的方法,传值
start_respones('200 ok',[("Content-Type","text/html;charset=utf-8")])
# 接受字典传过来的数据
file_name = env['PATH_INFO']
# 判断客户请求
if file_name == "/index.py":
return index()
elif file_name == "/login.py":
return login()
else:
return "python 中国"
WSGI原理的更多相关文章
- 想学Python?这里有一个最全面的职位分析
Python从2015年开始,一直处于火爆的趋势,目前Python工程师超越Java.Web前端等岗位,起薪在15K左右,目前不管是小公司还是知名大公司都在热招中. 当然,每个城市对岗位的需求也不尽相 ...
- Python Web开发中,WSGI协议的作用和实现原理详解
首先理解下面三个概念: WSGI:全称是Web Server Gateway Interface,WSGI不是服务器,python模块,框架,API或者任何软件,只是一种规范,描述web server ...
- 理解 OpenStack Swift (2):架构、原理及功能 [Architecture, Implementation and Features]
本系列文章着重学习和研究OpenStack Swift,包括环境搭建.原理.架构.监控和性能等. (1)OpenStack + 三节点Swift 集群+ HAProxy + UCARP 安装和配置 ( ...
- Nginx+uWSGI+Django原理
Python的Web开发中,如果使用Django框架,那么较为成熟稳定的服务器架构一般是Nginx+uWSGI+Django.而为什么一定要三个结合在一起呢?直接使用Django的runserver来 ...
- WSGI规格说明书
PEP 333 这应该是WSGI最权威的文档了 http://www.python.org/dev/peps/pep-3333/ 值翻译了最重要的前面部分,后面读者可以参考 当然文档有些生硬,欢迎 ...
- [Django 1.5] Windows + Apache + wsgi配置
基本步骤 下载安装Apache http://httpd.apache.org/download.cgi. 下载安装modwsgi 模块http://code.google.com/p/modwsgi ...
- 读Flask源代码学习Python--config原理
读Flask源代码学习Python--config原理 个人学习笔记,水平有限.如果理解错误的地方,请大家指出来,谢谢!第一次写文章,发现好累--!. 起因 莫名其妙在第一份工作中使用了从来没有接 ...
- nova的wsgi介绍【WIP】
有关openstack的所有的帖子. https://www.ustack.com/blog/openstack_hacker/#Nova_Workflow 网上已经很多的分析文章了: http:// ...
- tornado+WSGI+Apache
1.原理 2.安装mod_wsgi http://pan.baidu.com/s/1sjsccWH configure的时候会找对应的python脚本,默认是/usr/bin/python 生成mod ...
随机推荐
- QT QML 在qml中自定义信号
服从真理,就能征服一切事物. -- 塞涅卡 实例: 自定义文件 MoveYou.qml: import QtQuick 2.5 import QtQuick.Controls 1.4 import Q ...
- mac远程连接linux 服务器桌面by VNC
为了远程使用Linux服务器,折腾了一个下午.最终看来还是用vnc最简单了. 实验室有两台强劲的Linux服务器用来做研究.之前我一直都是用ssh登到服务器上去码代码,反应速度很快,感觉很不错.但是因 ...
- [转帖]GNU, Free Software and Open Source 自由软件与开源软件
GNU, Free Software and Open Source 自由软件与开源软件 https://blog.csdn.net/icycolawater/article/details/7792 ...
- PAT(B) 1067 试密码(Java)
题目链接:1067 试密码 (20 point(s)) 题目描述 当你试图登录某个系统却忘了密码时,系统一般只会允许你尝试有限多次,当超出允许次数时,账号就会被锁死.本题就请你实现这个小功能. 输入格 ...
- Linux iptables常用防火墙规则
iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT #允许本地回环接口(即运行本机访问本机) iptables -A INPUT -m stat ...
- Linux jdk8 安装
wegt 命令安装 wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.o ...
- Dubbo快速入门 五
5. Dubbo注解版 之前在dubbo配置文件显式编写内容提供者和消费者,官方还提供了了一种注解方式,接下来改造项目 1.服务提供方 dubbo配置文件 将之前手动申明注释掉,添加<dubbo ...
- SpringCloud——eureka集群
目的: 第一种普通方式 第二种方式骚操作 Eureka自我保护机制 Eureka集群搭建 说的通俗易懂一点就是,同一个项目部署在多个服务器上. 当注册中心扛不住高并发的时候,这时候 要用集群来扛: 今 ...
- Qt5 源代码自动跳转
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/nixiaoxianggong/articl ...
- [LOJ6432] [PKUSC2018] 真实排名
题目链接 LOJ:https://loj.ac/problem/6432 Solution 假设我们当前要算\(x\)的答案,分两种情况讨论: \(x\)没被翻倍,那么\([a_x/2,a_x]\)这 ...