Skip to content

Commit

Permalink
bugfix: default encoding: ascii
Browse files Browse the repository at this point in the history
  • Loading branch information
freelamb committed Mar 16, 2022
1 parent 888b33b commit 87cb147
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions simple_http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
and HEAD requests in a fairly straightforward manner.
"""

__version__ = "0.3.0"
__version__ = "0.3.1"
__author__ = "freelamb@126.com"
__all__ = ["SimpleHTTPRequestHandler"]

Expand All @@ -32,6 +32,8 @@
from http.server import BaseHTTPRequestHandler
else:
# Python2
reload(sys)
sys.setdefaultencoding('utf-8')
from urllib import quote
from urllib import unquote
from BaseHTTPServer import HTTPServer
Expand Down Expand Up @@ -205,8 +207,7 @@ def list_directory(self, path):
if os.path.islink(fullname):
display_name = name + "@"
# Note: a link to a directory displays with @ and links with /
f.write(b'<li><a href="%s">%s</a>\n' %
(quote(linkname).encode('utf-8'), escape(display_name).encode('utf-8')))
f.write(b'<li><a href="%s">%s</a>\n' % (quote(linkname).encode('utf-8'), escape(display_name).encode('utf-8')))
f.write(b"</ul>\n<hr>\n</body>\n</html>\n")
length = f.tell()
f.seek(0)
Expand Down Expand Up @@ -289,6 +290,7 @@ def main():
httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)
server = httpd.socket.getsockname()
print("server_version: " + SimpleHTTPRequestHandler.server_version + ", python_version: " + SimpleHTTPRequestHandler.sys_version)
print("sys encoding: " + sys.getdefaultencoding())
print("Serving http on: " + str(server[0]) + ", port: " + str(server[1]) + " ... (http://" + server[0] + ":" + str(server[1]) + "/)")
httpd.serve_forever()

Expand Down

0 comments on commit 87cb147

Please sign in to comment.