Flask SQLAlchemy在WSGI服务器中引发Unicode错误,但在调试本地服务器中未引发Unicode错误?

时间:2019-08-20 02:45:30

标签: python flask unicode sqlalchemy mod-wsgi

我在flask-sqlalchemy中创建了一个测试表,该表将一堆文件路径存储在sqlite3数据库的Unicode列中。其中许多文件路径的字符都位于utf-8之外。​​

如果我使用flask run运行调试服务器,则一切正常,并且我的文件路径已正确添加到数据库中。

如果我正在通过实时apache2 mod-wsgi服务器运行烧瓶,则SqlAlchemy会抛出:UnicodeEncodeError: 'utf-8' codec can't encode characters in position 55-81: surrogates not allowed

此查询抛出此错误: models.Test.query.filter_by(file_path=path_file).first()

debug和wsgi服务器都运行Python 3.6.8

解决此问题的正确方法是什么?

谢谢!

编辑:追溯追溯:

[Mon Aug 19 22:16:56.440632 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554] [2019-08-19 22:16:56,436] ERROR in app: Exception on /library/test [POST]
[Mon Aug 19 22:16:56.440658 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554] Traceback (most recent call last):
[Mon Aug 19 22:16:56.440665 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]   File "/mnt/stor/www/sapi/venv/lib/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
[Mon Aug 19 22:16:56.440671 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]     response = self.full_dispatch_request()
[Mon Aug 19 22:16:56.440677 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]   File "/mnt/stor/www/sapi/venv/lib/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
[Mon Aug 19 22:16:56.440683 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]     rv = self.handle_user_exception(e)
[Mon Aug 19 22:16:56.440688 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]   File "/mnt/stor/www/sapi/venv/lib/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
[Mon Aug 19 22:16:56.440693 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]     reraise(exc_type, exc_value, tb)
[Mon Aug 19 22:16:56.440699 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]   File "/mnt/stor/www/sapi/venv/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
[Mon Aug 19 22:16:56.440704 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]     raise value
[Mon Aug 19 22:16:56.440709 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]   File "/mnt/stor/www/sapi/venv/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
[Mon Aug 19 22:16:56.440715 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]     rv = self.dispatch_request()
[Mon Aug 19 22:16:56.440720 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]   File "/mnt/stor/www/sapi/venv/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
[Mon Aug 19 22:16:56.440726 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]     return self.view_functions[rule.endpoint](**req.view_args)
[Mon Aug 19 22:16:56.440732 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]   File "/mnt/stor/www/sapi/sapi/auth.py", line 93, in decorated
[Mon Aug 19 22:16:56.440737 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]     return f(*args, **kwargs)
[Mon Aug 19 22:16:56.440742 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]   File "/mnt/stor/www/sapi/sapi/routes/library.py", line 16, in test_scan_library
[Mon Aug 19 22:16:56.440747 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]     library_test.scan_library()
[Mon Aug 19 22:16:56.440753 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]   File "/mnt/stor/www/sapi/sapi/library_test.py", line 31, in scan_library
[Mon Aug 19 22:16:56.440758 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]     check_track = models.Test.query.filter_by(file_path=path_file).first()
[Mon Aug 19 22:16:56.440764 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]   File "/mnt/stor/www/sapi/venv/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3228, in first
[Mon Aug 19 22:16:56.440781 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]     ret = list(self[0:1])
[Mon Aug 19 22:16:56.440786 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]   File "/mnt/stor/www/sapi/venv/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3018, in __getitem__
[Mon Aug 19 22:16:56.440792 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]     return list(res)
[Mon Aug 19 22:16:56.440796 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]   File "/mnt/stor/www/sapi/venv/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3330, in __iter__
[Mon Aug 19 22:16:56.440801 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]     return self._execute_and_instances(context)
[Mon Aug 19 22:16:56.440807 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]   File "/mnt/stor/www/sapi/venv/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3355, in _execute_and_instances
[Mon Aug 19 22:16:56.440812 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]     result = conn.execute(querycontext.statement, self._params)
[Mon Aug 19 22:16:56.440818 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]   File "/mnt/stor/www/sapi/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 988, in execute
[Mon Aug 19 22:16:56.440823 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]     return meth(self, multiparams, params)
[Mon Aug 19 22:16:56.440827 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]   File "/mnt/stor/www/sapi/venv/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 287, in _execute_on_connection
[Mon Aug 19 22:16:56.440832 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]     return connection._execute_clauseelement(self, multiparams, params)
[Mon Aug 19 22:16:56.440837 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]   File "/mnt/stor/www/sapi/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1107, in _execute_clauseelement
[Mon Aug 19 22:16:56.440842 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]     distilled_params,
[Mon Aug 19 22:16:56.440847 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]   File "/mnt/stor/www/sapi/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1248, in _execute_context
[Mon Aug 19 22:16:56.440852 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]     e, statement, parameters, cursor, context
[Mon Aug 19 22:16:56.440857 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]   File "/mnt/stor/www/sapi/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1468, in _handle_dbapi_exception
[Mon Aug 19 22:16:56.440863 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]     util.reraise(*exc_info)
[Mon Aug 19 22:16:56.440868 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]   File "/mnt/stor/www/sapi/venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 153, in reraise
[Mon Aug 19 22:16:56.440873 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]     raise value
[Mon Aug 19 22:16:56.440878 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]   File "/mnt/stor/www/sapi/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1244, in _execute_context
[Mon Aug 19 22:16:56.440883 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]     cursor, statement, parameters, context
[Mon Aug 19 22:16:56.440887 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]   File "/mnt/stor/www/sapi/venv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 552, in do_execute
[Mon Aug 19 22:16:56.440893 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]     cursor.execute(statement, parameters)
[Mon Aug 19 22:16:56.440900 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554] UnicodeEncodeError: 'utf-8' codec can't encode characters in position 55-81: surrogates not allowed
[Mon Aug 19 22:16:56.440939 2019] [wsgi:error] [pid 21975] [remote 192.168.1.1:54554]

0 个答案:

没有答案