我创建了一个小型的Python HTML服务器,但是我在发送外部CSS和JavaScript方面遇到了问题。HTML正常传输且内联CSS也能正常工作。Chrome开发者工具显示以下错误信息:
资源被解释为样式表,但 MIME 类型却为 text/plain:"http://localhost:8888/style.css"
不幸的是,我不知道"MIME类型"是什么。
以下是Python代码:
# server.py
import socket
file = open('website/index.html', 'r')
def start_server(HOST, PORT):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(1)
print('Serving HTTP on port %s ...' % PORT)
while True:
client_connection, client_address = s.accept()
request = client_connection.recv(1024)
print(request.decode('utf-8'))
http_response = """\
http/1.1 200 OK
""" + file.read() + """
"""
client_connection.sendall(bytes(http_response, 'utf-8'))
client_connection.close()
Connection: close
,因为你没有持久连接。 - UltraInstinct