def list_directory(self, path):
"""Helper to produce a directory listing (absent index.html).
Return value is either a file object, or None (indicating an
error). In either case, the headers are sent, making the
interface the same as for send_head().
"""
try:
list = os.listdir(path)
except OSError:
self.send_error(
HTTPStatus.NOT_FOUND,
"No permission to list directory")
return None
list.sort(key=lambda a: a.lower())
r = []
try:
displaypath = urllib.parse.unquote(self.path,
errors='surrogatepass')
except UnicodeDecodeError:
displaypath = urllib.parse.unquote(path)
displaypath = html.escape(displaypath)
enc = "gb18030"; # sys.getfilesystemencoding()
默认返回"mbcs“,这对于某些特殊CJK字符抛出错误:
File "C:\Users\chenzx\AppData\Local\Programs\Python\Python35\lib\http\server.py", line 755, in list_directory
encoded = '\n'.join(r).encode(enc, 'surrogateescape')
UnicodeEncodeError: 'mbcs' codec can't encode character '\ub2e4' in position 509: invalid character