Python WSGI如何在浏览器上打印原始MYSQL输出

Python WSGI如何在浏览器上打印原始MYSQL输出,python,python-2.7,python-3.x,wsgi,python-db-api,Python,Python 2.7,Python 3.x,Wsgi,Python Db Api,mysql输出: b.query("select * from b where a='" + c + "' limit 1") result = b.store_result() d = result.fetch_row(0) WSGI脚本的底部: start_response('200 OK', [('content-type', 'text/html')]) return [d] apache错误: TypeError: sequence of byte str

mysql输出:

    b.query("select * from b where a='" + c + "' limit 1")
    result = b.store_result()
    d = result.fetch_row(0)
WSGI脚本的底部:

start_response('200 OK', [('content-type', 'text/html')])
return [d]
apache错误:

 TypeError: sequence of byte string values expected, value of type tuple found
我想使用收益率,如果可能的话不返回

因为我通常使用yield,所以如果我想在web上以原始方式查看mysql输出


我该怎么办?

看起来您正在使用与Python DB-API兼容的驱动程序。要获得最佳答案,您应该包括您正在使用的库和数据库驱动程序

无论哪种方式,您当前都有一个不安全的操作,适合于某种未经转移的SQL注入攻击

首先将查询更改为:

b.query("select * from b where a=%s limit 1", c)
result = b.store_result()
WSGI只希望返回str对象,而您返回的是值的元组。您正在返回text/html,因此可能需要执行以下操作:

def csvify(row):
    return ",".join([str(col) for col in row])
start_response('200 OK', [('content-type', 'text/html')])
return ["<html><body><pre>"] + [ csvify(row) for row in results ] + ["</pre></body></html>"]
如果您想使用yield,只需创建并返回一个生成器,而不是列表

我强烈建议您不要手动生成自己的HTML和WSGI响应,而是考虑使用一个简单的框架(如flask)来抽象出大量的样板文件。像jinja这样的模板系统可以显著提高读取、写入、维护和扩展的效率。

b和d是什么类型的对象?
def csvify(row):
    return ",".join([str(col) for col in row])
def my_response_generator(results):
    yield "<html><body><pre>"
    for row in results:
        yield csvify(row)
    "</pre></body></html>"
start_response('200 OK', [('content-type', 'text/html')])
return my_response_generator(result)