0%

Python Challenge (Level 17)

第17关

这一关颇为复杂,所以我不打算写太多探索的过程(其实也没怎么探索,基本是看网上答案的),简略解释一下过程。

先把可能用到的库给出来,后面的代码就不再重复了。

1
2
3
4
5
6
from urllib2 import Request, build_opener, HTTPCookieProcessor, HTTPHandler
import urllib
import cookielib
import re
import bz2
import xmlrpclib

大图是一些cookies,左下角的小图其实就是第4关的图。所以,我们要查看一下cookie

1
2
3
4
5
6
cj = cookielib.CookieJar()
opener = build_opener(HTTPCookieProcessor(cj), HTTPHandler)
f = opener.open("http://www.pythonchallenge.com/pc/def/linkedlist.php")
html = f.read()
for cookie in cj:
print cookie

得到以下信息

you+should+have+followed+busynothing

所以,是要像第4关那样,不断获取下一个页面,不过要将nothing改为busynothing,同时还要保存期间得到的cookie

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
pat = re.compile('and the next busynothing is (\d+)')
url_template = r'http://www.pythonchallenge.com/pc/def/linkedlist.php?busynothing={0}'
next_num = '12345'
cookies = []
while True:
f = opener.open(url_template.format(next_num))
html = f.read()
for cookie in cj:
cookies.append(cookie)
matchRes = pat.findall(html)
if matchRes:
next_num = matchRes[0]
print next_num
else:
break

得到的cookie里面,组合起来是’BZh91…’这样一串数据,用到bz2进行decompression,与第8关类似

1
2
3
values = [x.value for x in cookies]
msg = urllib.unquote_plus("".join(values))
print bz2.decompress(msg)

得到以下信息

is it the 26th already? call his father and inform him that “the flowers are on their way”. he’ll understand.

“26th”,”flowers”,这些信息都与Mozart那一关有关联。Mozart的父亲是Leopold Mozart. 打电话,又要用到第13关的phonebook。

1
2
proxy = xmlrpclib.ServerProxy("http://www.pythonchallenge.com/pc/phonebook.php")
print proxy.phone("Leopold")

得到

555-VIOLIN

这还没完,直接输入violin还不能过关,还要用到cookie

1
2
list(cj)[0].value = 'the+flowers+are+on+their+way'
print opener.open('http://www.pythonchallenge.com/pc/stuff/violin.php').read()

终于得到了通关信息——balloons

oh well, don’t you dare to forget the balloons.

http://www.pythonchallenge.com/pc/return/balloons.html