最近在自学python后端(flask框架),用“gevent_pywsgi”运行时出现了一点问题:
报错内容说:
“utf-8”编解码器无法解码字节0xbf:无效的起始字节
原报错内容是这样的:
Traceback (most recent call last):
File "C:\Users\段禹珩\AppData\Roaming\Python\Python37\site-packages\gevent\threadpool.py", line 175, in __run_task
thread_result.set(func(*args, **kwargs))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbf in position 0: invalid start byte
2023-04-08T04:49:57Z (0x1f08db18888 thread_ident=0x242c threadpool-hub=0x1f08d7c0f48 thread_ident=0x1ac>>, ) failed with UnicodeDecodeError
Traceback (most recent call last):
File "C:/Users/段禹珩/Desktop/610网站/main.py", line 103, in
server.serve_forever()
File "C:\Users\段禹珩\AppData\Roaming\Python\Python37\site-packages\gevent\baseserver.py", line 398, in serve_forever
self.start()
File "C:\Users\段禹珩\AppData\Roaming\Python\Python37\site-packages\gevent\baseserver.py", line 336, in start
self.init_socket()
File "C:\Users\段禹珩\AppData\Roaming\Python\Python37\site-packages\gevent\pywsgi.py", line 1546, in init_socket
self.update_environ()
File "C:\Users\段禹珩\AppData\Roaming\Python\Python37\site-packages\gevent\pywsgi.py", line 1558, in update_environ
name = socket.getfqdn(address[0])
File "C:\Users\段禹珩\AppData\Roaming\Python\Python37\site-packages\gevent\_socketcommon.py", line 304, in getfqdn
hostname, aliases, _ = gethostbyaddr(name)
File "C:\Users\段禹珩\AppData\Roaming\Python\Python37\site-packages\gevent\_socketcommon.py", line 276, in gethostbyaddr
return get_hub().resolver.gethostbyaddr(ip_address)
File "C:\Users\段禹珩\AppData\Roaming\Python\Python37\site-packages\gevent\resolver\thread.py", line 66, in gethostbyaddr
return self.pool.apply(_socket.gethostbyaddr, args, kwargs)
File "C:\Users\段禹珩\AppData\Roaming\Python\Python37\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\段禹珩\AppData\Roaming\Python\Python37\site-packages\gevent\_compat.py", line 66, in reraise
raise value.with_traceback(tb)
File "C:\Users\段禹珩\AppData\Roaming\Python\Python37\site-packages\gevent\threadpool.py", line 175, in __run_task
thread_result.set(func(*args, **kwargs))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbf in position 0: invalid start byte
项目原代码:
# -*- coding : utf-8 -*-
# coding: utf-8
from flask import *
from http.server import *
from gevent import pywsgi as internet
app = Flask(__name__)
'''测试'''
@app.route('/internet')
def cs ():
return render_template('index.html')
'''运行程序'''
if __name__ == "__main__":
# app.config["JSON_AS_ASCII"] = False
# app.run(host="0.0.0.0", port=610, debug=True, threaded=True, processes=1)
server = internet.WSGIServer(('0.0.0.0', 5000), app)
server.serve_forever()
可是我乍一看,那根本不是项目文件夹中的文件啊!那是库文件夹中的文件!
我在网上找了很久,都说编码问题只用在另存为中更改编码格式就够了。我也把所有报错了的文件另存为了一遍,报错内容还是一样的。
这个问题怎么解决啊,是要用python更高版本还是重新安装“gevent”啊?
这个报错是由于在使用gevent的getfqdn函数时,获取IP地址对应的主机名时出现了解码错误,原因可能是IP地址对应的主机名中包含了无法解码的非ASCII字符。解决方法是在调用getfqdn函数之前手动将IP地址转换成主机名,可以使用socket库中的gethostbyaddr函数实现。代码示例:
import socket
from gevent import pywsgi as internet
ip = '127.0.0.1'
hostname = socket.gethostbyaddr(ip)[0]
server = internet.WSGIServer((ip, 5000), app)
server.serve_forever()
其中,ip是服务器的IP地址,app是Flask应用对象。将IP地址转换成主机名后,就可以在gevent的WSGIServer中使用了。