0

The mysql output:

    b.query("select * from b where a='" + c + "' limit 1")
    result = b.store_result()
    d = result.fetch_row(0)

the bottom of WSGI script:

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

apache error:

 TypeError: sequence of byte string values expected, value of type tuple found

i would like to use "yield" if possible not "return".

because usually i use "yield" so if i want to see a mysql output in a raw way on the web..

what should i do ?

stderr
  • 8,567
  • 1
  • 34
  • 50
user3880134
  • 63
  • 1
  • 1
  • 5

1 Answers1

0

It looks like you're using a Python DB-API compatible driver. To get the best answer you should really include the library you're using and the database driver.

Either way, you've currently got an unsafe operation, ripe for some kind of unescaped SQL injection exploit.

First change your query to this:

b.query("select * from b where a=%s limit 1", c)
result = b.store_result()

WSGI wants just str() objects back, and you're returning instead tuples of values. You're returning text/html so you probably want to do something like this:

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>"]

If you'd like to use yield, just create and return a generator instead of a list.

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)

I would strongly recommend moving away from manually generating your own HTML and WSGI responses and look at using a simple framework like flask to abstract away a lot of the boilerplate. A template system like jinja can make this significantly better to read, write, maintain, and extend.

stderr
  • 8,567
  • 1
  • 34
  • 50