多程序web伺服器---01
阿新 • • 發佈:2018-12-13
程式碼流程
main函式設計:
-
建立套接字
-
將套接字設定成四次揮手不出bug
-
繫結套接字
-
把套接字變為監聽套接字
-
迴圈等待客戶端連結
-
接受套接字accept
-
new_socket, client_addr = tcp_server_socket.accept()
-
開啟程序
-
關閉新套接字
-
關閉監聽套接字
service_client函式
- 新套接字recv
- 分割字串
- 字串匹配
- 開啟html檔案
- 新套接字關閉
程式碼
import socket
import re
import multiprocessing
def service_client(new_socket):
"""為這個客戶端返回資料"""
# 1. 接收瀏覽器傳送過來的請求 ,即http請求
# GET / HTTP/1.1
# .....
request = new_socket.recv(1024).decode("utf-8")
# print(">>>"*50)
# print(request)
request_lines = request.splitlines( )
print("")
print(">" * 20)
print(request_lines)
# GET /index.html HTTP/1.1
# get post put del
file_name = ""
ret = re.match(r"[^/]+(/[^ ]*)", request_lines[0])
if ret:
file_name = ret.group(1)
# print("*"*50, file_name)
if file_name == "/":
file_name = "/index.html"
# 2. 返回http格式的資料,給瀏覽器
try:
f = open("./html" + file_name, "rb")
except:
response = "HTTP/1.1 404 NOT FOUND\r\n"
response += "\r\n"
response += "------file not found-----"
new_socket.send(response.encode("utf-8"))
else:
html_content = f.read()
f.close()
# 2.1 準備傳送給瀏覽器的資料---header
response = "HTTP/1.1 200 OK\r\n"
response += "\r\n"
# 2.2 準備傳送給瀏覽器的資料---boy
# response += "hahahhah"
# 將response header傳送給瀏覽器
new_socket.send(response.encode("utf-8"))
# 將response body傳送給瀏覽器
new_socket.send(html_content)
# 關閉套接
new_socket.close()
def main():
"""用來完成整體的控制"""
# 1. 建立套接字
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# 2. 繫結
tcp_server_socket.bind(("172.16.70.64", 7890))
# 3. 變為監聽套接字
tcp_server_socket.listen(128)
while True:
# 4. 等待新客戶端的連結
new_socket, client_addr = tcp_server_socket.accept()
# 開一個程序
p = multiprocessing.Process(target=service_client, args=(new_socket,))
p.start()
new_socket.close()
# # 5. 為這個客戶端服務
# service_client(new_socket)
# 關閉監聽套接字
tcp_server_socket.close()
if __name__ == "__main__":
main()
面向物件改造
import socket
import re
import multiprocessing
class WSGIserver(object):
def __init__(self):
# 1. 建立套接字
self.tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# 2. 繫結
self.tcp_server_socket.bind(("172.16.70.64", 7890))
# 3. 變為監聽套接字
self.tcp_server_socket.listen(128)
def service_client(self,new_socket):
"""為這個客戶端返回資料"""
# 1. 接收瀏覽器傳送過來的請求 ,即http請求
# GET / HTTP/1.1
# .....
request = new_socket.recv(1024).decode("utf-8")
# print(">>>"*50)
# print(request)
request_lines = request.splitlines()
print("")
print(">" * 20)
print(request_lines)
# GET /index.html HTTP/1.1
# get post put del
file_name = ""
ret = re.match(r"[^/]+(/[^ ]*)", request_lines[0])
if ret:
file_name = ret.group(1)
# print("*"*50, file_name)
if file_name == "/":
file_name = "/index.html"
# 2. 返回http格式的資料,給瀏覽器
try:
f = open("./html" + file_name, "rb")
except:
response = "HTTP/1.1 404 NOT FOUND\r\n"
response += "\r\n"
response += "------file not found-----"
new_socket.send(response.encode("utf-8"))
else:
html_content = f.read()
f.close()
# 2.1 準備傳送給瀏覽器的資料---header
response = "HTTP/1.1 200 OK\r\n"
response += "\r\n"
# 2.2 準備傳送給瀏覽器的資料---boy
# response += "hahahhah"
# 將response header傳送給瀏覽器
new_socket.send(response.encode("utf-8"))
# 將response body傳送給瀏覽器
new_socket.send(html_content)
# 關閉套接
new_socket.close()
def runforever(self):
"""用來完成整體的控制"""
while True:
# 4. 等待新客戶端的連結
new_socket, client_addr = self.tcp_server_socket.accept()
# 開一個程序
p = multiprocessing.Process(target=self.service_client, args=(new_socket,))
p.start()
new_socket.close()
# # 5. 為這個客戶端服務
# service_client(new_socket)
# 關閉監聽套接字
tcp_server_socket.close()
def main():
"""
控制整體,建立一個web伺服器物件,然後呼叫這個物件的run方法
:return:None
"""
wsgi_server=WSGIserver()
wsgi_server.runforever()
if __name__=="__main__":
main()
執行結果
前端介面是下載的網路資源
上傳git
- echo“#mini_web”>> README.md 建立readme
- git init #初始化倉庫
- git add README.md #提交到暫存庫
- git commit -m “first commit” #提交到儲存庫
×author:[email protected] 歡迎交流
×github:https://github.com/zhangyuespec/mini_web