4 回答

TA貢獻1712條經驗 獲得超3個贊
這樣做真的很復雜,因為你必須創建新的服務器來服務你的 css 文件。**最好使用強大且流行的解決方案,例如 Flask 和 Django **,您可以在其中輕松配置這些文件。

TA貢獻1946條經驗 獲得超4個贊
嘿,我必須自己解決這個問題。您可能已經解決了這個問題,但以防萬一其他人偶然發現這個問題,這就是我想出的方法,效果很好!
def do_GET(self):
# Cache request
path = self.path
# Validate request path, and set type
if path == "/resources/index.html":
type = "text/html"
elif path == "/resources/script.js":
type = "text/javascript"
elif path == "/resources/style.css":
type = "text/css"
elif path == "/favicon.ico":
path = "/resources/favicon.ico"
type = "image/x-icon"
else:
# Wild-card/default
if not path == "/":
print("UNRECONGIZED REQUEST: ", path)
path = "/resources/index.html"
type = "text/html"
# Set header with content type
self.send_response(200)
self.send_header("Content-type", type)
self.end_headers()
# Open the file, read bytes, serve
with open(path[1:], 'rb') as file:
self.wfile.write(file.read()) # Send

TA貢獻1804條經驗 獲得超2個贊
當您的 HTML 文件加載到瀏覽器中時,它會嘗試加載 CSS 文件 ( ./output.css
)。由于您通過 HTTP 提供此頁面,瀏覽器會發出 HTTP 請求來獲取此 output.css 文件,但顯然您的服務器不提供此 CSS 文件。
添加回答
舉報