tags:

views:

340

answers:

4
ACTIVATE_THIS = """
eJx1UsGOnDAMvecrIlYriDRlKvU20h5aaY+teuilGo1QALO4CwlKAjP8fe1QGGalRoLEefbzs+Mk
Sb7NcvRo3iTcoGqwgyy06As+HWSNVciKaBTFywYoJWc7yit2ndBVwEkHkIzKCV0YdQdmkvShs6YH
E3IhfjFaaSNLoHxQy2sLJrL0ow98JQmEG/rAYn7OobVGogngBgf0P0hjgwgt7HOUaI5DdBVJkggR
3HwSktaqWcCtgiHIH7qHV+esW2CnkRJ+9R5cQGsikkWEV/J7leVGs9TV4TvcO5QOOrTHYI+xeCjY
JR/m9GPDHv2oSZunUokS2A/WBelnvx6tF6LUJO2FjjlH5zU6Q+Kz/9m69LxvSZVSwiOlGnT1rt/A
77j+WDQZ8x9k2mFJetOle88+lc8sJJ/AeerI+fTlQigTfVqJUiXoKaaC3AqmI+KOnivjMLbvBVFU
1JDruuadNGcPmkgiBTnQXUGUDd6IK9JEQ9yPdM96xZP8bieeMRqTuqbxIbbey2DjVUNzRs1rosFS
TsLAdS/0fBGNdTGKhuqD7mUmsFlgGjN2eSj1tM3GnjfXwwCmzjhMbR4rLZXXk+Z/6Hp7Pn2+kJ49
jfgLHgI4Jg==
""".decode("base64").decode("zlib")

my code:

import zlib
print 'dsss'.decode('base64').decode('zlib')#error

Traceback (most recent call last):
  File "D:\zjm_code\b.py", line 4, in <module>
    print 'dsss'.decode('base64').decode('zlib')
  File "D:\Python25\lib\encodings\zlib_codec.py", line 43, in zlib_decode
    output = zlib.decompress(input)
zlib.error: Error -3 while decompressing data: unknown compression method

a='dsss'.encode('zlib')
print a
a.encode('base64')
print a
a.decode('base64')#error
print a
a.decode('zlib')
print a

x\x9cK)..Traceback (most recent call last):
  File "D:\zjm_code\b.py", line 7, in <module>
    a.decode('base64')
  File "D:\Python25\lib\encodings\base64_codec.py", line 42, in base64_decode
    output = base64.decodestring(input)
  File "D:\Python25\lib\base64.py", line 321, in decodestring
    return binascii.a2b_base64(s)
binascii.Error: Incorrect padding

a='dsss'
a=a.encode('zlib')
print a
a=a.decode('zlib')
print a#why can't print 'dsss'

x\x9cK)..

a='dsss'
a=a.encode('zlib')
#print a
a=a.decode('zlib')
print a#its ok

i think the 'print a' encode the a with 'uhf-8'. so:

#encoding:utf-8
a='dsss'
a=a.encode('zlib')
print a
a=a.decode('utf-8')#but error.
a=a.decode('zlib')
print a#


x\x9cK)..Traceback (most recent call last):
  File "D:\zjm_code\b.py", line 5, in <module>
    a=a.decode('utf-8')
  File "D:\Python25\lib\encodings\utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x9c in position 1: unexpected code byte
+4  A: 

The data in the strings is encoded and compressed binary data. The .decode("base64").decode("zlib") unencodes and decompresses it.

The error you got was because 'dsss' decoded from base64 is not valid zlib compressed data.

MiffTheFox
import zlib; print 'dsss'.encode("zlib").encode("base64") # 'eJxLKS4uBgAERgG+\n'
Joel
@Joel: and the point that you are trying to make is ...?
John Machin
@Joel, you're passing 'dsss' into the encoder, not the decoder.
MiffTheFox
I think that's the point - Joel's showing what you'd have to pass to `.decode("base64").decode("zlib")` in order to get 'dsss' back.
Cristián Romo
@Cristian: and that's irrelevant; Joel could have written `print 1+1 # 2` with equal effect.
John Machin
+1  A: 

.decode('base64') can be called only on a string that's encoded as "base-64, in order to retrieve the byte sequence that was there encoded. Presumably that byte sequence, in the example you bring, was zlib-compressed, and so the .decode('zlib') part decompresses it.

Now, for your case:

