s=str(binary)

G

gert

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 ?
 
J

John Machin

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:

According to the fabulous manual:

str([object[, encoding[, errors]]])
Return a string version of an object, using one of the following
modes:
[snip]
When only object is given, this returns its nicely printable
representation. For strings, this is the string itself. The difference
with repr(object) is that str(object) does not always attempt to
return a string that is acceptable to eval(); its goal is to return a
printable string.

Hmm looks like (1) we need to do the dreaded eval() and (2) there's no
guarantee it will work.
.... blob = bytes()
.... if eval(str(blob)) != blob:
.... print(i, blob, str(blob), eval(str(blob)))
....
Looks like it's going to work, but you better be rather sure that you
trust the source.

HTH,
John
 
G

gert

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))
According to the fabulous manual:

str([object[, encoding[, errors]]])
Return a string version of an object, using one of the following
modes:
[snip]
When only object is given, this returns its nicely printable
representation. For strings, this is the string itself. The difference
with repr(object) is that str(object) does not always attempt to
return a string that is acceptable to eval(); its goal is to return a
printable string.

Hmm looks like (1) we need to do the dreaded eval() and (2) there's no
guarantee it will work.

...    blob = bytes()
...    if eval(str(blob)) != blob:
...       print(i, blob, str(blob), eval(str(blob)))
...

Looks like it's going to work, but you better be rather sure that you
trust the source.


Any other suggestions ?
 
J

John Machin

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
 
G

gert

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

gert

b = environ['CONTENT_TYPE'].split('boundary=')[1]
oops forgot, is indeed better solution :)
 

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

Forum statistics

Threads
474,299
Messages
2,571,545
Members
48,299
Latest member
Ruby87897

Latest Threads

Top