如何使用Python SimpleHTTPServer提供UTF-8编码的文件?

时间:2023-01-15 10:23:17

I often use the following to quickly fire up a web server to serve HTML content from the current folder (for local testing):

我经常使用以下方法快速启动web服务器,从当前文件夹(用于本地测试)中服务HTML内容:

python -m SimpleHTTPServer 8000

Is there a reasonably simple way I can do this, but have the server serve the files with a UTF-8 encoding rather than the system default?

是否有一种相当简单的方法可以做到这一点,但是让服务器使用UTF-8编码而不是系统默认值来服务文件?

1 个解决方案

#1


14  

Had the same problem, the following code worked for me.

有同样的问题,下面的代码对我有效。

To start a SimpleHTTPServer with UTF-8 encoding, simply copy/paste the following in terminal.

要启动一个带有UTF-8编码的SimpleHTTPServer,只需在终端中复制/粘贴以下内容。

python -c "import SimpleHTTPServer; m = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map; m[''] = 'text/plain'; m.update(dict([(k, v + ';charset=UTF-8') for k, v in m.items()])); SimpleHTTPServer.test();"

Ensure that you have the correct charset in your HTML files beforehand.

确保在HTML文件中预先拥有正确的字符集。

#1


14  

Had the same problem, the following code worked for me.

有同样的问题,下面的代码对我有效。

To start a SimpleHTTPServer with UTF-8 encoding, simply copy/paste the following in terminal.

要启动一个带有UTF-8编码的SimpleHTTPServer,只需在终端中复制/粘贴以下内容。

python -c "import SimpleHTTPServer; m = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map; m[''] = 'text/plain'; m.update(dict([(k, v + ';charset=UTF-8') for k, v in m.items()])); SimpleHTTPServer.test();"

Ensure that you have the correct charset in your HTML files beforehand.

确保在HTML文件中预先拥有正确的字符集。