On Jan 20, 9:41 pm, John Machin <sjmac...@lexicon.net> wrote: > On Jan 21, 5:31 am, gert <gert.cuyk...@gmail.com> wrote: > > > On Jan 20, 5:23 am, John Machin <sjmac...@lexicon.net> wrote: > > > > On Jan 20, 12:54 pm, gert <gert.cuyk...@gmail.com> wrote: > > > > > How do you convert s back to binary data in python 3 so I can put in a > > > > sqlite blob ? > > > > Is there a build in function or do I need to use binascii ? > > > > byte(s) or bin(s) would make more sense but can not figure it out ? > > > > Can't imagine why you would do str(binary_data) especially if you want > > > it back again ... however: > > > def application(environ, response): > > s = str(environ['wsgi.input'].read()) > > b = compile('boundary=(.*)').search(environ['CONTENT_TYPE']).group > > (1) > > p = compile(r'.*Content-Type: application/octet-stream\\r\\n\\r\\n > > (.*)\\r\\n--'+b+'.*'+b+'--', DOTALL).match(s).group(1) > > db.execute('UPDATE users SET picture=? WHERE uid=?', > > (p,session.UID)) > > > (a) don't write obfuscatory code :-0 > > E.g. > (1) re.compile(pattern).search(data) -> re.search(pattern, data) > (2) re.compile(pattern).match(data) -> re.match(pattern, data) > (3) re.match('.*blahblah', data) -> re.search('blahblah', data) > > (b) don't use re when ordinary str or bytes methods will do > > E.g. instead of: > b = re.search('boundary=(.*)'), environ['CONTENT_TYPE']).group(1) > try > b = environ['CONTENT_TYPE'].split('boundary=')[1] > > (c) Is that code meant to be rough pseudocode to illustrate what you > are trying to do, or is it meant to be working code? If the latter: > * Do you have this working in 2.X?
yep > * Are you sure the pattern to retrieve the picture is correct? yep http://91.121.53.159/file.txt > * What is the "Content-Transfer-Encoding"? print (environ['Content-Transfer-Encoding'],file=sys.stderr) Key error flash 10 does not send it ? > (d) Surely there must be a library somewhere that parses that kind of > data for you ... p = FieldStorage(fp=environ['wsgi.input'], environ=environ) In python 3 you get TypeError: Can't convert 'bytes' object to str implicitly > (e) if all else fails, I'd suggest: > > instead of s = str(binary) > do s = binary.decode('latin1') > # this won't change the number of characters and will allow > # reconstitution of your non-ascii bytes > Then do your DIY parsing > then at the end do > blob = p.encode('latin1') > # blob will be type bytes which is presumably what the database > expects Victory :) http://91.121.53.159/appwsgi/www/register/register.htm http://code.google.com/p/appwsgi/source/browse/trunk from db import Db from session import Session from re import search,match,DOTALL def application(environ, response): db = Db() cookie = "SID="+environ['QUERY_STRING'] session = Session(db,cookie,'guest') response('200 OK', [('Content-type', 'text/xml'), ('Set-Cookie', session.COOKIE)]) if not session.GID : return [] s = environ['wsgi.input'].read().decode('latin1') b = search(r'boundary=(.*)',environ['CONTENT_TYPE']).group(1) p = search(r'Content-Type: application/octet-stream\r\n\r\n(.*)\r \n--',s,DOTALL).group(1) db.execute('UPDATE users SET picture=? WHERE uid=?',(p.encode ('latin1'),session.UID)) xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" xml+= "<root>"+str(db.ERROR)+"</root>" response('200 OK', [('Content-type', 'text/xml')]) return [xml] -- http://mail.python.org/mailman/listinfo/python-list