Flask gevent启动报错UnicodeDecodeError

发布于:2024-05-10 ⋅ 阅读:(26) ⋅ 点赞:(0)

环境

  • 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 _bootstrap
    self.run()
  File "C:\Users\Lenovo\.conda\envs\python37_32\lib\multiprocessing\process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Jexhen\WorkSpace\cjspd_print_plugin_win7_32\cjspd_print_client.py", line 63, in run_server
    server.serve_forever()
  File "C:\Users\Lenovo\.conda\envs\python37_32\lib\site-packages\gevent\baseserver.py", line 398, in serve_forever
    self.start()
  File "C:\Users\Lenovo\.conda\envs\python37_32\lib\site-packages\gevent\baseserver.py", line 336, in start
    self.init_socket()
  File "C:\Users\Lenovo\.conda\envs\python37_32\lib\site-packages\gevent\pywsgi.py", line 1546, in init_socket
    self.update_environ()
  File "C:\Users\Lenovo\.conda\envs\python37_32\lib\site-packages\gevent\pywsgi.py", line 1558, in update_environ
    name = socket.getfqdn(address[0])
  File "C:\Users\Lenovo\.conda\envs\python37_32\lib\site-packages\gevent\_socketcommon.py", line 304, in getfqdn
    hostname, aliases, _ = gethostbyaddr(name)
  File "C:\Users\Lenovo\.conda\envs\python37_32\lib\site-packages\gevent\_socketcommon.py", line 276, in gethostbyaddr
    return get_hub().resolver.gethostbyaddr(ip_address)
  File "C:\Users\Lenovo\.conda\envs\python37_32\lib\site-packages\gevent\resolver\thread.py", line 66, in gethostbyaddr
    return self.pool.apply(_socket.gethostbyaddr, args, kwargs)
  File "C:\Users\Lenovo\.conda\envs\python37_32\lib\site-packages\gevent\pool.py", line 161, in apply
    return self.spawn(func, *args, **kwds).get()
  File "src/gevent/event.py", line 329, in gevent._gevent_cevent.AsyncResult.get
  File "src/gevent/event.py", line 359, in gevent._gevent_cevent.AsyncResult.get
  File "src/gevent/event.py", line 347, in gevent._gevent_cevent.AsyncResult.get
  File "src/gevent/event.py", line 327, in gevent._gevent_cevent.AsyncResult._raise_exception
  File "C:\Users\Lenovo\.conda\envs\python37_32\lib\site-packages\gevent\_compat.py", line 65, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\Lenovo\.conda\envs\python37_32\lib\site-packages\gevent\threadpool.py", line 167, in __run_task
    thread_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 issued response
  • 作者的意思说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 getfqdn
    hostname, aliases, ipaddrs = gethostbyaddr(name)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc1 in position 0: invalid start byte
"C:\Users\Lenovo\.conda\envs\python37_32\lib\site-packages\gevent\_socketcommon.py", line 304, in getfqdn
    hostname, aliases, _ = gethostbyaddr(name)
  • 修改gevent的源码
    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, then
    possibly existing aliases. In case no FQDN is available, hostname
    from gethostname() is returned.
    """
    # pylint: disable=undefined-variable
    name = name.strip()
    if not name or name == '0.0.0.0':
        name = gethostname()
    try:
        hostname, aliases, _ = gethostbyaddr(name.encode('ascii','ignore'))# 主要是修改这里
    except error:
        pass
    else:
        aliases.insert(0, hostname)
        for name in aliases: # EWW! pylint:disable=redefined-argument-from-local
            if isinstance(name, bytes):
                if b'.' in name:
                    break
            elif '.' in name:
                break
        else:
            name = hostname
    return name

网站公告

今日签到

点亮在社区的每一天
去签到