文章目录
- 环境
- 代码
- 报错Track
- 解决思路
环境
- acondana 24.1.2
- python 3.7.13 32bit
- flask 2.2.3
- gevent 21.8.0
代码
port = 7236
logging.basicConfig(level=logging.INFO, # 控制台打印的日志级别filename='./logs/app.log', # 将日志写入log_new.log文件中filemode='a', # 模式,有w和a,w就是写模式,每次都会重新写日志,覆盖之前的日志 a是追加模式,默认如果不写的话,就是追加模式format="%(asctime)s:%(levelname)s:%(name)s -- %(message)s", datefmt="%Y/%m/%d %H:%M:%S" # 日志格式)
server = pywsgi.WSGIServer(('0.0.0.0', port), app, handler_class=WebSocketHandler)
server.serve_forever()
报错Track
Process Process-1:
Traceback (most recent call last):File "C:\Users\Lenovo\.conda\envs\python37_32\lib\multiprocessing\process.py", line 297, in _bootstrapself.run()File "C:\Users\Lenovo\.conda\envs\python37_32\lib\multiprocessing\process.py", line 99, in runself._target(*self._args, **self._kwargs)File "C:\Jexhen\WorkSpace\cjspd_print_plugin_win7_32\cjspd_print_client.py", line 63, in run_serverserver.serve_forever()File "C:\Users\Lenovo\.conda\envs\python37_32\lib\site-packages\gevent\baseserver.py", line 398, in serve_foreverself.start()File "C:\Users\Lenovo\.conda\envs\python37_32\lib\site-packages\gevent\baseserver.py", line 336, in startself.init_socket()File "C:\Users\Lenovo\.conda\envs\python37_32\lib\site-packages\gevent\pywsgi.py", line 1546, in init_socketself.update_environ()File "C:\Users\Lenovo\.conda\envs\python37_32\lib\site-packages\gevent\pywsgi.py", line 1558, in update_environname = socket.getfqdn(address[0])File "C:\Users\Lenovo\.conda\envs\python37_32\lib\site-packages\gevent\_socketcommon.py", line 304, in getfqdnhostname, aliases, _ = gethostbyaddr(name)File "C:\Users\Lenovo\.conda\envs\python37_32\lib\site-packages\gevent\_socketcommon.py", line 276, in gethostbyaddrreturn get_hub().resolver.gethostbyaddr(ip_address)File "C:\Users\Lenovo\.conda\envs\python37_32\lib\site-packages\gevent\resolver\thread.py", line 66, in gethostbyaddrreturn self.pool.apply(_socket.gethostbyaddr, args, kwargs)File "C:\Users\Lenovo\.conda\envs\python37_32\lib\site-packages\gevent\pool.py", line 161, in applyreturn self.spawn(func, *args, **kwds).get()File "src/gevent/event.py", line 329, in gevent._gevent_cevent.AsyncResult.getFile "src/gevent/event.py", line 359, in gevent._gevent_cevent.AsyncResult.getFile "src/gevent/event.py", line 347, in gevent._gevent_cevent.AsyncResult.getFile "src/gevent/event.py", line 327, in gevent._gevent_cevent.AsyncResult._raise_exceptionFile "C:\Users\Lenovo\.conda\envs\python37_32\lib\site-packages\gevent\_compat.py", line 65, in reraiseraise value.with_traceback(tb)File "C:\Users\Lenovo\.conda\envs\python37_32\lib\site-packages\gevent\threadpool.py", line 167, in __run_taskthread_result.set(func(*args, **kwargs))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc1 in position 0: invalid start byte
解决思路
- 先去gevent的github查找相关issued
gevent github issue#1717
- 作者的意思说gevent只是对socket进行了封装,调用的还是socket的方法和gevent没有关系
- 自己按照作者思路,确实报一样的错误
import socket
socket.getfqdn('0.0.0.0')
Traceback (most recent call last):File "<stdin>", line 1, in <module>File "C:\Users\Lenovo\.conda\envs\python37_32\lib\socket.py", line 676, in getfqdnhostname, aliases, ipaddrs = gethostbyaddr(name)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc1 in position 0: invalid start byte
-
再次搜索相关博客得到答案
如果电脑名是中文,Python中调用gethostbyaddr(name)函数会出错
查看我的主机名确实是中文名,改成英文重启不会报错,但不是最终解决方案! -
于是根据报错路径查看gevent的源码
"C:\Users\Lenovo\.conda\envs\python37_32\lib\site-packages\gevent\_socketcommon.py", line 304, in getfqdnhostname, aliases, _ = gethostbyaddr(name)
- 修改gevent的源码
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."""# pylint: disable=undefined-variablename = name.strip()if not name or name == '0.0.0.0':name = gethostname()try:hostname, aliases, _ = gethostbyaddr(name.encode('ascii','ignore'))# 主要是修改这里except error:passelse:aliases.insert(0, hostname)for name in aliases: # EWW! pylint:disable=redefined-argument-from-localif isinstance(name, bytes):if b'.' in name:breakelif '.' in name:breakelse:name = hostnamereturn name