Skip to content

Commit 87cb147

Browse files
author
freelamb
committed
bugfix: default encoding: ascii
1 parent 888b33b commit 87cb147

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

simple_http_server.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
and HEAD requests in a fairly straightforward manner.
77
"""
88

9-
__version__ = "0.3.0"
9+
__version__ = "0.3.1"
1010
__author__ = "freelamb@126.com"
1111
__all__ = ["SimpleHTTPRequestHandler"]
1212

@@ -32,6 +32,8 @@
3232
from http.server import BaseHTTPRequestHandler
3333
else:
3434
# Python2
35+
reload(sys)
36+
sys.setdefaultencoding('utf-8')
3537
from urllib import quote
3638
from urllib import unquote
3739
from BaseHTTPServer import HTTPServer
@@ -205,8 +207,7 @@ def list_directory(self, path):
205207
if os.path.islink(fullname):
206208
display_name = name + "@"
207209
# Note: a link to a directory displays with @ and links with /
208-
f.write(b'<li><a href="%s">%s</a>\n' %
209-
(quote(linkname).encode('utf-8'), escape(display_name).encode('utf-8')))
210+
f.write(b'<li><a href="%s">%s</a>\n' % (quote(linkname).encode('utf-8'), escape(display_name).encode('utf-8')))
210211
f.write(b"</ul>\n<hr>\n</body>\n</html>\n")
211212
length = f.tell()
212213
f.seek(0)
@@ -289,6 +290,7 @@ def main():
289290
httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)
290291
server = httpd.socket.getsockname()
291292
print("server_version: " + SimpleHTTPRequestHandler.server_version + ", python_version: " + SimpleHTTPRequestHandler.sys_version)
293+
print("sys encoding: " + sys.getdefaultencoding())
292294
print("Serving http on: " + str(server[0]) + ", port: " + str(server[1]) + " ... (http://" + server[0] + ":" + str(server[1]) + "/)")
293295
httpd.serve_forever()
294296

0 commit comments

Comments
 (0)