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. Host is not allowed to connect to this MySQL server---------------->windows10

    错误,数据库无法远程连接. 第一步,关闭本地防火墙 注意: 两台连接的机器都需要关闭 第二步,两台机器互相ping,看是否可以互相访问 丢失为零,意为可以连接. 第三步登录数据库 第四步,切换,数据库 ...

  2. 关于DOM级别的一些问题,DOM0,DOM1,DOM2

    之前看书没太注意这个问题,直到我今天看书看到一个DOM0级,于是我就在群里问了下各个级别的意思区别.. 首先我们的确定标准了是没有DOM0级的.在平时阅读的时候可能会读到DOM0级(DOM Level ...

  3. highChart图表

    Highcharts 是一个用纯JavaScript编写的一个图表库, 能够很简单便捷的在web网站或是web应用程序添加有交互性的图表,并且免费提供给个人学习.个人网站和非商业用途使用.HighCh ...

  4. leaflet入门(五)API翻译(下)

    L.PointConstructor(函数构造器)Properties(属性)Methods(方法) L.BoundsConstructor(函数构造器)Properties(属性)Methods(方 ...

  5. Django-配置、静态文件与路由

    -----配置文件 1.BASE_DIR BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))   2.DEBU ...

  6. MIT Molecular Biology 笔记3 DNA同源重组

    视频  https://www.bilibili.com/video/av7973580?from=search&seid=16993146754254492690 教材 Molecular ...

  7. git windows下载安装 (git命令)

    Set up git At the heart of GitHub is an open source version control system (VCS) called Git. Git is ...

  8. linux 配置阿里云yum库

    备份当前yum库 mv /etc/yum.repos.d /etc/yum.repos.d.backup4comex 新建yum源配置目录 mkdir /etc/yum.repos.d 设置阿里yum ...

  9. noip第16课作业

    1. 猴子吃桃 [问题描述] 猴子第一天摘了若干个桃子,当即吃了一半,还不解馋,又多吃了一个:第二天,吃剩下的桃子的一半,还不过瘾,又多吃了一个:以后每天都吃前一天剩下的一半多一个,到第10天想再吃时 ...

  10. CALayer-CAShapeLayer/CAGradientLayer

    参考博客 CAShapeLayer http://blog.csdn.net/yongyinmg/article/details/38755955 CAShapeLayer和CAGradientLay ...