问题出现:
在python项目中使用flask模块开启http端口失败,日志如下
File "C:\Users\hello\Desktop\py_project\lib\socket.py", line 676, in getfqdn
hostname, aliases, ipaddrs = gethostbyaddr(name)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position 0: invalid continuation byte
原因分析:
查看socket.py中源码如下
try:
hostname, aliases, ipaddrs = gethostbyaddr(name)
except error:
pass
原因1:其中 gethostbyaddr(name)会在计算机名为中文(不是用户名)的时候报错UnicodeDecodeError
原因2:except error并不能捕捉异常,依然会在外层调用时出错
解决方法:
方法1:修改计算机名为中文名(方法看最后)
方法2:将上述代码块修改为如下即可
except Exception as error 不会让这个异常扩散到外部
try:
hostname, aliases, ipaddrs = gethostbyaddr(name)
except Exception as error:
name = "hello" #自定义英文的计算机名
如何查看并更改windows计算机名?
此电脑 --> 属性 即可查看以下界面
注意:更改计算机名后重启才会生效!!!
补充:
查看计算机名法2:
在命令行(cmd)中输入hostname
C:\Users\hello>hostname
大哥的电脑