Python靜態(tài)Web服務(wù)器面向?qū)ο筇幚砜蛻舳苏埱?/h1>
更新時間:2022年06月17日 15:51:02 作者:落雨
這篇文章主要為大家介紹了Python面向?qū)ο髮崿F(xiàn)靜態(tài)Web服務(wù)器處理客戶端請求示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
概述
把Web服務(wù)器抽象成一個類,方法初始化,在初始化中建立套接字對線。提供一個開啟Web服務(wù)器的方法,讓W(xué)eb服務(wù)器處理客戶端的請求。
實現(xiàn)步驟
1.定義web服務(wù)器類
初始化類
class HttpWebServer(object):
def __init__(self):
# 創(chuàng)建tcp服務(wù)端套接字
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 設(shè)置端口號復(fù)用, 程序退出端口立即釋放
tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
# 綁定端口號
tcp_server_socket.bind(("", 9000))
# 設(shè)置監(jiān)聽
tcp_server_socket.listen(128)
# 保存創(chuàng)建成功的服務(wù)器套接字
self.tcp_server_socket = tcp_server_socket
處理客戶端請求
@staticmethod
def handle_client_request(new_socket):
# 代碼執(zhí)行到此,說明連接建立成功
recv_client_data = new_socket.recv(4096)
if len(recv_client_data) == 0:
print("關(guān)閉瀏覽器了")
new_socket.close()
return
# 對二進(jìn)制數(shù)據(jù)進(jìn)行解碼
recv_client_content = recv_client_data.decode("utf-8")
print(recv_client_content)
# 根據(jù)指定字符串進(jìn)行分割, 最大分割次數(shù)指定2
request_list = recv_client_content.split(" ", maxsplit=2)
# 獲取請求資源路徑
request_path = request_list[1]
print(request_path)
# 判斷請求的是否是根目錄,如果條件成立,指定首頁數(shù)據(jù)返回
if request_path == "/":
request_path = "/index.html"
try:
# 動態(tài)打開指定文件
with open("static" + request_path, "rb") as file:
# 讀取文件數(shù)據(jù)
file_data = file.read()
except Exception as e:
# 請求資源不存在,返回404數(shù)據(jù)
# 響應(yīng)行
response_line = "HTTP/1.1 404 Not Found\r\n"
# 響應(yīng)頭
response_header = "Server: PWS1.0\r\n"
with open("static/error.html", "rb") as file:
file_data = file.read()
# 響應(yīng)體
response_body = file_data
# 拼接響應(yīng)報文
response_data = (response_line + response_header + "\r\n").encode("utf-8") + response_body
# 發(fā)送數(shù)據(jù)
new_socket.send(response_data)
else:
# 響應(yīng)行
response_line = "HTTP/1.1 200 OK\r\n"
# 響應(yīng)頭
response_header = "Server: PWS1.0\r\n"
# 響應(yīng)體
response_body = file_data
# 拼接響應(yīng)報文
response_data = (response_line + response_header + "\r\n").encode("utf-8") + response_body
# 發(fā)送數(shù)據(jù)
new_socket.send(response_data)
finally:
# 關(guān)閉服務(wù)與客戶端的套接字
new_socket.close()
啟動web服務(wù)器進(jìn)行工作
def start(self):
while True:
# 等待接受客戶端的連接請求
new_socket, ip_port = self.tcp_server_socket.accept()
# 當(dāng)客戶端和服務(wù)器建立連接程,創(chuàng)建子線程
sub_thread = threading.Thread(target=self.handle_client_request, args=(new_socket,))
# 設(shè)置守護(hù)主線程
sub_thread.setDaemon(True)
# 啟動子線程執(zhí)行對應(yīng)的任務(wù)
sub_thread.start()
代碼實現(xiàn)
import socket
import threading
# 定義web服務(wù)器類
class HttpWebServer(object):
def __init__(self):
# 創(chuàng)建tcp服務(wù)端套接字
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 設(shè)置端口號復(fù)用, 程序退出端口立即釋放
tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
# 綁定端口號
tcp_server_socket.bind(("", 9000))
# 設(shè)置監(jiān)聽
tcp_server_socket.listen(128)
# 保存創(chuàng)建成功的服務(wù)器套接字
self.tcp_server_socket = tcp_server_socket
# 處理客戶端的請求
@staticmethod
def handle_client_request(new_socket):
# 代碼執(zhí)行到此,說明連接建立成功
recv_client_data = new_socket.recv(4096)
if len(recv_client_data) == 0:
print("關(guān)閉瀏覽器了")
new_socket.close()
return
# 對二進(jìn)制數(shù)據(jù)進(jìn)行解碼
recv_client_content = recv_client_data.decode("utf-8")
print(recv_client_content)
# 根據(jù)指定字符串進(jìn)行分割, 最大分割次數(shù)指定2
request_list = recv_client_content.split(" ", maxsplit=2)
# 獲取請求資源路徑
request_path = request_list[1]
print(request_path)
# 判斷請求的是否是根目錄,如果條件成立,指定首頁數(shù)據(jù)返回
if request_path == "/":
request_path = "/index.html"
try:
# 動態(tài)打開指定文件
with open("static" + request_path, "rb") as file:
# 讀取文件數(shù)據(jù)
file_data = file.read()
except Exception as e:
# 請求資源不存在,返回404數(shù)據(jù)
# 響應(yīng)行
response_line = "HTTP/1.1 404 Not Found\r\n"
# 響應(yīng)頭
response_header = "Server: PWS1.0\r\n"
with open("static/error.html", "rb") as file:
file_data = file.read()
# 響應(yīng)體
response_body = file_data
# 拼接響應(yīng)報文
response_data = (response_line + response_header + "\r\n").encode("utf-8") + response_body
# 發(fā)送數(shù)據(jù)
new_socket.send(response_data)
else:
# 響應(yīng)行
response_line = "HTTP/1.1 200 OK\r\n"
# 響應(yīng)頭
response_header = "Server: PWS1.0\r\n"
# 響應(yīng)體
response_body = file_data
# 拼接響應(yīng)報文
response_data = (response_line + response_header + "\r\n").encode("utf-8") + response_body
# 發(fā)送數(shù)據(jù)
new_socket.send(response_data)
finally:
# 關(guān)閉服務(wù)與客戶端的套接字
new_socket.close()
# 啟動web服務(wù)器進(jìn)行工作
def start(self):
while True:
# 等待接受客戶端的連接請求
new_socket, ip_port = self.tcp_server_socket.accept()
# 當(dāng)客戶端和服務(wù)器建立連接程,創(chuàng)建子線程
sub_thread = threading.Thread(target=self.handle_client_request, args=(new_socket,))
# 設(shè)置守護(hù)主線程
sub_thread.setDaemon(True)
# 啟動子線程執(zhí)行對應(yīng)的任務(wù)
sub_thread.start()
# 程序入口函數(shù)
def main():
# 創(chuàng)建web服務(wù)器對象
web_server = HttpWebServer()
# 啟動web服務(wù)器進(jìn)行工作
web_server.start()
if __name__ == '__main__':
main()
以上就是Python面向?qū)ο髮崿F(xiàn)靜態(tài)Web服務(wù)器處理客戶端請求的詳細(xì)內(nèi)容,更多關(guān)于Python面向?qū)ο箪o態(tài)Web服務(wù)器的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
-
用python實現(xiàn)的去除win下文本文件頭部BOM的代碼
windows環(huán)境下新建或編輯文本文件,保存時會在頭部加上BOM。使用ftp上傳到linux下,在執(zhí)行時第一行即報錯。以下方法可以去除BOM頭,有需要的朋友可以參考下 2013-02-02
-
python入門學(xué)習(xí)關(guān)于for else的特殊特性講解
本文將介紹 Python 中的" for-else"特性,并通過簡單的示例說明如何正確使用它,有需要的朋友可以借鑒參考下,希望能夠有所幫助 2021-11-11
最新評論
概述
把Web服務(wù)器抽象成一個類,方法初始化,在初始化中建立套接字對線。提供一個開啟Web服務(wù)器的方法,讓W(xué)eb服務(wù)器處理客戶端的請求。
實現(xiàn)步驟
1.定義web服務(wù)器類
初始化類
class HttpWebServer(object): def __init__(self): # 創(chuàng)建tcp服務(wù)端套接字 tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 設(shè)置端口號復(fù)用, 程序退出端口立即釋放 tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) # 綁定端口號 tcp_server_socket.bind(("", 9000)) # 設(shè)置監(jiān)聽 tcp_server_socket.listen(128) # 保存創(chuàng)建成功的服務(wù)器套接字 self.tcp_server_socket = tcp_server_socket
處理客戶端請求
@staticmethod def handle_client_request(new_socket): # 代碼執(zhí)行到此,說明連接建立成功 recv_client_data = new_socket.recv(4096) if len(recv_client_data) == 0: print("關(guān)閉瀏覽器了") new_socket.close() return # 對二進(jìn)制數(shù)據(jù)進(jìn)行解碼 recv_client_content = recv_client_data.decode("utf-8") print(recv_client_content) # 根據(jù)指定字符串進(jìn)行分割, 最大分割次數(shù)指定2 request_list = recv_client_content.split(" ", maxsplit=2) # 獲取請求資源路徑 request_path = request_list[1] print(request_path) # 判斷請求的是否是根目錄,如果條件成立,指定首頁數(shù)據(jù)返回 if request_path == "/": request_path = "/index.html" try: # 動態(tài)打開指定文件 with open("static" + request_path, "rb") as file: # 讀取文件數(shù)據(jù) file_data = file.read() except Exception as e: # 請求資源不存在,返回404數(shù)據(jù) # 響應(yīng)行 response_line = "HTTP/1.1 404 Not Found\r\n" # 響應(yīng)頭 response_header = "Server: PWS1.0\r\n" with open("static/error.html", "rb") as file: file_data = file.read() # 響應(yīng)體 response_body = file_data # 拼接響應(yīng)報文 response_data = (response_line + response_header + "\r\n").encode("utf-8") + response_body # 發(fā)送數(shù)據(jù) new_socket.send(response_data) else: # 響應(yīng)行 response_line = "HTTP/1.1 200 OK\r\n" # 響應(yīng)頭 response_header = "Server: PWS1.0\r\n" # 響應(yīng)體 response_body = file_data # 拼接響應(yīng)報文 response_data = (response_line + response_header + "\r\n").encode("utf-8") + response_body # 發(fā)送數(shù)據(jù) new_socket.send(response_data) finally: # 關(guān)閉服務(wù)與客戶端的套接字 new_socket.close()
啟動web服務(wù)器進(jìn)行工作
def start(self): while True: # 等待接受客戶端的連接請求 new_socket, ip_port = self.tcp_server_socket.accept() # 當(dāng)客戶端和服務(wù)器建立連接程,創(chuàng)建子線程 sub_thread = threading.Thread(target=self.handle_client_request, args=(new_socket,)) # 設(shè)置守護(hù)主線程 sub_thread.setDaemon(True) # 啟動子線程執(zhí)行對應(yīng)的任務(wù) sub_thread.start()
代碼實現(xiàn)
import socket import threading # 定義web服務(wù)器類 class HttpWebServer(object): def __init__(self): # 創(chuàng)建tcp服務(wù)端套接字 tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 設(shè)置端口號復(fù)用, 程序退出端口立即釋放 tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) # 綁定端口號 tcp_server_socket.bind(("", 9000)) # 設(shè)置監(jiān)聽 tcp_server_socket.listen(128) # 保存創(chuàng)建成功的服務(wù)器套接字 self.tcp_server_socket = tcp_server_socket # 處理客戶端的請求 @staticmethod def handle_client_request(new_socket): # 代碼執(zhí)行到此,說明連接建立成功 recv_client_data = new_socket.recv(4096) if len(recv_client_data) == 0: print("關(guān)閉瀏覽器了") new_socket.close() return # 對二進(jìn)制數(shù)據(jù)進(jìn)行解碼 recv_client_content = recv_client_data.decode("utf-8") print(recv_client_content) # 根據(jù)指定字符串進(jìn)行分割, 最大分割次數(shù)指定2 request_list = recv_client_content.split(" ", maxsplit=2) # 獲取請求資源路徑 request_path = request_list[1] print(request_path) # 判斷請求的是否是根目錄,如果條件成立,指定首頁數(shù)據(jù)返回 if request_path == "/": request_path = "/index.html" try: # 動態(tài)打開指定文件 with open("static" + request_path, "rb") as file: # 讀取文件數(shù)據(jù) file_data = file.read() except Exception as e: # 請求資源不存在,返回404數(shù)據(jù) # 響應(yīng)行 response_line = "HTTP/1.1 404 Not Found\r\n" # 響應(yīng)頭 response_header = "Server: PWS1.0\r\n" with open("static/error.html", "rb") as file: file_data = file.read() # 響應(yīng)體 response_body = file_data # 拼接響應(yīng)報文 response_data = (response_line + response_header + "\r\n").encode("utf-8") + response_body # 發(fā)送數(shù)據(jù) new_socket.send(response_data) else: # 響應(yīng)行 response_line = "HTTP/1.1 200 OK\r\n" # 響應(yīng)頭 response_header = "Server: PWS1.0\r\n" # 響應(yīng)體 response_body = file_data # 拼接響應(yīng)報文 response_data = (response_line + response_header + "\r\n").encode("utf-8") + response_body # 發(fā)送數(shù)據(jù) new_socket.send(response_data) finally: # 關(guān)閉服務(wù)與客戶端的套接字 new_socket.close() # 啟動web服務(wù)器進(jìn)行工作 def start(self): while True: # 等待接受客戶端的連接請求 new_socket, ip_port = self.tcp_server_socket.accept() # 當(dāng)客戶端和服務(wù)器建立連接程,創(chuàng)建子線程 sub_thread = threading.Thread(target=self.handle_client_request, args=(new_socket,)) # 設(shè)置守護(hù)主線程 sub_thread.setDaemon(True) # 啟動子線程執(zhí)行對應(yīng)的任務(wù) sub_thread.start() # 程序入口函數(shù) def main(): # 創(chuàng)建web服務(wù)器對象 web_server = HttpWebServer() # 啟動web服務(wù)器進(jìn)行工作 web_server.start() if __name__ == '__main__': main()
以上就是Python面向?qū)ο髮崿F(xiàn)靜態(tài)Web服務(wù)器處理客戶端請求的詳細(xì)內(nèi)容,更多關(guān)于Python面向?qū)ο箪o態(tài)Web服務(wù)器的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
用python實現(xiàn)的去除win下文本文件頭部BOM的代碼
windows環(huán)境下新建或編輯文本文件,保存時會在頭部加上BOM。使用ftp上傳到linux下,在執(zhí)行時第一行即報錯。以下方法可以去除BOM頭,有需要的朋友可以參考下2013-02-02python入門學(xué)習(xí)關(guān)于for else的特殊特性講解
本文將介紹 Python 中的" for-else"特性,并通過簡單的示例說明如何正確使用它,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11