`
kanpiaoxue
  • 浏览: 1744996 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多

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

参考资料: https://stackoverflow.com/questions/15288891/how-can-i-serve-files-with-utf-8-encoding-using-python-simplehttpserver

 

python2

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();" 8001

 

python3

python3 -c "from http.server import test, SimpleHTTPRequestHandler as RH; RH.extensions_map={k:v+';charset=UTF-8' for k,v in RH.extensions_map.items()}; test(RH,port=8002)"

 

 

原文如下: 

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 (for Python 2).

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.

EDIT: Update for Python 3:

python3 -c "from http.server import test, SimpleHTTPRequestHandler as RH; RH.extensions_map={k:v+';charset=UTF-8' for k,v in RH.extensions_map.items()}; test(RH)"

The test function also accepts arguments like port and bind so that it's possible to specify the address and the port to listen on.

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics