用單程序/單線程我們可以實作一個傳回網頁頁面的http伺服器,其中主要的是把堵塞變為非堵塞 套接字.setblocking(False),來實作一個長連接配接
import socket
import re
import time
def main():
# 建立套接字
socket_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket_tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# 綁定本地資訊
socket_tcp.bind(("", 7899))
# 監聽 listen
socket_tcp.listen(128)
# 把堵塞變為非堵塞
socket_tcp.setblocking(False)
# 建立空清單以存儲使用者
client_list = list()
while True:
# time.sleep(0.5)
try:
# 拆包accept,等待客戶來連接配接
new_socket, add_socket = socket_tcp.accept()
# 為連接配接進來的客戶服務
except Exception as ret:
# print("沒有使用者連接配接")
pass
else:
# print("有使用者連接配接")
# 伺服器接收浏覽器請求
new_socket.setblocking(False)
# 将連接配接到的使用者添加到清單中
client_list.append(new_socket)
# 對清單進行周遊
for client_sock in client_list:
# 處理異常
try:
# 周遊到的使用者進行接受資訊
requese = client_sock.recv(1024).decode("utf-8")
except Exception as ret:
# print("客戶沒有發送請求")
pass
else:
# 如果已接受到資訊
if requese:
# print("客戶已發送請求")
#對接受到的資訊進行拆分,以定制規則
requess = requese.splitlines()
# print(requese)
print(requess)
file_name = " "
res = re.match(r"[^/]+(/[^ ]*)", requess[0])
if res:
file_name = res.group(1)
if file_name == "/":
file_name = "/index.html"
"""伺服器響應浏覽器"""
try:
file = open("./html" + file_name, "rb")
except:
respont = "HTTP/1.1 404 NOT FOUND \r\n"
respont += "\r\n"
respont += "<h1 style = 'color:red'>NOT FOUND 此目錄</h1>"
client_sock.send(respont.encode("gbk"))
else:
file_content = file.read()
file.close()
# 響應頭HTTP/1.1 200 OK
respont = "HTTP/1.1 200 OK \r\n"
# 接受body的長度去實作長連接配接的http
respont += "Content-Length:%d\r\n" % len(file_content)
respont += "\r\n"
client_sock.send(respont.encode("utf-8"))
# 響應body
client_sock.send(file_content)
else:
# 關閉套接字
client_sock.close()
client_list.remove(client_sock)
# 關閉套接字
socket_tcp.close()
if __name__ == '__main__':
main()