Python的web服务器
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>
<br>当年以为六月不过也很平常
<br>当自己真正经历了毕业
<br>才知道偶尔看到六月毕业季等字里所流露的种种想要重温却不敢提及的回忆
<br>毕业了
<br>那个夏天,我的毕业季,我的青春年少
<br>六月
<br>有人笑着说解脱,有人哭着说不舍
<br>那年,
<br>你对我说的你好
<br>在不知不觉中
<br>变成了
<br>再见。
</body>
</html>
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
访问结果:
'''
自定义的符合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)
学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入学习交流群
626062078,我们一起学Python!
Python的web服务器的更多相关文章
- Python搭建Web服务器,与Ajax交互,接收处理Get和Post请求的简易结构
用python搭建web服务器,与ajax交互,接收处理Get和Post请求:简单实用,没有用框架,适用于简单需求,更多功能可进行扩展. python有自带模块BaseHTTPServer.CGIHT ...
- python写web服务器
#coding = utf-8 from http.server import BaseHTTPRequestHandler, HTTPServer class RequestHandler(Base ...
- (转)Python的web服务器
1.浏览器请求动态页面过程 2.WSGI Python Web Server Gateway Interface (或简称 WSGI,读作“wizgy”). WSGI允许开发者将选择web框架和web ...
- python之Web服务器案例
HTTP协议简介 1. 使用谷歌/火狐浏览器分析 在Web应用中,服务器把网页传给浏览器,实际上就是把网页的HTML代码发送给浏览器,让浏览器显示出来.而浏览器和服务器之间的传输协议是HTTP,所以: ...
- Python基础Web服务器案例
一.WSGI 1.PythonWeb服务器网关接口(Python Web Server Gateway Interface,缩写为WSGI) 是Python应用程序或框架和Web服务器之间的一种接口, ...
- Python——轻量级web服务器flask的学习
前言: 根据工程需要,开始上手另一个python服务器---flask,flask是一个轻量级的python服务器,简单易用.将我的学习过程记录下来,有新的知识会及时补充. 记录只为更好的分享~ 正文 ...
- python对web服务器做压力测试并做出图形直观显示
压力测试有很多工具啊.apache的,还有jmeter, 还有loadrunner,都比较常用. 其实你自己用python写的,也足够用. 压力测试过程中要统计时间. 比如每秒的并发数,每秒的最大响应 ...
- Python 简单web服务器的实现
import re import socket def service_cilent(new_socket): request = new_socket.recv(1024).decode(" ...
- python开发web服务器——搭建简易网站
参看:https://blog.csdn.net/baidu_35085676/article/details/69807145
随机推荐
- URL 编码规则
规则: 1.将空格转换为加号(+) 2.对0-9.a-z.A-Z之间的字符保持不变 3.对于所有其他的字符,用这个字符的当前当前字符集编码在内存中的十六进制格式表示,并在每一个字节前加上一个百分号(% ...
- 【BZOJ5301】【CQOI2018】异或序列(莫队)
[BZOJ5301][CQOI2018]异或序列(莫队) 题面 BZOJ 洛谷 Description 已知一个长度为 n 的整数数列 a[1],a[2],-,a[n] ,给定查询参数 l.r ,问在 ...
- BZOJ4597:[SHOI2016]随机序列——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4597 你的面前有N个数排成一行.分别为A1, A2, … , An.你打算在每相邻的两个 Ai和 ...
- BZOJ3922 Karin的弹幕 【线段树】
题目链接 BZOJ3922 题解 考虑暴力,修改\(O(1)\),查询\(O(\frac{n}{d})\) 考虑线段树,如果对每种差值建一棵线段树,修改\(O(nlogn)\),查询\(O(logn) ...
- 实验五 TCP传输及加解密
北京电子科技学院(BESTI) 实 验 报 告 课程:Java程序设计 班级:1353 姓名:陈巧然 ...
- [BJOI2018]求和
link 其实可以用$sum(i,j)$表示从$i$到$1$的$k$次方的值,然后就是$lca$的基本操作 注意,能一起干的事情就一起搞,要不会超时 #include<iostream> ...
- Nginx 配置解析
概述:本篇文章主要对Nginx配置文件中一些常用配置进行了讲解,和如何使用Docker进行安装Nginx.因为该文章是回首在工作闲暇之余整理的,还有待完善,如果有疑义和更好的建议的朋友可以留言给我. ...
- 获取 exception 对象的字符串形式(接口服务返回给调用者)
工具类: package com.taotao.common.utils; import java.io.PrintWriter; import java.io.StringWriter; publi ...
- 洛谷:P2922 [USACO08DEC]秘密消息(Trie树)
P2922 [USACO08DEC]秘密消息Secret Message 题目链接:https://www.luogu.org/problemnew/show/P2922 题目描述 贝茜正在领导奶牛们 ...
- sudoers文件配置
http://note.drx.tw/2008/01/linuxsudo.html foobar ALL=(ALL) ALL 現在讓我們來看一下那三個 ALL 到底是什麼意思.第一個 ALL 是指網路 ...