python自定义html,自定义简单的Python HTTP服务器不提供css文件

I had found written in python, a very simple http server, it's do_get method looks like this:

def do_GET(self):

try:

self.send_response(200)

self.send_header('Content-type', 'text/html')

self.end_headers();

filepath = self.path

print filepath, USTAW['rootwww']

f = file("./www" + filepath)

s = f.readline();

while s != "":

self.wfile.write(s);

s = f.readline();

return

except IOError:

self.send_error(404,'File Not Found: %s ' % filepath)

It works ok, besides the fact - that it is not serving any css files ( it is rendered without css). Anyone got a suggestion / solution for this quirk?

Best regards,

praavDa

解决方案

it seems to be returning the html mimetype for all files:

self.send_header('Content-type', 'text/html')

Also, it seems to be pretty bad. Why are you interested in this sucky server? Look at cherrypy or paste for good python implementations of HTTP server and a good code to study.

EDIT: Trying to fix it for you:

import os

import mimetypes

#...

def do_GET(self):

try:

filepath = self.path

print filepath, USTAW['rootwww']

f = open(os.path.join('.', 'www', filepath))

except IOError:

self.send_error(404,'File Not Found: %s ' % filepath)

else:

self.send_response(200)

mimetype, _ = mimetypes.guess_type(filepath)

self.send_header('Content-type', mimetype)

self.end_headers()

for s in f:

self.wfile.write(s)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值