開發(fā)步驟:
1、編寫一個(gè)TCP服務(wù)端程序
2、獲取瀏覽器發(fā)送的HTTP請(qǐng)求報(bào)文數(shù)據(jù)
3、讀取固定頁面數(shù)據(jù),把頁面數(shù)據(jù)組裝HTTP響應(yīng)報(bào)文數(shù)據(jù)發(fā)送給瀏覽器
4、HTTP響應(yīng)報(bào)文數(shù)據(jù)發(fā)送完成之后,關(guān)閉服務(wù)于客戶端的套接字
import socket
if __name__ == '__main__':
# 1、編寫一個(gè)TCP服務(wù)端程序
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_server_socket.bind(("", 8080))
tcp_server_socket.listen(128)
while True:
# 2、獲取瀏覽器發(fā)送的HTTP請(qǐng)求報(bào)文
client_socket, client_addr = tcp_server_socket.accept()
client_request_data = client_socket.recv(1024).decode()
print(client_request_data)
# 3、讀取固定頁面數(shù)據(jù),把頁面數(shù)據(jù)組裝稱HTTP響應(yīng)報(bào)文數(shù)據(jù)發(fā)送給瀏覽器
# rb r 讀取數(shù)據(jù),b讀取二進(jìn)制的方式讀取文件
with open("./index.html", "rb") as f:
file_data = f.read()
# 組裝http響應(yīng)報(bào)文
response_line = "HTTP/1.1 200 OK \n"
response_header = "Server:pwb\n"
response_body = file_data
response_data = (response_line + response_header + "\n").encode() + response_body
client_socket.send(response_data)
client_socket.close()
和之前服務(wù)端編寫差不多,差別在于,首先客戶端不需要我們編寫了,是通過瀏覽器發(fā)送請(qǐng)求。
然后,因?yàn)闉g覽器遵循的是http請(qǐng)求,所以我們?cè)谟胹ocket進(jìn)行返回時(shí),需要遵循h(huán)ttp響應(yīng)的格式,將數(shù)據(jù)進(jìn)行返回。
目前為止,還沒有學(xué)習(xí)框架,不能像java的spring那樣。
目前端口是寫死在程序中,如果我們?cè)贚inux服務(wù)器中啟動(dòng)服務(wù)起,通過命令行的方式指定端口:
1、導(dǎo)入模塊
import sys
2、獲取命令終端輸入的數(shù)據(jù)
sys.argv
這樣就可以讀取命令行傳入的參數(shù),通過指定端口進(jìn)行啟動(dòng)。
最終版,通過命令行方式啟動(dòng)
import socket
import sys
import threading
class HttpWebServer:
def __init__(self, port):
self.tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.tcp_server_socket.bind(("", port))
self.tcp_server_socket.listen(128)
def handle_client_request(self, client_socket):
client_request_data = client_socket.recv(1024).decode()
print(client_request_data)
request_data = client_request_data.split(" ")
print(request_data)
if len(request_data) == 1:
client_socket.close()
return
request_path = request_data[1]
if request_path == "/":
request_path = "/index.html"
try:
with open("./static" + request_path, "rb") as f:
file_data = f.read()
except Exception as e:
response_line = "HTTP/1.1 404 Not Found \n"
response_header = "Server:pwb\n"
response_body = "404 Not Found sorry"
response_data = (response_line + response_header + "\n").encode() + response_body.encode()
client_socket.send(response_data)
else:
response_line = "HTTP/1.1 200 OK \n"
response_header = "Server:pwb\n"
response_body = file_data
response_data = (response_line + response_header + "\n").encode() + response_body
client_socket.send(response_data)
finally:
client_socket.close()
def start(self):
while True:
client_socket, client_addr = self.tcp_server_socket.accept()
print("客戶端地址:", client_addr)
sub_thread = threading.Thread(target=self.handle_client_request, args=(client_socket,))
sub_thread.start()
def main():
if len(sys.argv) != 2:
print("格式錯(cuò)誤 python xxx.py 9090")
return
if not sys.argv[1].isdigit():
print("格式錯(cuò)誤 python xxx.py 9090")
return
port = int(sys.argv[1])
my_web_server = HttpWebServer(port)
my_web_server.start()
if __name__ == '__main__':
main()
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