I have just started playing. If it is quite complex, the essential coding is not very difficult, which makes very useful modules very interesting.
My question is about level 17. As was required in level 4, while collecting cookies, which I did, I can not compress the BZ2-decompress string that I get.
I tried Googling, and found a good blog with the solution in Python 2. Specifically, while analyzing one of the 17 for the level 17, I realized that I actually get the compressed string (from the cookies) and it gets dealt properly in Python 2:
bz2.decompress (urllib.unquote_plus (compressed))
However, in bz2.decompress
Python 3 requires a byte array instead of a string But the clear Pyrens 3 equivalent of the above line:
bz2.decompress (urllib.parse.unquote_plus (message) .encode ("utf8"))
OSError: Valid data stream fails
. I tried all the above and some forms, but to no avail.
Even my (non-working) solution is up to now:
#! / Usr / Bin / env python3 "" "Python Challenge # 17: http://www.pythonchallenge.com/pc/return/romance.html It is similar to # 4 and it actually uses its solution. The cookie of the page says, "You should + follow + + + busiasing ...", then we use the word "busing" from # 4 and read cookies Follow the series. "" "Import urllib.request, Import Urlib.parse import again bz2 nothing =" 12345 "last_cookie = no message =" "while correct: shi Title = dict () if last_cookie: headings ["cookie"] = last_cokai r = urllib.request.reequest ("http: / Urlib.request.urlopen (r) with U: last_cookie = u.getheader (" Set-Cookie ") With /www.pythonchallenge.com/pc/def/linkedlist.php?busynothing=" + nothing, headers = headers = m = re-match (r "info = (. *?);", Last) if I : Message + = Mg. Freeze (1) text = u.rad (). Decode ("UTF 8") print ("{}> gt>> {}}. Format (nothing, text)) m = re.search (r" \ d + $ ", text) Effort: Except print (e) print ("cookies message:", message) print ("decoded:", bez 2.decpress (none) = str (int (m.group (0)) except the exception. Urllib.parse.unquote_plus (message) .encode ("utf8")))
So, my question is: How does a Python 3 solution look for the above problem and why My job is not as expected?
I know very well that some parts of it can be done more thoroughly. I was going for a quick and dirty solution, so my interest is only that it works ( And why not I did it up).
You need to use here. It does not support space mapping to +
, but it has worked almost exclusively with str.replace ()
:
Urllib.parse.unquote_to_bytes (message.replace ('+', '% 20'))
This then decompresses well then you can decode the e uncompressed string as ASCII In:
print ("decoded:", bz2.decompress (urllib.parse.unquote_to_bytes (message.replace ('+', '% 20')).) Decoded (' ASCI '))
I do not remove the demo using a different message Am ready for:
& gt; & Gt; & Gt; Import bz2 & gt; & Gt; & Gt; Import urllib.parse & gt; & Gt; & Gt; Other _ Message = 'BJ-91Ai% 26Sy% 80% F4C% E8% 00% 00% 02% 13% 80% 40% 00% 04% 00% 22% E3% 8C% 00 +% 00% 22% 004% d% 40% D04% 0C% B7% 3B% E6h% B1AIM% 3D% 5E.% E4% 8 AP% A1% 21% 01% E8% 87% D0 '& gt; & Gt; & Gt; BZ2.decompress (urllib.parse.unquote_to_bytes (another_message.replace ('+', '% 20')). Decode ('ascii') 'This Message Not'
Someone Sometimes with visits your blog regularly and recommended it in my experience to read as well. The way of writing is excellent and also the content is top-notch. Thanks for that insight you provide the readers!
ReplyDeletelist to string python