#coding=utf-8
import socket
import re
def handle_client(client_socket):
「為乙個客戶端進行服務」
# 如果沒有指定訪問哪個頁面。例如index.html
# 404表示沒有這個頁面
# 因為頭資訊在組織的時候,是按照字串組織的,不能與以二進位制開啟檔案讀取的資料合併,因此分開傳送
# 先傳送response的頭資訊
client_socket.send(response_headers.encode('utf-8'))
# 再傳送body
client_socket.send(response_body)
client_socket.close()def main():
「作為程式的主控制入口」
server_socket = socket.socket(socket.af_inet, socket.sock_stream)
server_socket.setsockopt(socket.sol_socket, socket.so_reuseaddr, 1)
server_socket.bind(("", 7788))
server_socket.listen(128)
while true:
client_socket, clien_caddr = server_socket.accept()
handle_client(client_socket)
#這裡配置伺服器
documents_root = 「./html」
ifname== 「main」:
main()
Web靜態伺服器 顯示需要的頁面
coding utf 8 import socket import re from multiprocessing import process 常量,所有字母全部大寫 設定靜態檔案根目錄 html root dir html defhandle client client socket 處理客戶端...
Web靜態伺服器 python顯示固定的頁面
coding utf 8 import socket def handle client client socket 為乙個客戶端進行服務 recv data client socket.recv 1024 decode utf 8 request header lines recv data.sp...
Web靜態伺服器02 顯示需要的頁面
coding utf 8 import socket import re def handle client client socket 為乙個客戶端進行服務 如果沒有指定訪問哪個頁面。例如index.html 404表示沒有這個頁面 因為頭資訊在組織的時候,是按照字串組織的,不能與以二進位制開啟檔...