s=str(binary)

John Machin sjmachin at lexicon.net
Tue Jan 20 21:41:28 CET 2009


On Jan 21, 5:31 am, gert <gert.cuyk... at gmail.com> wrote:
> On Jan 20, 5:23 am, John Machin <sjmac... at lexicon.net> wrote:
>
> > On Jan 20, 12:54 pm, gert <gert.cuyk... at 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))
>
[snip]
>
> > Looks like it's [it being eval(str(blob))] going to work, but you better be rather sure that you
> > trust the source.
>
> Any other suggestions ?

Yeah.

(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?
* Are you sure the pattern to retrieve the picture is correct?
* What is the "Content-Transfer-Encoding"?

(d) Surely there must be a library somewhere that parses that kind of
data for you ...

(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

HTH,
John



More information about the Python-list mailing list