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原理的更多相关文章

  1. 想学Python?这里有一个最全面的职位分析

    Python从2015年开始,一直处于火爆的趋势,目前Python工程师超越Java.Web前端等岗位,起薪在15K左右,目前不管是小公司还是知名大公司都在热招中. 当然,每个城市对岗位的需求也不尽相 ...

  2. Python Web开发中,WSGI协议的作用和实现原理详解

    首先理解下面三个概念: WSGI:全称是Web Server Gateway Interface,WSGI不是服务器,python模块,框架,API或者任何软件,只是一种规范,描述web server ...

  3. 理解 OpenStack Swift (2):架构、原理及功能 [Architecture, Implementation and Features]

    本系列文章着重学习和研究OpenStack Swift,包括环境搭建.原理.架构.监控和性能等. (1)OpenStack + 三节点Swift 集群+ HAProxy + UCARP 安装和配置 ( ...

  4. Nginx+uWSGI+Django原理

    Python的Web开发中,如果使用Django框架,那么较为成熟稳定的服务器架构一般是Nginx+uWSGI+Django.而为什么一定要三个结合在一起呢?直接使用Django的runserver来 ...

  5. WSGI规格说明书

    PEP 333 这应该是WSGI最权威的文档了  http://www.python.org/dev/peps/pep-3333/  值翻译了最重要的前面部分,后面读者可以参考 当然文档有些生硬,欢迎 ...

  6. [Django 1.5] Windows + Apache + wsgi配置

    基本步骤 下载安装Apache http://httpd.apache.org/download.cgi. 下载安装modwsgi 模块http://code.google.com/p/modwsgi ...

  7. 读Flask源代码学习Python--config原理

    读Flask源代码学习Python--config原理 个人学习笔记,水平有限.如果理解错误的地方,请大家指出来,谢谢!第一次写文章,发现好累--!. 起因   莫名其妙在第一份工作中使用了从来没有接 ...

  8. nova的wsgi介绍【WIP】

    有关openstack的所有的帖子. https://www.ustack.com/blog/openstack_hacker/#Nova_Workflow 网上已经很多的分析文章了: http:// ...

  9. tornado+WSGI+Apache

    1.原理 2.安装mod_wsgi http://pan.baidu.com/s/1sjsccWH configure的时候会找对应的python脚本,默认是/usr/bin/python 生成mod ...

随机推荐

  1. Spring中的乱码问题

    最近发现一个问题, 中文编码保存到数据库里显示正确, 打印出来却是一串问号, 然后怀疑是平台默认编码的问题, locale命令显示是UTF-8正常, 然后单独编写一个java文件, 编译然后Java命 ...

  2. K8S从入门到放弃系列-(16)Kubernetes集群Prometheus-operator监控部署

    Prometheus Operator不同于Prometheus,Prometheus Operator是 CoreOS 开源的一套用于管理在 Kubernetes 集群上的 Prometheus 控 ...

  3. vue 项目之后生成的 dist 文件该怎么在本地启动运行

    简单高效 npm i -g servecd distserve

  4. 在windows中使用PuTTy上传下载文件和目录

    打开windows的cmd,使用cd命令切换到PuTTy安装目录 C:\Users\NUC>cd C:\Program Files\PuTTY 在cmd中使用pscp命令上传下载文件 windo ...

  5. java之struts2之文件下载

    1.在实际应用开发中,文件下载功能也非常常见. 2.最简单的文件下载方式是通过超链接来进行文件下载: <body> <a href="download/s.txt" ...

  6. nginx在Windows环境安装

    nginx介绍 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP服务器进行网站的发布处理,另外 ...

  7. 7 批量查询mget、批量修改bulk

    注意:当执行多条数据查询.增删改时,一定要用mget.bulk,提升性能,减少网络传输   mget   回顾:查询单个文档 GET /beauties/my/2   mget 查询多个文档: 不同 ...

  8. windows下pyinstaller打包踩坑记录

    示例: 需要打包的是 ReadConfig.py 文件,同文件夹下调用了Interface.py文件,ui文件夹下调用了 Ui_config.py和Ui_Error.py文件,Interface.py ...

  9. 【洛谷 P2408】 不同子串个数(后缀自动机)

    题目链接 裸体就是身体. 建出\(SAM\),\(DAG\)上跑\(DP\),\(f[u]=1+\sum_{(u,v)\in DAG}f[v]\) 答案为\(f[1]-1\)(因为根节点没有字符) # ...

  10. 【转载】C#中List集合使用Last方法获取最后一个元素

    在C#的List集合操作过程中,如果要获取List集合中的最后一个元素对象,则一般会先通过获取到list集合的个数Count属性,然后再使用索引的方式获取到该集合的最后一个位置的元素信息.其实在Lis ...