mod_python and storing binary form data

C

canistel

Hi, I have a little python webservice that I created, and in one of
the methods I need to store some binary data that was "posted"... I
want to do something like this, but it doesn't work.

username = form.get("username", "")
message = form.get("message", "")
attachment = form.get("attachment", None)
....
c.execute("""INSERT INTO Message (username, message, attachment)
VALUES (%s, %s, %s)""", (username, message, attachment))


"attachment" is then some binary data that was posted, but when I look
in the mysql database, I see values for the attachment field which
look like:

Field('attachment', '\x89PNG\r\n\x1a\n\x00\x00\...

so it is storing something, just not my binary data (in this case a
picture). the attachment column is a longblob in mysql.
 
G

Gabriel Genellina

Hi, I have a little python webservice that I created, and in one of
the methods I need to store some binary data that was "posted"... I
want to do something like this, but it doesn't work.

username = form.get("username", "")
message = form.get("message", "")
attachment = form.get("attachment", None)
...
c.execute("""INSERT INTO Message (username, message, attachment)
VALUES (%s, %s, %s)""", (username, message, attachment))


"attachment" is then some binary data that was posted, but when I look
in the mysql database, I see values for the attachment field which
look like:

Field('attachment', '\x89PNG\r\n\x1a\n\x00\x00\...

so it is storing something, just not my binary data (in this case a
picture). the attachment column is a longblob in mysql.

I assume you're using mod_python.util.FieldStorage.
For normal (string) values, form.get returns a somewhat bizarre
StringField that's a subclass of str itself. For files, it returns a Field
object; it has a "file" attribute that you can use.
See
http://www.modpython.org/live/current/doc-html/pyapi-util-fstor-fld.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top