Python多任務(wù)版靜態(tài)Web服務(wù)器實(shí)現(xiàn)示例
概述
使用多線程,讓web服務(wù)器可以同時(shí)處理多個(gè)用戶的訪問。當(dāng)客戶端和服務(wù)端建立連接成功,創(chuàng)建子線程,使用子線程專門處理客戶端的請(qǐng)求,防止主線程阻塞。把創(chuàng)建的子線程設(shè)置成為守護(hù)主線程,防止主線程無(wú)法退出。
實(shí)現(xiàn)步驟
1.導(dǎo)入threading模塊
import threading
2.設(shè)置端口號(hào)復(fù)用, 程序退出端口立即釋放
tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
3.當(dāng)客戶端和服務(wù)器建立連接程,創(chuàng)建子線程
設(shè)置target的名稱,并且通過元組的方式傳入new_socket參數(shù)
sub_thread = threading.Thread(target=handle_client_request, args=(new_socket,))
4.設(shè)置守護(hù)主進(jìn)程
sub_thread.setDaemon(True)
5.啟動(dòng)子線程
sub_thread.start()
代碼實(shí)現(xiàn)
import socket
import threading
# 處理客戶端的請(qǐng)求
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
# 對(duì)二進(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)
# 獲取請(qǐng)求資源路徑
request_path = request_list[1]
print(request_path)
# 判斷請(qǐng)求的是否是根目錄,如果條件成立,指定首頁(yè)數(shù)據(jù)返回
if request_path == "/":
request_path = "/index.html"
try:
# 動(dòng)態(tài)打開指定文件
with open("static" + request_path, "rb") as file:
# 讀取文件數(shù)據(jù)
file_data = file.read()
except Exception as e:
# 請(qǐng)求資源不存在,返回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)報(bào)文
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)報(bào)文
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()
# 程序入口函數(shù)
def main():
# 創(chuàng)建tcp服務(wù)端套接字
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 設(shè)置端口號(hào)復(fù)用, 程序退出端口立即釋放
tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
# 綁定端口號(hào)
tcp_server_socket.bind(("", 9000))
# 設(shè)置監(jiān)聽
tcp_server_socket.listen(128)
while True:
# 等待接受客戶端的連接請(qǐng)求
new_socket, ip_port = tcp_server_socket.accept()
print(ip_port)
# 當(dāng)客戶端和服務(wù)器建立連接程,創(chuàng)建子線程
sub_thread = threading.Thread(target=handle_client_request, args=(new_socket,))
# 設(shè)置守護(hù)主線程
sub_thread.setDaemon(True)
# 啟動(dòng)子線程執(zhí)行對(duì)應(yīng)的任務(wù)
sub_thread.start()
if __name__ == '__main__':
main()以上就是Python靜態(tài)Web服務(wù)器多任務(wù)版實(shí)現(xiàn)示例的詳細(xì)內(nèi)容,更多關(guān)于Python靜態(tài)Web服務(wù)器多任務(wù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python sqlalchemy時(shí)間戳及密碼管理實(shí)現(xiàn)代碼詳解
這篇文章主要介紹了Python sqlalchemy時(shí)間戳及密碼管理實(shí)現(xiàn)代碼詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
19個(gè)Python?Sklearn中超實(shí)用的隱藏功能分享
今天跟大家介紹?19?個(gè)?Sklearn?中超級(jí)實(shí)用的隱藏的功能,這些功能雖然不常見,但非常實(shí)用,它們可以直接優(yōu)雅地替代手動(dòng)執(zhí)行的常見操作2022-07-07
關(guān)于Python連接Cassandra容器進(jìn)行查詢的問題
這篇文章主要介紹了Python連接Cassandra容器進(jìn)行查詢的問題,問題的關(guān)鍵在于尋找到Cassandra的9042端口,從而獲取數(shù)據(jù),具有內(nèi)容詳情跟隨小編一起看看吧2021-11-11
Python 實(shí)現(xiàn)opencv所使用的圖片格式與 base64 轉(zhuǎn)換
今天小編就為大家分享一篇Python 實(shí)現(xiàn)opencv所使用的圖片格式與 base64 轉(zhuǎn)換,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-01-01

