我正在編寫一個非常基本的網絡服務器(當然,嘗試),雖然它現在提供HTML罰款,但我的CSS文件似乎完全不被識別。我的機器上也運行了Apache2,並且當我將文件複製到docroot時,頁面正確地被傳送。我也檢查了權限,他們似乎很好。這裏是我到目前爲止的代碼:BaseHTTPServer無法識別CSS文件
class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
try:
if self.path == "/":
self.path = "/index.html"
if self.path == "favico.ico":
return
if self.path.endswith(".html"):
f = open(curdir+sep+self.path)
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(f.read())
f.close()
return
return
except IOError:
self.send_error(404)
def do_POST(self):
...
爲了提供CSS文件,我需要做些什麼嗎?
謝謝!
謝謝!我想這是這樣的,但不知道我是否需要一個單獨的案例。添加了這個,它像一個魅力工作! – tparrott 2010-07-20 14:57:37
這對我來說是非常有啓發性的代碼 - 謝謝,gnibbler。一個小問題:至少在python 2.7中,它是mimetypes.types_map(複數),而不是.type_map – mikeh 2010-09-22 21:29:14
@mikeh,謝謝我修復了它 – 2010-09-22 22:19:35