Python靜態(tài)Web服務(wù)器面向?qū)ο筇幚砜蛻舳苏?qǐng)求
概述
把Web服務(wù)器抽象成一個(gè)類,方法初始化,在初始化中建立套接字對(duì)線。提供一個(gè)開(kāi)啟Web服務(wù)器的方法,讓W(xué)eb服務(wù)器處理客戶端的請(qǐng)求。
實(shí)現(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è)置端口號(hào)復(fù)用, 程序退出端口立即釋放
tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
# 綁定端口號(hào)
tcp_server_socket.bind(("", 9000))
# 設(shè)置監(jiān)聽(tīng)
tcp_server_socket.listen(128)
# 保存創(chuàng)建成功的服務(wù)器套接字
self.tcp_server_socket = tcp_server_socket處理客戶端請(qǐng)求
@staticmethod
def handle_client_request(new_socket):
# 代碼執(zhí)行到此,說(shuō)明連接建立成功
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)打開(kā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()
啟動(dòng)web服務(wù)器進(jìn)行工作
def start(self):
while True:
# 等待接受客戶端的連接請(qǐng)求
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)
# 啟動(dòng)子線程執(zhí)行對(duì)應(yīng)的任務(wù)
sub_thread.start()代碼實(shí)現(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è)置端口號(hào)復(fù)用, 程序退出端口立即釋放
tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
# 綁定端口號(hào)
tcp_server_socket.bind(("", 9000))
# 設(shè)置監(jiān)聽(tīng)
tcp_server_socket.listen(128)
# 保存創(chuàng)建成功的服務(wù)器套接字
self.tcp_server_socket = tcp_server_socket
# 處理客戶端的請(qǐng)求
@staticmethod
def handle_client_request(new_socket):
# 代碼執(zhí)行到此,說(shuō)明連接建立成功
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)打開(kā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()
# 啟動(dòng)web服務(wù)器進(jìn)行工作
def start(self):
while True:
# 等待接受客戶端的連接請(qǐng)求
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)
# 啟動(dòng)子線程執(zhí)行對(duì)應(yīng)的任務(wù)
sub_thread.start()
# 程序入口函數(shù)
def main():
# 創(chuàng)建web服務(wù)器對(duì)象
web_server = HttpWebServer()
# 啟動(dòng)web服務(wù)器進(jìn)行工作
web_server.start()
if __name__ == '__main__':
main()以上就是Python面向?qū)ο髮?shí)現(xiàn)靜態(tài)Web服務(wù)器處理客戶端請(qǐng)求的詳細(xì)內(nèi)容,更多關(guān)于Python面向?qū)ο箪o態(tài)Web服務(wù)器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
用python實(shí)現(xiàn)的去除win下文本文件頭部BOM的代碼
windows環(huán)境下新建或編輯文本文件,保存時(shí)會(huì)在頭部加上BOM。使用ftp上傳到linux下,在執(zhí)行時(shí)第一行即報(bào)錯(cuò)。以下方法可以去除BOM頭,有需要的朋友可以參考下2013-02-02
Python實(shí)現(xiàn)網(wǎng)站表單提交和模板
今天小編就為大家分享一篇關(guān)于Python實(shí)現(xiàn)網(wǎng)站表單提交和模板,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01
python入門學(xué)習(xí)關(guān)于for else的特殊特性講解
本文將介紹 Python 中的" for-else"特性,并通過(guò)簡(jiǎn)單的示例說(shuō)明如何正確使用它,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11
Pycharm連接遠(yuǎn)程服務(wù)器過(guò)程圖解
這篇文章主要介紹了Pycharm連接遠(yuǎn)程服務(wù)器過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Python?Enum枚舉類的定義及使用場(chǎng)景最佳實(shí)踐
枚舉(Enum)是一種有助于提高代碼可讀性和可維護(hù)性的數(shù)據(jù)類型,允許我們?yōu)橐唤M相關(guān)的常量賦予有意義的名字,在Python中,枚舉類(Enum)提供了一種簡(jiǎn)潔而強(qiáng)大的方式來(lái)定義和使用枚舉2023-11-11
python微信公眾號(hào)開(kāi)發(fā)簡(jiǎn)單流程實(shí)現(xiàn)
這篇文章主要介紹了python微信公眾號(hào)開(kāi)發(fā)簡(jiǎn)單流程實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03

