1.浏览器请求动态页面过程

2.WSGI

Python Web Server Gateway Interface (或简称 WSGI,读作“wizgy”)。

WSGI允许开发者将选择web框架和web服务器分开。可以混合匹配web服务器和web框架,选择一个适合的配对。比如,可以在Gunicorn 或者 Nginx/uWSGI 或者 Waitress上运行 Django, Flask, 或 Pyramid。真正的混合匹配,得益于WSGI同时支持服务器和架构.

web服务器必须具备WSGI接口,所有的现代Python Web框架都已具备WSGI接口,它让你不对代码作修改就能使服务器和特点的web框架协同工作。

WSGI由web服务器支持,而web框架允许你选择适合自己的配对,但它同样对于服务器和框架开发者提供便利使他们可以专注于自己偏爱的领域和专长而不至于相互牵制。其他语言也有类似接口:java有Servlet API,Ruby 有 Rack。

3.定义WSGI接口

WSGI接口定义非常简单,它只要求Web开发者实现一个函数,就可以响应HTTP请求。我们来看一个最简单的Web版本的“Hello World!”:

def application(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return 'Hello World!'

上面的 application( ) 函数就是符合WSGI标准的一个HTTP处理函数,它接收两个参数:

  • environ:一个包含所有HTTP请求信息的dict对象;
  • start_response:一个发送HTTP响应的函数。

整个application( )函数本身没有涉及到任何解析HTTP的部分,也就是说,把底层web服务器解析部分和应用程序逻辑部分进行了分离,这样开发者就可以专心做一个领域了.

application( )函数必须由WSGI服务器来调用。有很多符合WSGI规范的服务器。而我们此时的web服务器项目的目的就是做一个极可能解析静态网页还可以解析动态网页的服务器

实现代码:

import time,multiprocessing,socket,os,re

class MyHttpServer(object):

 def __init__(self):
serveSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.serveSocket = serveSocket
self.HTMLPATH = './html' def bind(self,port=8000):
self.serveSocket.bind(('',port)) def start(self):
self.serveSocket.listen()
while True:
clientSocket, clientAddr = self.serveSocket.accept()
print(clientSocket)
multiprocessing.Process(target=self.serveHandler, args=(clientSocket, clientAddr)).start()
clientSocket.close() def serveHandler(self,clientSocket,clientAddr):
try:
recvData = clientSocket.recv(1024).decode('gbk')
fileName = re.split(r' +', recvData.splitlines()[0])[1]
filePath = self.HTMLPATH if fileName.endswith('.py'):
try:
pyname=fileName[1:-3]
# 导入
pyModule = __import__(pyname) env={}
responseBody = pyModule.application(env,self.startResponse)
responseLine = self.responseLine
responseHeader = self.responseHeader
except ImportError:
responseLine = 'HTTP/1.1 404 NOT FOUND'
responseHeader = 'Server: ererbai' + os.linesep
responseHeader += 'Date: %s' % time.ctime()
responseBody = '<h1>很抱歉,服务器中找不到你想要的内容<h1>'
else:
if '/'== fileName:
filePath += '/index.html'
else:
filePath += fileName try:
file = None
file =open(filePath,'r',encoding='gbk')
responseBody = file.read() responseLine = 'HTTP/1.1 200 OK'
responseHeader = 'Server: ererbai' + os.linesep
responseHeader += 'Date:%s' % time.ctime()
except FileNotFoundError:
responseLine = 'HTTP/1.1 404 NOT FOUND'
responseHeader = 'Server: ererbai' + os.linesep
responseHeader += 'Date:%s' % time.ctime()
responseBody = '很抱歉,服务器中找不到你想要的内容' finally:
if (file!=None) and (not file.closed):
file.close() except Exception as ex:
responseLine = 'HTTP/1.1 500 ERROR'
responseHeader = 'Server: ererbai' + os.linesep
responseHeader += 'Date: %s' % time.ctime()
responseBody = '服务器正在维护中,请稍后再试。%s'%ex
finally:
senData = responseLine + os.linesep + responseHeader + os.linesep + os.linesep + responseBody
print(senData)
senData = senData.encode('gbk')
clientSocket.send(senData)
if (clientSocket!=None) and ( not clientSocket._closed):
clientSocket.close() def startResponse(self,status,responseHeaders):
self.responseLine = status
self.responseHeader = ''
for k,v in responseHeaders:
kv = k + ':' + v + os.linesep
self.responseHeader += kv if __name__ == '__main__':
server = MyHttpServer()
server.bind(8000)
server.start()

服务器中存在的html的文件:

  • index.html
<html>
<head>
<title>首页-毕业季</title>
<meta http-equiv=Content-Type content="text/html;charset=gbk"> </head>
<body>我们仍需共生命的慷慨与繁华相爱,即使岁月以刻薄和荒芜相欺。
</body>
</html>
  • biye.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="gbk">
<title>毕业季</title>
</head>
<body>![](http://localhost:51017/day18/html/biyeji.png)
<br>当年以为六月不过也很平常
<br>当自己真正经历了毕业
<br>才知道偶尔看到六月毕业季等字里所流露的种种想要重温却不敢提及的回忆
<br>毕业了
<br>那个夏天,我的毕业季,我的青春年少
<br>六月
<br>有人笑着说解脱,有人哭着说不舍
<br>那年,
<br>你对我说的你好
<br>在不知不觉中
<br>变成了
<br>再见。 </body>
</html>

biyeji.png

mytime.py文件

import time
def application(env,startResponse):
status = 'HTTP/1.1 200 OK'
responseHeaders = [('Server','bfe/1.0.8.18'),('Date','%s'%time.ctime()),('Content-Type','text/plain')]
startResponse(status,responseHeaders) responseBody = str(time.ctime())
return responseBody

访问结果:

首页

biye.html

mytime.py
'''
自定义的符合wsgi的框架
'''
import time class Application(object):
def __init__(self, urls):
'''框架初始化的时候需要获取路由列表'''
self.urls = urls def __call__(self, env, startResponse):
'''
判断是静态资源还是动态资源。
设置状态码和响应头和响应体
:param env:
:param startResponse:
:return:
'''
# 从请求头中获取文件名
fileName = env.get('PATH_INFO') # 判断静态还是动态
if fileName.startwith('/static'):
fileName = fileName[7:]
if '/' == fileName:
filePath += '/index.html'
else:
filePath += fileName
try:
file = None
file = open(filePath, 'r', encoding='gbk')
responseBody = file.read()
status = 'HTTP/1.1 200 OK'
responseHeaders = [('Server', 'ererbai')] except FileNotFoundError:
status = 'HTTP/1.1 404 Not Found'
responseHeaders = [('Server', 'ererbai')]
responseBody = '<h1>找不到<h1>'
finally:
startResponse(status, responseHeaders)
if (file != None) and (not file.closed):
file.close()
else:
isHas = False # 表示请求的名字是否在urls中,True:存在,False:不存在
for url, func in self.urls:
if url == fileName:
responseBody = func(env, startResponse)
isHas = True
break
if isHas == False:
status = 'HTTP/1.1 404 Not Found'
responseHeaders = [('Server', 'ererbai')]
responseBody = '<h1>找不到<h1>'
startResponse(status, responseHeaders)
return responseBody def mytime(env, startResponse):
status = 'HTTP/1.1 200 OK'
responseHeaders = [('Server', 'time')]
startResponse(status, responseHeaders)
responseBody = str(time.ctime())
return responseBody def mynews(env, startResponse):
status = 'HTTP/1.1 200 OK'
responseHeaders = [('Server', 'news')]
startResponse(status, responseHeaders)
responseBody = str('xx新闻')
return responseBody '''路由列表'''
urls = [
('/mytime', mytime),
('/mynews', mynews)
] application = Application(urls)

(转)Python的web服务器的更多相关文章

  1. Python搭建Web服务器,与Ajax交互,接收处理Get和Post请求的简易结构

    用python搭建web服务器,与ajax交互,接收处理Get和Post请求:简单实用,没有用框架,适用于简单需求,更多功能可进行扩展. python有自带模块BaseHTTPServer.CGIHT ...

  2. python写web服务器

    #coding = utf-8 from http.server import BaseHTTPRequestHandler, HTTPServer class RequestHandler(Base ...

  3. python之Web服务器案例

    HTTP协议简介 1. 使用谷歌/火狐浏览器分析 在Web应用中,服务器把网页传给浏览器,实际上就是把网页的HTML代码发送给浏览器,让浏览器显示出来.而浏览器和服务器之间的传输协议是HTTP,所以: ...

  4. Python的web服务器

    1.浏览器请求动态页面过程 2.WSGI Python Web Server Gateway Interface (或简称 WSGI,读作“wizgy”). WSGI允许开发者将选择web框架和web ...

  5. Python基础Web服务器案例

    一.WSGI 1.PythonWeb服务器网关接口(Python Web Server Gateway Interface,缩写为WSGI) 是Python应用程序或框架和Web服务器之间的一种接口, ...

  6. Python——轻量级web服务器flask的学习

    前言: 根据工程需要,开始上手另一个python服务器---flask,flask是一个轻量级的python服务器,简单易用.将我的学习过程记录下来,有新的知识会及时补充. 记录只为更好的分享~ 正文 ...

  7. python对web服务器做压力测试并做出图形直观显示

    压力测试有很多工具啊.apache的,还有jmeter, 还有loadrunner,都比较常用. 其实你自己用python写的,也足够用. 压力测试过程中要统计时间. 比如每秒的并发数,每秒的最大响应 ...

  8. Python 简单web服务器的实现

    import re import socket def service_cilent(new_socket): request = new_socket.recv(1024).decode(" ...

  9. python开发web服务器——搭建简易网站

    参看:https://blog.csdn.net/baidu_35085676/article/details/69807145

随机推荐

  1. openstack网络基本概念(转)

    OpenStack的Neutron能够管理OpenStack环境中的虚拟 网络基础设施(VNI).和物理网络基础设施(PNI). OpenStack的Neutron同意租户创建虚拟网络拓扑结构.包括的 ...

  2. idea设置字体大小

    第一次玩儿idea,也是个新手小白,甚是惭愧,也是一步步慢慢摸索,下面我们按照步骤一步步操作 就可以了. 1.首先,先设置代码的字体大小: 2.设置周围菜单栏的字体大小: 3.设置控制台的字体大小:

  3. C# 编码标准(一)

    一直想写一个自己用的代码标准,经过一段时间的优秀开源源码的观察和看其他人写的标准,感觉好的代码给人感觉就是舒服,也非常重要.所以把它们记录归纳总结,以备以后忘记,另外平时写代码的时候可以拿来参考下.下 ...

  4. 未找到导入的项目“C:\Program Files\MSBuild\Microsoft\Silverlight\v5.0\Microsoft.Silverlight.CSharp.targets”。

    问题描述: 原先创建的Silverlight程序,后来系统重装了,再打开Silverlight程序时提示:C:\Users\yzg\Desktop\ocr\TJSilverlight\TJSilver ...

  5. app.json

    APP.JSON 文件来对微信小程序进行全局配置,决定页面文件的路径.窗口表现.设置网络超时时间.设置多 TAB 等.相当于iOS开发中的AppDelegate 注意该文件不可添加任何注释. {   ...

  6. mac终端的命令都失效的解决方法

    step1. 在terminal里面输入: export PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin&qu ...

  7. LoadRunner改脚本

    加action,不支持嵌套大括号 Action(){int iCt = 0; iCt = lr_output_message("abcdefg"); iCt = funA(3,5) ...

  8. MapGIS计算瓦片数据集

    https://www.docin.com/p-2103834433.html

  9. POJ 1191棋盘分割问题

    棋盘分割问题 题目大意,将一个棋盘分割成k-1个矩形,每个矩形都对应一个权值,让所有的权值最小求分法 很像区间DP,但是也不能说就是 我们只要想好了一个怎么变成两个,剩下的就好了,但是怎么变,就是变化 ...

  10. AngularJS $scope 继承性 作用 生命周期

    一.基本概念 作用域是一个指向应用模型的对象,相当于MVVM中的ViewModel,能绑定数据(属性)和行为(方法),能监控表达式和传递事件,是实现双向绑定的基础,是应用在 HTML (视图) 和 J ...