一、靜態(tài)web服務(wù)器
靜態(tài)web法服務(wù)器:可為發(fā)出請(qǐng)求的瀏覽器提供靜態(tài)文檔的程序,平時(shí)上網(wǎng)瀏覽的頁面都是動(dòng)態(tài)的,而開發(fā)的是靜態(tài)的,頁面數(shù)據(jù)不會(huì)發(fā)生變化
搭建python自帶的靜態(tài)web服務(wù)器
-
命令:python3 -m http.server 端口號(hào)
- -m:表示運(yùn)行包里面的模塊,執(zhí)行該命令需進(jìn)入指定靜態(tài)文件目錄,通過瀏覽器就能訪問對(duì)應(yīng)html文件
- 端口號(hào)若不指定則默認(rèn)為8000
- 訪問:http://127.0.0.1:9000/
瀏覽器訪問搭建的靜態(tài)web服務(wù)器及其通信過程
二、靜態(tài)Web服務(wù)器-返回固定頁面數(shù)據(jù)?
靜態(tài)Web服務(wù)器實(shí)現(xiàn)步驟
- 編寫TCP服務(wù)端程序
- 獲取瀏覽器發(fā)送的http請(qǐng)求報(bào)文數(shù)據(jù)
- 讀取固定數(shù)據(jù),將頁面數(shù)據(jù)組裝為http響應(yīng)報(bào)文數(shù)據(jù)發(fā)送給瀏覽器
- http響應(yīng)報(bào)文數(shù)據(jù)發(fā)送完后,關(guān)閉服務(wù)于客戶端的套接字
實(shí)現(xiàn)代碼?
import socket
if __name__ == '__main__':
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 創(chuàng)建TCP服務(wù)端套接字
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) # 設(shè)置端口號(hào)復(fù)用,程序退出端口立即釋放
server_socket.bind(('', 9000)) # 綁定端口號(hào)
server_socket.listen(128) # 設(shè)置監(jiān)聽
while True: # 循環(huán)接受客戶端的連接請(qǐng)求
comm_socket, ip_port = server_socket.accept() # 等待接受客戶端的連接請(qǐng)求
recv_data = comm_socket.recv(4096) # 獲取瀏覽器發(fā)送的http請(qǐng)求報(bào)文數(shù)據(jù)
print('接收到的數(shù)據(jù)解碼后為:', recv_data.decode('utf-8'))
with open('C:/Users/username/Desktop/ubuntu/file1/1.txt', 'rb') as file:
file_data = file.read() # 讀取文件數(shù)據(jù)
response_line = "HTTP/1.1 200 OK ?。?!\r\n" # 響應(yīng)行
response_header = "Server: PWS1.0 # 服務(wù)器名稱及版本\r\n" # 響應(yīng)頭
response_body = file_data # 響應(yīng)體
response_data = (response_line + response_header + '\r\n').encode('utf-8') + response_body # 將數(shù)據(jù)組裝成HTTP響應(yīng)報(bào)文數(shù)據(jù)發(fā)送給瀏覽器
comm_socket.send(response_data) # 發(fā)送響應(yīng)報(bào)文數(shù)據(jù)至瀏覽器
comm_socket.close() # 關(guān)閉服務(wù)于客戶端的套接字
輸出如下
瀏覽器刷新后輸出:
接收到的數(shù)據(jù)解碼后為: GET /file1/1.txt HTTP/1.1
Host: 127.0.0.1:9000
Connection: keep-alive
Cache-Control: max-age=0
sec-ch-ua: "Google Chrome";v="105", "Not)A;Brand";v="8", "Chromium";v="105"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Cookie: csrftoken=1wb7v0r0BQuokJCxRS4JAO2XApvrHXFP90t2PiYb0mz7AwWS0NoKTi0zNaIjOfTl; Hm_lvt_18f619820855042bca58b32408f44de7=1658219884
If-Modified-Since: Sat, 15 Oct 2022 02:36:31 GMT
瀏覽器響應(yīng)如下?
三、靜態(tài)Web服務(wù)器-返回指定頁面數(shù)據(jù)
實(shí)現(xiàn)步驟
- 獲取用戶請(qǐng)求資源路徑
- 根據(jù)路徑讀取指定文件數(shù)據(jù)
- 將指定文件數(shù)據(jù)組裝為響應(yīng)報(bào)文,發(fā)送給瀏覽器
- 判斷請(qǐng)求文件在服務(wù)端不存在,組裝404狀態(tài)響應(yīng)報(bào)文,發(fā)送給瀏覽器
代碼實(shí)現(xiàn)
import socket
def main():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 創(chuàng)建TCP服務(wù)端套接字
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) # 設(shè)置端口號(hào)復(fù)用,程序退出端口立即釋放
server_socket.bind(("", 9000)) # 綁定端口號(hào)
server_socket.listen(128) # 設(shè)置監(jiān)聽
while True:
comm_socket, ip_port = server_socket.accept() # 等待接收客戶端連接請(qǐng)求
recv_data = comm_socket.recv(4096) # 接收請(qǐng)求的二進(jìn)制數(shù)據(jù)
if len(recv_data) == 0 :
print('未獲取到請(qǐng)求數(shù)據(jù)!')
comm_socket.close()
return
recv_content = recv_data.decode('utf-8') # 對(duì)二進(jìn)制數(shù)據(jù)解碼
print('獲取到的數(shù)據(jù)內(nèi)容為:', recv_content)
request_list = recv_content.split(" ", maxsplit=2) # 根據(jù)指定字符串進(jìn)行分割,最大分割次數(shù)為2
request_path = request_list[1] # 獲取請(qǐng)求資源路徑
print('請(qǐng)求路徑為:', request_path)
if request_path == "/": # 判斷請(qǐng)求的是否是根目錄,若是則返回首頁指定數(shù)據(jù)
request_path = "/index.html"
try:
with open('C:/Users/username/Desktop/ubuntu' + request_path, 'rb') as file: # 動(dòng)態(tài)打開指定文件
file_data = file.read() # 讀取指定文件數(shù)據(jù)
except Exception as e: # 請(qǐng)求異常,資源不存在,返回指定404錯(cuò)誤數(shù)據(jù)
with open('C:/Users/username/Desktop/ubuntu/error.html', 'rb') as file: # 打開指定錯(cuò)誤文件
error_data = file.read() # 讀取指定錯(cuò)誤數(shù)據(jù)
response_line = "HTTP/1.1 404 Not Found??!\r\n" # 響應(yīng)行
response_header = "Server: PWS1.0 服務(wù)器名稱及版本……\r\n" # 響應(yīng)頭
response_body = error_data # 響應(yīng)體
response_data = (response_line + response_header + '\r\n').encode('utf-8') + response_body # 拼接響應(yīng)報(bào)文
comm_socket.send(response_data) # 發(fā)送數(shù)據(jù)給瀏覽器
else:
response_line = "HTTP/1.1 200 OK # 成功??!\r\n"
response_header = "Server: PWS1.0 # 服務(wù)器名稱版本!\r\n"
response_body = file_data
response_data = (response_line + response_header + '\r\n').encode('utf-8') + response_body
comm_socket.send(response_data)
finally:
comm_socket.close() # 關(guān)閉服務(wù)于客戶端的套接字
if __name__ == '__main__':
main()
輸出如下
404錯(cuò)誤輸出:
獲取到的數(shù)據(jù)內(nèi)容為: GET /ab.txt HTTP/1.1
Host: 127.0.0.1:9000
Connection: keep-alive
Cache-Control: max-age=0
sec-ch-ua: "Google Chrome";v="105", "Not)A;Brand";v="8", "Chromium";v="105"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Cookie: csrftoken=1wb7v0r0BQuokJCxRS4JAO2XApvrHXFP90t2PiYb0mz7AwWS0NoKTi0zNaIjOfTl; Hm_lvt_18f619820855042bca58b32408f44de7=1658219884
請(qǐng)求路徑為: /ab.txt
未獲取到請(qǐng)求數(shù)據(jù)!
正確響應(yīng)輸出:
獲取到的數(shù)據(jù)內(nèi)容為: GET /index.html HTTP/1.1
Host: 127.0.0.1:9000
Connection: keep-alive
……
…… # 中間部分同錯(cuò)誤輸出
請(qǐng)求路徑為: /index.html
未獲取到請(qǐng)求數(shù)據(jù)!
錯(cuò)誤響應(yīng)
正確響應(yīng)文章來源:http://www.zghlxwxcb.cn/news/detail-438418.html
??學(xué)習(xí)導(dǎo)航:http://xqnav.top/文章來源地址http://www.zghlxwxcb.cn/news/detail-438418.html
到了這里,關(guān)于python自帶靜態(tài)web服務(wù)器搭建代碼實(shí)現(xiàn)(一)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!