静态Web服务器-返回固定页面数据
学习目标
1. 开发自己的静态Web服务器
实现步骤:
- 编写一个TCP服务端程序
- 获取浏览器发送的http请求报文数据
- 读取固定页面数据,把页面数据组装成HTTP响应报文数据发送给浏览器。
- HTTP响应报文数据发送完成以后,关闭服务于客户端的套接字。
2. 静态Web服务器-返回固定页面数据的示例代码
import socket
if __name__ == '__main__':
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
tcp_server_socket.bind(("", 8000))
tcp_server_socket.listen(128)
while True:
new_socket, ip_port = tcp_server_socket.accept()
recv_data = new_socket.recv(4096)
print(recv_data)
with open("static/index.html", "r") as file:
file_data = file.read()
response_line = "HTTP/1.1 200 OK\r\n"
response_header = "Server: PWS/1.0\r\n"
response_body = file_data
response = response_line + response_header + "\r\n" + response_body
response_data = response.encode("utf-8")
new_socket.send(response_data)
new_socket.close()
3. 小结
- 编写一个TCP服务端程序
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while True:
conn_socket, ip_port = tcp_server_socket.accept()
- 获取浏览器发送的http请求报文数据
client_request_data = conn_socket.recv(4096)
- 读取固定页面数据,把页面数据组装成HTTP响应报文数据发送给浏览器。
response_data = (response_line + response_header + "\r\n").encode("utf-8") + response_body
conn_socket.send(response_data)
- HTTP响应报文数据发送完成以后,关闭服务于客户端的套接字。
conn_socket.close()
静态Web服务器-返回指定页面数据
学习目标
1. 静态Web服务器的问题
目前的Web服务器,不管用户访问什么页面,返回的都是固定页面的数据,接下来需要根据用户的请求返回指定页面的数据
返回指定页面数据的实现步骤:
- 获取用户请求资源的路径
- 根据请求资源的路径,读取指定文件的数据
- 组装指定文件数据的响应报文,发送给浏览器
- 判断请求的文件在服务端不存在,组装404状态的响应报文,发送给浏览器
2. 静态Web服务器-返回指定页面数据的示例代码
import socket
import os
def main():
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
tcp_server_socket.bind(("", 8000))
tcp_server_socket.listen(128)
while True:
new_socket, ip_port = tcp_server_socket.accept()
recv_data = new_socket.recv(4096)
if len(recv_data) == 0:
new_socket.close()
return
recv_content = recv_data.decode("utf-8")
print(recv_content)
request_list = recv_content.split(" ", maxsplit=2)
request_path = request_list[1]
print(request_path)
if request_path == "/":
request_path = "/index.html"
with open("static" + request_path, "rb") as file:
file_data = file.read()
response_line = "HTTP/1.1 200 OK\r\n"
response_header = "Server: PWS/1.0\r\n"
response_body = file_data
response = (response_line +
response_header +
"\r\n").encode("utf-8") + response_body
new_socket.send(response)
new_socket.close()
if __name__ == '__main__':
main()
3. 小结
- 获取用户请求资源的路径
request_list = client_request_conent.split(" ", maxsplit=2)
request_path = request_list[1]
- 根据请求资源的路径,读取请求指定文件的数据
with open("static" + request_path, "rb") as file:
file_data = file.read()
- 组装指定文件数据的响应报文,发送给浏览器
response_data = (response_line + response_header + "\r\n").encode("utf-8") + response_body
conn_socket.send(response_data)
- 判断请求的文件在服务端不存在,组装404状态的响应报文,发送给浏览器
try:
except Exception as e:
conn_socket.send(404响应报文数据)
静态Web服务器-返回404页面数据
import socket
import os
def main():
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
tcp_server_socket.bind(("", 8000))
tcp_server_socket.listen(128)
while True:
new_socket, ip_port = tcp_server_socket.accept()
recv_data = new_socket.recv(4096)
if len(recv_data) == 0:
new_socket.close()
return
recv_content = recv_data.decode("utf-8")
print(recv_content)
request_list = recv_content.split(" ", maxsplit=2)
request_path = request_list[1]
print(request_path)
if request_path == "/":
request_path = "/index.html"
try:
with open("static" + request_path, "rb") as file:
file_data = file.read()
except Exception as e:
response_line = "HTTP/1.1 404 Not Found\r\n"
response_header = "Server: PWS/1.0\r\n"
with open("static/error.html", "rb") as file:
file_data = file.read()
response_body = file_data
response = (response_line +
response_header +
"\r\n").encode("utf-8") + response_body
new_socket.send(response)
else:
response_line = "HTTP/1.1 200 OK\r\n"
response_header = "Server: PWS/1.0\r\n"
response_body = file_data
response = (response_line +
response_header +
"\r\n").encode("utf-8") + response_body
new_socket.send(response)
finally:
new_socket.close()
if __name__ == '__main__':
main()
加油!
感谢!
努力!
|