Discuss / Python / 雪峰老师帮看下

雪峰老师帮看下

Topic source
# hello.py

def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    return [b'<h1>Hello, web!</h1>']
# server1.py
from wsgiref.simple_server import make_server
from hello import application

httpd = make_server('', 8000, application)
print('Serving HTTP on port 8000...')

httpd.serve_forever()

代码是照着你的, 用sublime text保存编码也是utf-8 这错误应该怎么解决?

D:\Python>python server1.py Traceback (most recent call last): File "server1.py", line 5, in <module> httpd = makeserver('', 8000, application) File "C:\Users\hasee\AppData\Local\Programs\Python\Python35\lib\wsgiref\simpleserver.py", line 153, in make_server server = server_class((host, port), handler_class) File "C:\Users\hasee\AppData\Local\Programs\Python\Python35\lib\socketserver.py", line 443, in __init self.server_bind() File "C:\Users\hasee\AppData\Local\Programs\Python\Python35\lib\wsgiref\simple_server.py", line 50, in server_bind HTTPServer.server_bind(self) File "C:\Users\hasee\AppData\Local\Programs\Python\Python35\lib\http\server.py", line 140, in server_bind self.server_name = socket.getfqdn(host) File "C:\Users\hasee\AppData\Local\Programs\Python\Python35\lib\socket.py", line 658, in getfqdn hostname, aliases, ipaddrs = gethostbyaddr(name) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7 in position 0: invalid continuation byte

廖雪峰

#2 Created at ... [Delete] [Delete and Lock User]

你的电脑主机名写了中文,改成英文重启后再试试

改了,好了,谢谢~可是为什么主机名有中文都不行呀QAQ

廖雪峰

#4 Created at ... [Delete] [Delete and Lock User]

windows默认是gbk编码,python解析主机名用的utf-8


  • 1

Reply