229.各种服务器
013.根据用户的需求返回相应的页面
import socket
import re
def service_client(new_socket):
"""为这个客户端返回数据"""
# 1. 接收浏览器发送过来的请求,即http请求
# GET / HTTP/1.1
# ……
#http://127.0.0.1:7890/
# http://127.0.0.1:7890/index.html
request = new_socket.recv(1024).decode("utf-8")
# print(">>>" * 20)
# print(request)
request_lines = request.splitlines()
print("")
print(">>>" *20)
print(request_lines)
# GET / HTTP/1.1
#GET /index.html HTTP / 1.1
#请求方式get post put del...
ret = re.match(r"[^/]+/[^ ]*",request_lines[0])
file_name =""
if ret:
file_name = ret.group(1)
print("*" *50,file_name)
if file_name =='/':
file_name ='/index.html'
# 2. 返回http格式的数据给浏览器
try:
# f = open("../模板/1.html",'rb')
f =open("../模板" + file_name, 'rb')
except:
response ="HTTP/1.1 404 NOT FOUND\r\n"
response +="\r\n"
response +="---file not found---"
new_socket.send(response.encode("utf-8"))
else:
html_content = f.read()
f.close()
# 2.1 准备发送给浏览器的数据:Header
response ="HTTP/1.1 200 OK\r\n"
response +="\r\n"
# 2.2 准备发送给浏览器的数据:Body
# response += "
hahaha
"
# 将Response Header发送给浏览器
new_socket.send(response.enc各种服务器ode("utf-8"))
# 将Response body发送给浏览器
new_socket.send( html_content)
# 关闭套接字
new_socket.close()
def main():
"""用来完成整体的控制"""
# 1. 创建套接字
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 2. 绑定
tcp_server_socket.bind(("", 7890))
# 3. 变为监听套接字(最大连接数是128)
tcp_server_socket.listen(128)
while True:
# 4. 等待新客户端的链接
new_socket, client_addr = tcp_server_socket.accept()
# 5. 为这个客户端服务
service_client(new_socket)
# 6.关闭监听套接字
tcp_server_socket.close()
if __name__ =="__main__":
main()
014.使用多进程完成http服务器
import socket
import re
import multiprocessing#想用多进程必须导进multiprocessing这个
def service_client(new_socket):
"""为这个客户端返回数据"""
# 1. 接收浏览器发送过来的请求,即http请求
# GET / HTTP/1.1
# ……
#http://127.0.0.1:7890/
# http://127.0.0.1:7890/index.html
request = new_socket.recv(1024).decode("utf-8")
# print(">>>" * 20)
# print(request)
request_lines = request.splitlines()
print("")
print(">>>" *20)
print(request_lines)
# GET / HTTP/1.1
#GET /index.html HTTP / 1.1
#请求方式get post put del...
ret = re.match(r"[^/]+/[^ ]*",request_lines[0])
file_name =""
if ret:
file_name = ret.group(1)
print("*" *50,file_name)
if file_name =='/':
file_name ='/index.html'
# 2. 返回http格式的数据给浏览器
try:
# f = open("../模板/1.html",'rb')
f =open("../模板" + file_name, 'rb')
except:
response ="HTTP/1.1 404 NOT FOUND\r\n"
response +="\r\n"
response +="---file not found---"
new_socket.send(response.encode("utf-8"))
else:
html_content = f.read()
f.close()
# 2.1 准备发送给浏览器的数据:Header
response ="HTTP/1.1 200 OK\r\n"
response +="\r\n"
# 2.2 准备发送给浏览器的数据:Body
# response += "
hahaha
"
# 将Response Header发送给浏览器
new_socket.send(response.encode("utf-8"))
# 将Response body发送给浏览器
new_socket.send( html_content)
# 关闭套接字
new_socket.close()
def main():
"""用来完成整体的控制"""
# 1. 创建套接字
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 2. 绑定
tcp_server_socket.bind(("", 7890))
# 3. 变为监听套接字(最大连接数是128)
tcp_server_socket.listen(128)
while True:
# 4. 等待新客户端的链接
new_socket, client_addr = tcp_server_socket.accept()
# 5. 为这个客户端服务
#开子进程
p = multiprocessing.Process(target=service_client,args=(new_socket,))
p.start()
new_socket.close()
# 6.关闭监听套接字
tcp_server_socket.close()
if __name__ =="__main__":
main()
015.使用多线程完成HTTP服务器
import socket
import re
import threading
def service_client(new_socket):
"""为这个客户端返回数据"""
# 1. 接收浏览器发送过来的请求,即http请求
# GET / HTTP/1.1
# ……
#http://127.0.0.1:7890/
# http://127.0.0.1:7890/index.html
request = new_socket.recv(1024).decode("utf-8")
# print(">>>" * 20)
# print(request)
request_lines = request.splitlines()
print("")
print(">>>" *20)
print(request_lines)
# GET / HTTP/1.1
#GET /index.html HTTP / 1.1
#请求方式get post put del...
ret = re.match(r"[^/]+/[^ ]*",request_lines[0])
file_name =""
if ret:
file_name = ret.group(1)
print("*" *50,file_name)
if file_name =='/':
file_name ='/index.html'
# 2. 返回http格式的数据给浏览器
try:
# f = open("../模板/1.html",'rb')
f =open("../模板" + file_name, 'rb')
except:
response ="HTTP/1.1 404 NOT FOUND\r\n"
response +="\r\n"
response +="---file not found---"
new_socket.send(response.encode("utf-8"))
else:
html_content = f.read()
f.close()
# 2.1 准备发送给浏览器的数据:Header
response ="HTTP/1.1 200 OK\r\n"
response +="\r\n"
# 2.2 准备发送给浏览器的数据:Body
# response += "
hahaha
"
# 将Response Header发送给浏览器
new_socket.send(response.encode("utf-8"))
# 将Response body发送给浏览器
new_socket.send( html_content)
# 关闭套接字
new_socket.close()
def main():
"""用来完成整体的控制"""
# 1. 创建套接字
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 2. 绑定
tcp_server_socket.bind(("", 7890))
# 3. 变为监听套接字(最大连接数是128)
tcp_server_socket.listen(128)
while True:
# 4. 等待新客户端的链接
new_socket, client_addr = tcp_server_socket.accept()
# 5. 为这个客户端服务
p = threading.Thread(target=service_client,args=(new_socket,))
p.start()
new_socket.close()
# 6.关闭监听套接字
tcp_server_socket.close()
if __name__ =="__main__":
main()
016.使用gevent来实现http服务器
import socket
import re
import gevent
from geventimport monkey
monkey.patch_all()
def service_client(new_socket):
"""为这个客户端返回数据"""
# 1. 接收浏览器发送过来的请求,即http请求
# GET / HTTP/1.1
# ……
request = new_socket.recv(1024).decode("utf-8")
# print(">>>" * 20)
# print(request)
request_lines = request.splitlines()
print("")
print(">" *20)
print(request_lines)
# GET /index.html HTTP/1.1
# get post put del
ret = re.match(r"[^/]+(/[^ ]*)", request_lines[0])
file_name =""
if ret:
file_name = ret.group(1)
print("*" *50, file_name)
if file_name =='/':
file_name ='/index.html'
# 2. 返回http格式的数据给浏览器
try:
# f = open("../buickmall/index.html", "rb")
f =open("../buickmall" + file_name, "rb")
except:
response ="HTTP/1.1 404 NOT FOUND\r\n"
response +="\r\n"
response +="---file not found---"
new_socket.send(response.encode("utf-8"))
else:
html_content = f.read()
f.close()
# 2.1 准备发送给浏览器的数据:Header
response ="HTTP/1.1 200 OK\r\n"
response +="\r\n"
# 2.2 准备发送给浏览器的数据:Body
# response += "
hahaha
"
# 将Response Header发送给浏览器
new_socket.send(response.encode("utf-8"))
# 将Response Body发送给浏览器
new_socket.send(html_content)
# 关闭套接字
new_socket.close()
def main():
"""用来完成整体的控制"""
# 1. 创建套接字
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 2. 绑定
tcp_server_socket.bind(("", 7890))
# 3. 变为监听套接字
tcp_server_socket.listen(128)
while True:
# 4. 等待新客户端的链接
new_socket, client_addr = tcp_server_socket.accept()
# 5. 为这个客户端服务
gevent.spawn(service_client, new_socket)
# new_socket.close()
# 6.关闭监听套接字
tcp_server_socket.close()
if __name__ =="__main__":
main()
017.单进程-单线程-非堵塞
import socket
import time
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_server_socket.bind(("", 7890))
tcp_server_socket.listen(128)
tcp_server_socket.setblocking(False)# 设置套接字为非堵塞的方式
client_socket_list =list()
while True:
# time.sleep(0.5)
try:
new_socket, new_addr = tcp_server_socket.accept()
except Exception as ret:
print("---没有新的客户端到来---")
else:
print("---只要没有产生异常,那么也就意味着来了一个新的客户端---")
new_socket.setblocking(False)# 设置套接字为非堵塞的方式
client_socket_list.append(new_socket)
for client_socketin client_socket_list:
try:
recv_data = client_socket.recv(1024)
except Exception as ret:
# print(ret) # 打印异常方便调试
print("---这个客户端没有发送过来数据---")
else:
print("---没有异常---")
print(recv_data)
if recv_data:
# 对方发送过来数据
print("---客户端发送过来了数据---")
else:
# 对方调用close导致了recv返回
client_socket_list.remove(client_socket)
client_socket.close()
print("---客户端已经关闭---")
018.单进程-单线程-非阻塞-长链接
import socket
import re
def service_client(new_socket, request):
"""为这个客户端返回数据"""
# 1. 接收浏览器发送过来的请求,即http请求
# GET / HTTP/1.1
# ……
#http://127.0.0.1:7890/
# http://127.0.0.1:7890/index.html
# request = new_socket.recv(1024).decode("utf-8")
# print(">>>" * 20)
# print(request)
request_lines = request.splitlines()
print("")
print(">>>" *20)
print(request_lines)
# GET / HTTP/1.1
#GET /index.html HTTP / 1.1
#请求方式get post put del...
ret = re.match(r"[^/]+/[^ ]*",request_lines[0])
file_name =""
if ret:
file_name = ret.group(1)
print("*" *50,file_name)
if file_name =='/':
file_name ='/index.html'
# 2. 返回http格式的数据给浏览器
try:
# f = open("../模板/1.html",'rb')
f =open("../模板" + file_name, 'rb')
except:
response ="HTTP/1.1 404 NOT FOUND\r\n"
response +="\r\n"
response +="---file not found---"
new_socket.send(response.encode("utf-8"))
else:
html_content = f.read()
f.close()
# 2.1 准备发送给浏览器的数据:Header
response_body = html_content
# 2.2 准备发送给浏览器的数据:Body
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
# 将Response Header发送给浏览器
new_socket.send(response)
# 关闭套接字
new_socket.close()
def main():
"""用来完成整体的控制"""
# 1. 创建套接字
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 2. 绑定
tcp_server_socket.bind(("", 7890))
# 3. 变为监听套接字(最大连接数是128)
tcp_server_socket.listen(128)
tcp_server_socket,setblocking(False)# 将套接字变为非阻塞
client_socket_list =list()
while True:
# 4. 等待新客户端的链接
try:
new_socket, client_addr = tcp_server_socket.accept()
except Exception as ret:
pass
else:
new_socket.setblocking(Flase)
client_socket_list.append(new_socket)
for client_socketin client_socket_list:
try:
recv_data = client_socket_recv(1024).decode("utf-8")
except Exception as ret:
pass
else:
if recv_data:
# 5. 为这个客户端服务
service_client(client_socket,recv_data)
else:
client_socket_close()
client_socket_list.remove(client_socket)
# 6.关闭监听套接字
tcp_server_socket.close()
if __name__ =="__main__":
main()
作者:颜青ya
链接:https://www.jianshu.com/p/028ed3e48540
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。