一、49_按需返回页面的http服务器
"""
http(超文本传输)协议: 基于Tcp
Tcp类似于2人通话可以接收到(稳定);
Cdp类似于广播, 有可能接收不到
"""
"""根据用户的需求返回相应的页面"""
import socket
import re
def service_client(new_socket):
"""为客户端返回数据"""
# 1、接收浏览器发送过来的请求, 即http请求
# -->例GET /index.html HTTP/1.1 .....
# http://127.0.0.1:7890/
# http://127.0.0.1:7890/index.html
requst = new_socket.recv(1024).decode("utf-8")
"""【.decode:解码】"""
# print("-*-" * 20)
# print(requst)
requst_lines = requst.splitlines()
print("")
print("-*-" *20)
print(requst_lines)
# GET / HTTP/1.1
# GET /index.html HTTP/1.1
# get post put del.....
ret = re.match(r"[^/]+/([^ ]*)", requst_lines[0])
file_name =""
if ret:
file_name = ret.group(1)
print("*" *50, file_name)
if file_name =="/":
file_name ='/inedx.html'
# 2、返回http格式的数据给浏览器
try:
# f = open("../buickmall/indexhtml", "rb")
f =open("../buickmall" + file_name, "rb")
"""【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_coutent = f.read()
f.close()
# 2.1 准备给浏览器发送的数据 :Header
response ="HTTP/1.1 200 OK\r\n"
response +="\r\n"
"""【\r\n: 换行(兼容所有的浏览器)】"""
# 2.2 准备给浏览器发送的数据 :Body
# response += "
hehehe
"
# 将Response Header发送给浏览器
new_socket.send(response.encode("utf-8"))
"""【send: 发送【encode: 编码】"""
# 将Response Body发送给浏览器
new_socket.send(html_coutent)
# 关闭套接字
new_socket.close()
def main():
"""用来整体控制"""
# 1、创建套接字
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
"""选择让服务端先关闭"""
# 2、绑定; 参数("任意服务端ip", 端口)
tcp_server_socket.bind(("", 7890))
# 3、变为监听的套接字; 【listen:最大链接数】
tcp_server_socket.listen(128)
while True:
# 4、等待新客户端的连接; 变量[新客户端的套接字, 客户端的IP、端口]
new_socket, client_addr = tcp_server_socket.accept()
# 5、为这个客户端服务
service_client(new_socket)
# 6、关闭监听套接字
tcp_server_socket.close(new_socket)
if __name__ =='__main__':
main()
二、50_http协议复习
请求和响应都会分头和体
三、51_多进程服务器
"""使用多进程完成http服务器"""
import multiprocessing
"""导入多进程包"""
import socket
import re
"""线程间共享全局变量; 进程间不共享"""
def service_client(new_socket):
"""为客户端返回数据"""
# 1、接收浏览器发送过来的请求, 即http请求
# -->例GET /index.html HTTP/1.1 .....
# http://127.0.0.1:7890/
# http://127.0.0.1:7890/index.html
requst = new_socket.recv(1024).decode("utf-8")
"""【.decode:解码】"""
# print("-*-" * 20)
# print(requst)
requst_lines = requst.splitlines()
print("")
print("-*-" *20)
print(requst_lines)
# GET / HTTP/1.1
# GET /index.html HTTP/1.1
# get post put del.....
ret = re.match(r"[^/]+/([^ ]*)", requst_lines[0])
file_name =""
if ret:
file_name = ret.group(1)
print("*" *50, file_name)
if file_name =="/":
file_name ='/inedx.html'
# 2、返回http格式的数据给浏览器
try:
# f = open("../buickmall/indexhtml", "rb")
f =open("../buickmall" + file_name, "rb")
"""【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_coutent = f.read()
f.close()
# 2.1 准备给浏览器发送的数据 :Header
response ="HTTP/1.1 200 OK\r\n"
response +="\r\n"
"""【\r\n: 换行(兼容所有的浏览器)】"""
# 2.2 准备给浏览器发送的数据 :Body
# response += "
hehehe
"
# 将Response Header发送给浏览器
new_socket.send(response.encode("utf-8"))
"""【send: 发送【encode: 编码】"""
# 将Response Body发送给浏览器
new_socket.send(html_coutent)
# 关闭套接字
new_socket.close()
def main():
"""用来整体控制"""
# 1、创建套接字
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
"""选择让服务端先关闭"""
# 2、绑定; 参数("任意服务端ip", 端口)
tcp_server_socket.bind(("", 7890))
# 3、变为监听的套接字; 【listen:最大链接数】
tcp_server_socket.listen(128)
while True:
# 4、等待新客户端的连接; 变量[新客户端的套接字, 客户端的IP、端口]
new_socket, client_addr = tcp_server_socket.accept()
# 5、为这个客户端服务
p = multiprocessing.process(target=service_client, args=(new_socket,))
"""作用: 创建一个子进程【args里面的是元组, 所以要加一个逗号"""
p.start()
new_socket.close()
# 6、关闭监听套接字
tcp_server_socket.close(new_socket)
if __name__ =='__main__':
main()
四、52_多线程服务器
"""使用多线程完成http服务器"""
import threading
"""导入线程包"""
import socket
import re
"""线程间共享全局变量; 进程间不共享"""
def service_client(new_socket):
"""为客户端返回数据"""
# 1、接收浏览器发送过来的请求, 即http请求
# -->例GET /index.html HTTP/1.1 .....
# http://127.0.0.1:7890/
# http://127.0.0.1:7890/index.html
requst = new_socket.recv(1024).decode("utf-8")
"""【.decode:解码】"""
# print("-*-" * 20)
# print(requst)
requst_lines = requst.splitlines()
print("")
print("-*-" *20)
print(requst_lines)
# GET / HTTP/1.1
# GET /index.html HTTP/1.1
# get post put del.....
ret = re.match(r"[^/]+/([^ ]*)", requst_lines[0])
file_name =""
if ret:
file_name = ret.group(1)
print("*" *50, file_name)
if file_name =="/":
file_name ='/inedx.html'
# 2、返回http格式的数据给浏览器
try:
# f = open("../buickmall/indexhtml", "rb")
f =open("../buickmall" + file_name, "rb")
"""【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_coutent = f.read()
f.close()
# 2.1 准备给浏览器发送的数据 :Header
response ="HTTP/1.1 200 OK\r\n"
response +="\r\n"
"""【\r\n: 换行(兼容所有的浏览器)】"""
# 2.2 准备给浏览器发送的数据 :Body
# response += "
hehehe
"
# 将Response Header发送给浏览器
new_socket.send(response.encode("utf-8"))
"""【send: 发送【encode: 编码】"""
# 将Response Body发送给浏览器
new_socket.send(html_coutent)
# 关闭套接字
new_socket.close()
def main():
"""用来整体控制"""
# 1、创建套接字
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
"""选择让服务端先关闭"""
# 2、绑定; 参数("任意服务端ip", 端口)
tcp_server_socket.bind(("", 7890))
# 3、变为监听的套接字; 【listen:最大链接数】
tcp_server_socket.listen(128)
while True:
# 4、等待新客户端的连接; 变量[新客户端的套接字, 客户端的IP、端口]
new_socket, client_addr = tcp_server_socket.accept()
# 5、为这个客户端服务
p = threading.Thread(target=service_client, args=(new_socket,))
"""作用: 创建一个子进程【args里面的是元组, 所以要加一个逗号"""
p.start()
# new_socket.close()
# 6、关闭监听套接字
tcp_server_socket.close(new_socket)
if __name__ =='__main__':
main()
五、53_协程服务器
"""使用gevent来实现http服务器"""
import gevent
from geventimport monkey
monkey.patch_all()
"""导入协程包等资源"""
import socket
import re
"""线程间共享全局变量; 进程间不共享"""
def service_client(new_socket):
"""为客户端返回数据"""
# 1、接收浏览器发送过来的请求, 即http请求
# -->例GET /index.html HTTP/1.1 .....
# http://127.0.0.1:7890/
# http://127.0.0.1:7890/index.html
requst = new_socket.recv(1024).decode("utf-8")
"""【.decode:解码】"""
# print("-*-" * 20)
# print(requst)
requst_lines = requst.splitlines()
print("")
print("-*-" *20)
print(requst_lines)
# GET / HTTP/1.1
# GET /index.html HTTP/1.1
# get post put del.....
ret = re.match(r"[^/]+/([^ ]*)", requst_lines[0])
file_name =""
if ret:
file_name = ret.group(1)
print("*" *50, file_name)
if file_name =="/":
file_name ='/inedx.html'
# 2、返回http格式的数据给浏览器
try:
# f = open("../buickmall/indexhtml", "rb")
f =open("../buickmall" + file_name, "rb")
"""【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_coutent = f.read()
f.close()
# 2.1 准备给浏览器发送的数据 :Header
response ="HTTP/1.1 200 OK\r\n"
response +="\r\n"
"""【\r\n: 换行(兼容所有的浏览器)】"""
# 2.2 准备给浏览器发送的数据 :Body
# response += "
hehehe
"
# 将Response Header发送给浏览器
new_socket.send(response.encode("utf-8"))
"""【send: 发送【encode: 编码】"""
# 将Response Body发送给浏览器
new_socket.send(html_coutent)
# 关闭套接字
new_socket.close()
def main():
"""用来整体控制"""
# 1、创建套接字
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
"""选择让服务端先关闭"""
# 2、绑定; 参数("任意服务端ip", 端口)
tcp_server_socket.bind(("", 7890))
# 3、变为监听的套接字; 【listen:最大链接数】
tcp_server_socket.listen(128)
while True:
# 4、等待新客户端的连接; 变量[新客户端的套接字, 客户端的IP、端口]
new_socket, client_addr = tcp_server_socket.accept()
# 5、为这个客户端服务
gevent.spawn(target=service_client, args=(new_socket,))
"""作用: 创建一个子进程【args里面的是元组, 所以要加一个逗号"""
# new_socket.close()
# 6、关闭监听套接字
tcp_server_socket.close(new_socket)
if __name__ =='__main__':
main()
六、54_单进程单线程非阻塞并发服务器
"""单进程单线程非阻塞"""
import socket
import time
# 1、创建一个Tcp服务端
tcp_server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
tcp_server_socket.bind(("", 7890))
"""绑定端口"""
tcp_server_socket.listen(128)
"""把套接字变为监听的; 【listen(x):同时可以有x个客户端连接】"""
tcp_server_socket.setblocking(False)
"""
【.recv():阻塞】
【.setblocking(False):设置套接字为非阻塞的方式】
"""
client_soket_list =list()
"""设置一个客户端的空列表"""
while True:
time.sleep(1)
try:
# 2、阻塞中等待客户端连接
new_socket, new_addr = tcp_server_socket.accept()
except Exception as ret:
print("---没有新的客户端到来---")
else:
print("---只要没有产生异常,就表明来了一个新的客户端---")
new_socket.setblocking(False)
"""设置套接字为非阻塞的方式"""
client_soket_list.append(new_socket)
for client_soketin client_soket_list:
try:
recv_data = new_socket.recv(1024).decode("utf-8")
"""【接收】:每次接收x个字节"""
except Exception as ret:
# print(ret) #方便用于进行调试
print("---这个客户端没有发送过来数据---")
else:
print("---没有异常---")
print(recv_data)
if recv_data:
# 对方发送过来数据
print("---客户端发送过来了数据---")
else:
# 对方调用了close导致recv返回
client_soket_list.remove(client_soket)
client_soket.close()
print("---客户端已经关闭了---")