【发布时间】:2022-03-31 15:55:28
【问题描述】:
我有一个从 python 运行的简单 http 服务器,它返回一个 HTML 文件作为 GET 请求。 HTMl 文件只有一些输入,并且它被正确发送,但即使它链接到 CSS 文件也没有样式。这是server.py:
from http.server import BaseHTTPRequestHandler, HTTPServer
import time
hostName = "localhost"
serverPort = 8080
class MyServer(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
h = open("main.html", "rb")
self.wfile.write(h.read())
def do_POST(s):
if s.path == '/':
s.path = './main.html'
if __name__ == "__main__":
webServer = HTTPServer((hostName, serverPort), MyServer)
print("Server started http://%s:%s" % (hostName, serverPort))
try:
webServer.serve_forever()
except KeyboardInterrupt:
pass
webServer.server_close()
print("Server stopped.")
main.html 是这个
<html>
<head>
<link rel="stylesheet" href="./output.css">
</head>
<body>
<h1 class="mx-auto text-center mt-8 px-0 py-8 border border-4 border-solid border-gray-600"
style="width: 700!important;">CHECKBOX INPUT</h1>
<div class="flex h-full mx-auto">
<form action="">
<div class="w-3/4 py-10 px-8">
<table class="table-auto">
<thead>
<tr>
<th class="py-10 h-4">
<div class="mr-64">
<input type="checkbox" class="form-checkbox h-8 w-8">
<label class="ml-4">test</label>
</div>
</th>
</tr>
<tr>
<th class="py-10 h-4">
<div class="mr-64">
<input type="checkbox" class="form-checkbox h-8 w-8">
<label class="ml-4"></label>
</div>
</th>
</tr>
<tr>
<th class="py-10 h-4">
<div class="mr-64">
<input type="checkbox" class="form-checkbox h-8 w-8">
<label class="ml-4">test</label>
</div>
</th>
</tr>
<tr>
<th class="py-10 h-4">
<div class="mr-64">
<input type="checkbox" class="form-checkbox h-8 w-8">
<label class="ml-4">test</label>
</div>
</th>
</tr>
<tr>
<th class="px-4 py-10 h-4">
<div class="mx-auto">
<span>TEXT INPUT:</span>
<input type="text" class="form-input mt-4">
<select>
<option value="value1" selected>DROPDOWN</option>
<option value="valeu2">Value 2</option>
<option value="valeu3">Value 3</option>
</select>
</div>
</th>
</tr>
<tr>
<th class="px-4 py-10 h-4">
<div class="mx-auto">
</div>
</th>
</tr>
<tr>
<th class="px-4 py-10 h-4">
<div class="mx-auto">
<input type="submit" value="Submit" class="bg-gray-600 p-4 border-0 border-solid rounded-lg">
</div>
</th>
</tr>
</thead>
</table>
</div>
</form>
</div>
</body>
</html>
即使在我托管服务器时 HTMl 文件链接到 output.css,它也会返回没有任何样式的 HTML 文件
提前谢谢你
【问题讨论】:
-
您是否尝试过使用不带前导点的
href="/output.css"?基本上只有一个前导/的 href 让浏览器尝试在找到 html 的同一主机和端口上获取资源。将该 href 放入提供的文档 @http://example.com:8000/bar.html将导致获取http://example.com:8000/output.css但是您doGet可能需要根据请求 URL 在 main.html 和 output.css 之间做出决定。