线程、进程、协程实现web静态服务器
1.Web静态服务器–多进程
#coding=utf-8
import socket
import re
import multiprocessing
class WSGIServer(object):
def __init__(self, server_address):
# 创建一个tcp套接字
self.listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 允许立即使用上次绑定的port
self.listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# 绑定本地信息
self.listen_socket.bind(server_address)
# 变为被动,并制定队列的长度
self.listen_socket.listen(128)
def serve_forever(self):
"循环运行web服务器,等待客户端的链接并为客户端服务"
while True:
# 等待新客户端到来
client_socket, client_address = self.listen_socket.accept()
print(client_address) # for test
new_process = multiprocessing.Process(target=self.handleRequest, args=(client_socket,))
new_process.start()
# 因为子进程已经复制了父进程的套接字等资源,所以父进程调用close不会将他们对应的这个链接关闭的
client_socket.close()
def handleRequest(self, client_socket):
"用一个新的进程,为一个客户端进行服务"
recv_data = client_socket.recv(1024).decode('utf-8')
print(recv_data)
requestHeaderLines = recv_data.splitlines()
for line in requestHeaderLines:
print(line)
request_line = requestHeaderLines[0]
get_file_name = re.match("[^/]+(/[^ ]*)", request_line).group(1)
print("file name is ===>%s" % get_file_name) # for test
if get_file_name == "/":
get_file_name = DOCUMENTS_ROOT + "/index.html"
else:
get_file_name = DOCUMENTS_ROOT + get_file_name
print("file name is ===2>%s" % get_file_name) # for test
try:
f = open(get_file_name, "rb")
except IOError:
response_header = "HTTP/1.1 404 not found\r\n"
response_header += "\r\n"
response_body = "====sorry ,file not found===="
else:
response_header = "HTTP/1.1 200 OK\r\n"
response_header += "\r\n"
response_body = f.read()
f.close()
finally:
client_socket.send(response_header.encode('utf-8'))
client_socket.send(response_body)
client_socket.close()
# 设定服务器的端口
SERVER_ADDR = (HOST, PORT) = "", 8888
# 设置服务器服务静态资源时的路径
DOCUMENTS_ROOT = "./html"
def main():
httpd = WSGIServer(SERVER_ADDR)
print("web Server: Serving HTTP on port %d ...\n" % PORT)
httpd.serve_forever()
if __name__ == "__main__":
main()
2.Web静态服务器–多线程
#coding=utf-8
import socket
import re
import threading
class WSGIServer(object):
def __init__(self, server_address):
# 创建一个tcp套接字
self.listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 允许立即使用上次绑定的port
self.listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# 绑定
self.listen_socket.bind(server_address)
# 变为被动,并制定队列的长度
self.listen_socket.listen(128)
def serve_forever(self):
"循环运行web服务器,等待客户端的链接并为客户端服务"
while True:
# 等待新客户端到来
client_socket, client_address = self.listen_socket.accept()
print(client_address)
new_process = threading.Thread(target=self.handleRequest, args=(client_socket,))
new_process.start()
# 因为线程是共享同一个套接字,所以主线程不能关闭,否则子线程就不能再使用这个套接字了
# client_socket.close()
def handleRequest(self, client_socket):
"用一个新的进程,为一个客户端进行服务"
recv_data = client_socket.recv(1024).decode('utf-8')
print(recv_data)
requestHeaderLines = recv_data.splitlines()
for line in requestHeaderLines:
print(line)
request_line = requestHeaderLines[0]
get_file_name = re.match("[^/]+(/[^ ]*)", request_line).group(1)
print("file name is ===>%s" % get_file_name) # for test
if get_file_name == "/":
get_file_name = DOCUMENTS_ROOT + "/index.html"
else:
get_file_name = DOCUMENTS_ROOT + get_file_name
print("file name is ===2>%s" % get_file_name) # for test
try:
f = open(get_file_name, "rb")
except IOError:
response_header = "HTTP/1.1 404 not found\r\n"
response_header += "\r\n"
response_body = "====sorry ,file not found===="
else:
response_header = "HTTP/1.1 200 OK\r\n"
response_header += "\r\n"
response_body = f.read()
f.close()
finally:
client_socket.send(response_header.encode('utf-8'))
client_socket.send(response_body)
client_socket.close()
# 设定服务器的端口
SERVER_ADDR = (HOST, PORT) = "", 8888
# 设置服务器服务静态资源时的路径
DOCUMENTS_ROOT = "./html"
def main():
httpd = WSGIServer(SERVER_ADDR)
print("web Server: Serving HTTP on port %d ...\n" % PORT)
httpd.serve_forever()
if __name__ == "__main__":
main()
3.Web静态服务器–非堵塞模式(并发原理)
import time
import socket
import sys
import re
class WSGIServer(object):
"""定义一个WSGI服务器的类"""
def __init__(self, port, documents_root):
# 1. 创建套接字
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 2. 绑定本地信息
self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server_socket.bind(("", port))
# 3. 变为监听套接字
self.server_socket.listen(128)
self.server_socket.setblocking(False)
self.client_socket_list = list()
self.documents_root = documents_root
def run_forever(self):
"""运行服务器"""
# 等待对方链接
while True:
# time.sleep(0.5) # for test
try:
new_socket, new_addr = self.server_socket.accept()
except Exception as ret:
print("-----1----", ret) # for test
else:
new_socket.setblocking(False)
self.client_socket_list.append(new_socket)
for client_socket in self.client_socket_list:
try:
request = client_socket.recv(1024).decode('utf-8')
except Exception as ret:
print("------2----", ret) # for test
else:
if request:
self.deal_with_request(request, client_socket)
else:
client_socket.close()
self.client_socket_list.remove(client_socket)
print(self.client_socket_list)
def deal_with_request(self, request, client_socket):
"""为这个浏览器服务器"""
if not request:
return
request_lines = request.splitlines()
for i, line in enumerate(request_lines):
print(i, line)
# 提取请求的文件(index.html)
# GET /a/b/c/d/e/index.html HTTP/1.1
ret = re.match(r"([^/]*)([^ ]+)", request_lines[0])
if ret:
print("正则提取数据:", ret.group(1))
print("正则提取数据:", ret.group(2))
file_name = ret.group(2)
if file_name == "/":
file_name = "/index.html"
# 读取文件数据
try:
f = open(self.documents_root+file_name, "rb")
except:
response_body = "file not found, 请输入正确的url"
response_header = "HTTP/1.1 404 not found\r\n"
response_header += "Content-Type: text/html; charset=utf-8\r\n"
response_header += "Content-Length: %d\r\n" % (len(response_body))
response_header += "\r\n"
# 将header返回给浏览器
client_socket.send(response_header.encode('utf-8'))
# 将body返回给浏览器
client_socket.send(response_body.encode("utf-8"))
else:
content = f.read()
f.close()
response_body = content
response_header = "HTTP/1.1 200 OK\r\n"
response_header += "Content-Length: %d\r\n" % (len(response_body))
response_header += "\r\n"
# 将header返回给浏览器
client_socket.send( response_header.encode('utf-8') + response_body)
# 设置服务器服务静态资源时的路径
DOCUMENTS_ROOT = "./html"
def main():
"""控制web服务器整体"""
# python3 xxxx.py 7890
if len(sys.argv) == 2:
port = sys.argv[1]
if port.isdigit():
port = int(port)
else:
print("运行方式如: python3 xxx.py 7890")
return
print("http服务器使用的port:%s" % port)
http_server = WSGIServer(port, DOCUMENTS_ROOT)
http_server.run_forever()
if __name__ == "__main__":
main()
4. Web静态服务器–epoll
import socket
import time
import sys
import re
import select
class WSGIServer(object):
"""定义一个WSGI服务器的类"""
def __init__(self, port, documents_root):
# 1. 创建套接字
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 2. 绑定本地信息
self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server_socket.bind(("", port))
# 3. 变为监听套接字
self.server_socket.listen(128)
self.documents_root = documents_root
# 创建epoll对象
self.epoll = select.epoll()
# 将tcp服务器套接字加入到epoll中进行监听
self.epoll.register(self.server_socket.fileno(), select.EPOLLIN|select.EPOLLET)
# 创建添加的fd对应的套接字
self.fd_socket = dict()
def run_forever(self):
"""运行服务器"""
# 等待对方链接
while True:
# epoll 进行 fd 扫描的地方 -- 未指定超时时间则为阻塞等待
epoll_list = self.epoll.poll()
# 对事件进行判断
for fd, event in epoll_list:
# 如果是服务器套接字可以收数据,那么意味着可以进行accept
if fd == self.server_socket.fileno():
new_socket, new_addr = self.server_socket.accept()
# 向 epoll 中注册 连接 socket 的 可读 事件
self.epoll.register(new_socket.fileno(), select.EPOLLIN | select.EPOLLET)
# 记录这个信息
self.fd_socket[new_socket.fileno()] = new_socket
# 接收到数据
elif event == select.EPOLLIN:
request = self.fd_socket[fd].recv(1024).decode("utf-8")
if request:
self.deal_with_request(request, self.fd_socket[fd])
else:
# 在epoll中注销客户端的信息
self.epoll.unregister(fd)
# 关闭客户端的文件句柄
self.fd_socket[fd].close()
# 在字典中删除与已关闭客户端相关的信息
del self.fd_socket[fd]
def deal_with_request(self, request, client_socket):
"""为这个浏览器服务器"""
if not request:
return
request_lines = request.splitlines()
for i, line in enumerate(request_lines):
print(i, line)
# 提取请求的文件(index.html)
# GET /a/b/c/d/e/index.html HTTP/1.1
ret = re.match(r"([^/]*)([^ ]+)", request_lines[0])
if ret:
print("正则提取数据:", ret.group(1))
print("正则提取数据:", ret.group(2))
file_name = ret.group(2)
if file_name == "/":
file_name = "/index.html"
# 读取文件数据
try:
f = open(self.documents_root+file_name, "rb")
except:
response_body = "file not found, 请输入正确的url"
response_header = "HTTP/1.1 404 not found\r\n"
response_header += "Content-Type: text/html; charset=utf-8\r\n"
response_header += "Content-Length: %d\r\n" % len(response_body)
response_header += "\r\n"
# 将header返回给浏览器
client_socket.send(response_header.encode('utf-8'))
# 将body返回给浏览器
client_socket.send(response_body.encode("utf-8"))
else:
content = f.read()
f.close()
response_body = content
response_header = "HTTP/1.1 200 OK\r\n"
response_header += "Content-Length: %d\r\n" % len(response_body)
response_header += "\r\n"
# 将数据返回给浏览器
client_socket.send(response_header.encode("utf-8")+response_body)
# 设置服务器服务静态资源时的路径
DOCUMENTS_ROOT = "./html"
def main():
"""控制web服务器整体"""
# python3 xxxx.py 7890
if len(sys.argv) == 2:
port = sys.argv[1]
if port.isdigit():
port = int(port)
else:
print("运行方式如: python3 xxx.py 7890")
return
print("http服务器使用的port:%s" % port)
http_server = WSGIServer(port, DOCUMENTS_ROOT)
http_server.run_forever()
if __name__ == "__main__":
main()
5.Web静态服务器–gevent版
from gevent import monkey
import gevent
import socket
import sys
import re
monkey.patch_all()
class WSGIServer(object):
"""定义一个WSGI服务器的类"""
def __init__(self, port, documents_root):
# 1. 创建套接字
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 2. 绑定本地信息
self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server_socket.bind(("", port))
# 3. 变为监听套接字
self.server_socket.listen(128)
self.documents_root = documents_root
def run_forever(self):
"""运行服务器"""
# 等待对方链接
while True:
new_socket, new_addr = self.server_socket.accept()
gevent.spawn(self.deal_with_request, new_socket) # 创建一个协程准备运行它
def deal_with_request(self, client_socket):
"""为这个浏览器服务器"""
while True:
# 接收数据
request = client_socket.recv(1024).decode('utf-8')
# print(gevent.getcurrent())
# print(request)
# 当浏览器接收完数据后,会自动调用close进行关闭,因此当其关闭时,web也要关闭这个套接字
if not request:
new_socket.close()
break
request_lines = request.splitlines()
for i, line in enumerate(request_lines):
print(i, line)
# 提取请求的文件(index.html)
# GET /a/b/c/d/e/index.html HTTP/1.1
ret = re.match(r"([^/]*)([^ ]+)", request_lines[0])
if ret:
print("正则提取数据:", ret.group(1))
print("正则提取数据:", ret.group(2))
file_name = ret.group(2)
if file_name == "/":
file_name = "/index.html"
file_path_name = self.documents_root + file_name
try:
f = open(file_path_name, "rb")
except:
# 如果不能打开这个文件,那么意味着没有这个资源,没有资源 那么也得需要告诉浏览器 一些数据才行
# 404
response_body = "没有你需要的文件......".encode("utf-8")
response_headers = "HTTP/1.1 404 not found\r\n"
response_headers += "Content-Type:text/html;charset=utf-8\r\n"
response_headers += "Content-Length:%d\r\n" % len(response_body)
response_headers += "\r\n"
send_data = response_headers.encode("utf-8") + response_body
client_socket.send(send_data)
else:
content = f.read()
f.close()
# 响应的body信息
response_body = content
# 响应头信息
response_headers = "HTTP/1.1 200 OK\r\n"
response_headers += "Content-Type:text/html;charset=utf-8\r\n"
response_headers += "Content-Length:%d\r\n" % len(response_body)
response_headers += "\r\n"
send_data = response_headers.encode("utf-8") + response_body
client_socket.send(send_data)
# 设置服务器服务静态资源时的路径
DOCUMENTS_ROOT = "./html"
def main():
"""控制web服务器整体"""
# python3 xxxx.py 7890
if len(sys.argv) == 2:
port = sys.argv[1]
if port.isdigit():
port = int(port)
else:
print("运行方式如: python3 xxx.py 7890")
return
print("http服务器使用的port:%s" % port)
http_server = WSGIServer(port, DOCUMENTS_ROOT")
http_server.run_forever()
if __name__ == "__main__":
main()