由于主机名为中文导致的 flask 服务起不来,报错如下:
File "D:\work\python3.9_64\lib\socket.py", line 791, in getfqdn
hostname, aliases, ipaddrs = gethostbyaddr(name)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 2: invalid start byte
最简单的解决方法是:
修改计算机名为英文,然后重启计算机。

修改源码解决问题请网下看。
计算机名查看:

根据报错的位置查看文件为:

首先 getfqdn() 这个方法是为了获取包含域名的计算机名,测试是用的英文计算机名。

>>> import socket
>>> socket.getfqdn("127.0.0.1")
'lanzao.xxx.com.cn'

Get fully qualified domain name from name.
An empty argument is interpreted as meaning the local host.
First the hostname returned by gethostbyaddr() is checked, then
possibly existing aliases. In case no FQDN is available, hostname
from gethostname() is returned.
译文:
从名称中获得完全合格的域名。
空参数被解释为表示本地主机。
首先检查gethostbyaddr()返回的主机名,然后
可能现有的别名。如果没有可用的FQDN,请输入主机名
从gethostname()返回。

用英文计算机名进行测试内部方法:

>>> socket.gethostbyaddr("127.0.0.1")
('lanzao.xxx.com.cn', [], ['127.0.0.1'])
>>> socket.gethostbyaddr("lanzao")
('lanzao.xxx.com.cn', [], ['fexx::a9xx:7fxx:15xx:5fxx'])
>>> socket.gethostname()
'lanzao'

中文情况下 gethostbyaddr() 报错,gethostname() 不会。
gethostbyaddr() 方法是封装在 __socket__.pyd 包里的。
如果要彻底修改就涉及反编译了。

我这里直接对现有方法进行了改动,如果是中文计算机名,这里直接返回计算机名就可以了。
本来没有域名的情况下返回的也是计算机名,只是针对这种中文的待域名的情况下,只能返回中文计算机名,这种场景比较少,而且如果我们的生产环境没有获取这种中文计算机名+域名的需求,这样改动几乎没有影响。

相应代码如下:

def getfqdn(name=''):"""Get fully qualified domain name from name.An empty argument is interpreted as meaning the local host.First the hostname returned by gethostbyaddr() is checked, thenpossibly existing aliases. In case no FQDN is available, hostnamefrom gethostname() is returned."""try:name = name.strip()if not name or name == '0.0.0.0':name = gethostname()try:hostname, aliases, ipaddrs = gethostbyaddr(name)except error:passelse:aliases.insert(0, hostname)for name in aliases:if '.' in name:breakelse:name = hostnamereturn nameexcept Exception as e:print(e)return gethostname()   # 仅返回计算机名,无域名

至此,问题解决,flask、socket 服务顺利起来,毫无影响。
喜欢的点个赞❤吧!

Python 技术篇 - 修改源码解决中文主机名导致的flask、socket服务起不来问题: ‘utf-8‘ codec can‘t decode byte 0xc0 in position...相关推荐

  1. UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xc0 in position 0: invalid start byte报错解决

    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 0: invalid start byte报错解决 这个错误一 ...

  2. 解决“UnicodeDecodeError: 'ascii' codec can't decode byte 0xc0 in position 7: ordi”

    因为同时安装了python2和python3,所以出现了下面这个错误 发现这个问题还经历过很多次,然后在网上找了一下原因和解决方案,发现这个不错 https://www.cnblogs.com/ing ...

  3. Python之——UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 0 解决办法

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/78976807 最近在用Python处理一些中文数据时,报出了如下错误: Unico ...

  4. Pandas读取中文文本文件报错:python ‘utf-8‘ codec can‘t decode byte 0xe3 in position 0: unexpected end of data

    近日用pandas的read_csv读取中文文本文件时报错:python 'utf-8' codec can't decode byte 0xe3 in position 0: unexpected ...

  5. python报错UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x97 in position的解决方法

    在编写代码时,调用python解释器中的模块时出现 UnicodeDecodeError: 'gbk' codec can't decode byte 0x97 in position 20: ill ...

  6. 解决Python报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 658: illegal multibyte

    解决Python报错–UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 658: illegal multibyte ...

  7. Python 读取文件时报‘utf-8‘ codec can‘t decode byte 0xfc in position xxxx 的解决方法

    今天工作内容中要统计一个120万行的文件以 '> ' 开头的行的数量,写个Python脚本来解决: import os, sys ,rewith open('d:/test/xxxx/e.log ...

  8. python中txt文件读取错误原因以及解决办法‘gbk‘ codec can‘t decode byte 0x80 in position 2: illegal multibyte sequence

    python的读写文件操作时,有时候会出现一些小问题,详细如下,(原因在前面,解决办法在文末). 读取txt文件时出现错误反馈如下: Traceback (most recent call last) ...

  9. python 报错 UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xd3 in position 解决方法

    在使用urllib获取reqest的response的时候,还要进行解码 content = urllib.request.urlopen(request).read().decode() 当执行该语 ...

最新文章

  1. 深度学习:Opencv的BlobfromImage如何工作
  2. ELK教程3:logstash的部署、SpringBoot整合ELK+Filebeat
  3. oracle 被另一用户锁定,Oracle报错记录被另外一个用户锁定的解决方案
  4. 理解并从头搭建redis集群
  5. 案例分享,从0到1了解一个完整项目
  6. 开发可扩展的Web API
  7. 安卓虚拟机启动后报错: 类似 SDK Manager] Error: Error parsing .....devices.xml 解决方案...
  8. OpenStack 的诞生
  9. Structs 2 session 学习
  10. OLED(经典0.96英寸)--4SPI--SSD1306控制原理(含常用芯片_oled例程)
  11. crackme 004
  12. Target folder is neither empty nor does it point to an existing SDK installtion.
  13. 多旋翼无人机控制之完整闭环控制设计
  14. 【单片机毕业设计】【mcuclub-jj-035】基于单片机的保险柜的设计
  15. 极智AI | Attention 中 torch.chunk 的 TensorRT 实现
  16. switchport trunk native 的原理与作用
  17. 2048小游戏HTML网页版源码共享
  18. 无敌哥-创新设计思维
  19. 小鸟云虚拟主机Wordpress上传中文附件出现乱码
  20. DA14580的AD转换

热门文章

  1. BiliBili 第三方 Android 客户端应用源码
  2. Android笔记之FragmentTabHost实现选项卡
  3. How I can Built A-Z index site map in my website
  4. android 分区layout以及虚拟内存布局-小结
  5. AndroidApplication Fundamentals(Android应用基础)
  6. Equinix公司在巴西圣保罗开通了一个数据中心
  7. memset函数源码实现
  8. Android百分比布局初探
  9. 在LINUX下架设防火墙
  10. perl模块的安装,查询