python challenge 17解题总结

 

python challenge 17网址:www.pythonchallenge.com/pc/return/romance.html

这道谜题比较复杂,需要结合前面几个题一起来解,需要python的re, urllib, urllib2, bz2, xmlrpclib  这些模块,首先利用 urllib, urllib2去请求URL,获取一些数据,接着利用bz2模块对数据进行处理,。。。

源代码如下:

import re, urllib, urllib2, bz2, xmlrpclib  
  
# First of all, get the data that is hidden within the "Set-Cookie" headers:  
  
uri = "http://www.pythonchallenge.com/pc/def/linkedlist.php?busynothing=%s"  
nn_rep = re.compile("the next busynothing is (d+)")  
cookie_val = re.compile("info=([^;]+);")  
result = []  
n = "12345"  
  
while True:  
    h = urllib.urlopen(uri % n)  
    next = h.read()  
    cookie = h.info().getheader("Set-Cookie")  
    h.close()  
  
    cval = cookie_val.search(cookie)  
  
    if cookie and cval:  
        result.append(urllib.unquote_plus(cval.group(1)))  
  
    try:  
        n = nn_rep.search(next).group(1)  
    except:  
        break  
  
print bz2.decompress("".join(result)  
  
# Get Leopold's phone numer using the code for level 13:  
  
conn = xmlrpclib.ServerProxy("http://www.pythonchallenge.com/pc/phonebook.php")  
  
print conn.phone("Leopold")  
  
# Now phone Mozart's father and tell him that "the flowers are on their way":  
  
uri = "http://www.pythonchallenge.com/pc/stuff/violin.php"  
msg = "the flowers are on their way"  
req = urllib2.Request(  
    uri, headers = { "Cookie": "info=" + urllib.quote_plus(msg)}  
)  

下一关网址为:www.pythonchallenge.com/pc/return/balloons.html