>>> 'dsss'.decode('base64')
'v\xcb,'

But 'v\xcv,' is not a zlib-compressed string! And so of course you cannot ask zlib to "decompress" it. Fortunately zlib recognizes the fact (that 'v\xcv,' could not possibly have been produced by applying any of the compression algorithms zlib knows about to any input whatsoever) and so gives you a helpful error message (instead of a random-ish string of bytes, which you might well have gotten if you had randomly supplied a different but equally crazy input string!-)

Edit: the error in

a.encode('base64')
print a
a.decode('base64')#error

is obviously due to the fact that strings are immutable: just calling a.encode (or any other method) does not alter a, it produces a new string object (and here you're just printing it).

In the next snippet, the error is only in the OP's mind:

>>> a='dsss'
>>> a=a.encode('zlib')
>>> print a
x?K)..F?
>>> a=a.decode('zlib')
>>> print a#why can't print 'dsss'
dsss
>>> 

that "why can't print" question is truly peculiar, applied to code that does print 'dsss'. Finally,

i think the 'print a' encode the a with 'uhf-8'.

You think wrongly: there's no such thing as "uhf-8" (you mean "utf-8" maybe?), and anyway print a does not alter a, any more than just calling a.encode does.

Alex Martelli
hi alex,i changed my question,help me to have a look.
zjm1126
+1  A: 

It is the reverse of:

original_message.encode('zlib').encode('base64')

zlib is a binary compression algorithm. base64 is a text encoding of binary data, which is useful to send binary message through text protocols like SMTP.

After 'dsss' was decoded from base64 (the three bytes 76h, CBh, 2Ch), the result was not valid zlib compressed data so it couldn't be decoded.

Try printing ACTIVATE_THIS to see the result of the decoding. It turns out to be some Python code.

Mark Tolonen
+2  A: 

What is the purpose of x.decode(”base64”).decode(”zlib”) for x in ("sss", "dsss", random_garbage)? Excuse me, you should know; you are the one who is doing it!

Edit after OP's addition of various puzzles

Puzzle 1

a='dsss'.encode('zlib')
print a
a.encode('base64')
print a
a.decode('base64')#error
print a
a.decode('zlib')
print a

Resolution: all 3 statements of the form

a.XXcode('encoding')

should be

a = a.XXcode('encoding')

Puzzle 2

a='dsss'
a=a.encode('zlib')
print a
a=a.decode('zlib')
print a#why can't print 'dsss'

x\x9cK)..

But it does print 'dsss':

>>> a='dsss'
>>> a=a.encode('zlib')
>>> print a
x£K)..♠ ♦F☺¥
>>> a=a.decode('zlib')
>>> print a#why can't print 'dsss'
dsss
>>>

Puzzle 3

"""i think the 'print a' encode the a with 'uhf-8'."""

Resolution: You think extremely incorrectly. What follows the print is an expression. There are no such side effects. What do you imagine happens when you do this:

print 'start text ' + a + 'end text'

?

What do you imagine happens if you do print a twice? Encoding the already-encoded text again? Why don't you stop imagining and try it out?

In any case, note that the output of str.encode('zlib') is an str object, not a unicode object:

>>> print repr('dsss'.encode('zlib'))
'x\x9cK)..\x06\x00\x04F\x01\xbe'

Getting from that to UTF-8 is going to be somewhat difficult ... it would have to be decoded into unicode first -- with what codec? ascii and utf8 are going to have trouble with the '\x9c' and the '\xbe' ...

John Machin
Language barrier. He's not a native English speaker, which makes literal interpretation of his questions difficult. :/
Travis Bradshaw
@Travis: Coincidence, not cause, IMHO. There are no vestiges of a sensible question (like "I ran that code and it produced some Python source; why would the author do that?") remaining after your supposed mistranslation from language X to English.
John Machin
thanks,John Machin and Travis Bradshaw ,you are good man.and i think its my ide's mistake,my ide is 'ulipad',and the output of above is true, i have sended a email to the auther of ide.many times i print some string bring about error.
zjm1126
@John Machin and @Travis Bradshaw ,and can you tell me some way to search for add friends with people(girl) who is native english speaker,i have yahoo messenger,msn,twitter,skype, but Until now I have not a chat friend,how can i get it.thanks
zjm1126
@zjm1126: Your last comment (the one including "search for add friends") is extremely off-topic for this forum. I suggest that you delete it.
John Machin