gpt4 book ai didi

python-3.x - 带有 python 3.4 的 mod_wsgi 得到预期的字节字符串值的错误序列,找到类型列表的值

转载 作者:行者123 更新时间:2023-12-01 01:49:40 25 4
gpt4 key购买 nike

我试图返回客户端 mysql 数据,我得到 mod_wsgi (pid=2304): 处理 WSGI 脚本时发生异常 TypeError: 预期的字节字符串值序列,找到类型列表的值\r

    def application(environ, start_response):

result = ChildClass().getValue()
status = '200 OK'
output = result

response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)

print(output)
return [output]

class ChildClass(): # define child class
print('ppp')
def __init__(self):
print("Calling child constructor")

def childMethod(self):
print('Calling child method')
#Parentclass().parentMethod()

def getValue(self):
# Open database connection
db = mysql.connector.connect(user='root', password='55118',host='127.0.0.1',database='test')
cursor = db.cursor()
query = ("SELECT * from employees2")
cursor.execute(query)
#for (first_name) in cursor:
return cursor.fetchall()

如何将 cursor.fetchall 转换为字节?

最佳答案

如果您正在关注 modwsgi readthedocs,它会提供一个小片段来检查 mod_wsgi 是否在您的服务器上工作。但是,我发现在安装了 Apache 和 mod_wsgi for Python 3 模块的情况下使用 Python 3.4 和 Django 1.9.2 时代码失败。我会不断收到“TypeError:预期的字节字符串值序列,找到 str 类型的值”。

答案是明确地将“b”放在我的字符串前面,使它们成为字节字符串而不是默认的 unicode。所以解决方法是:

output = b'Hello World!'

并且在返回和底部时确保您以列表形式返回,例如:

return [output]

这让我难过了好几个小时,直到我最终不得不阅读 PEP 3333 ( https://www.python.org/dev/peps/pep-3333/#a-note-on-string-types ) 和“关于字符串的注释”部分。

关于python-3.x - 带有 python 3.4 的 mod_wsgi 得到预期的字节字符串值的错误序列,找到类型列表的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31937508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com