Solutions for the Python Challenge

Show possible solutions for: All levels; level: previous, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, next

Possible solution for level 17

URI:
http://www.pythonchallenge.com/pc/return/romance.html
Log-in data:
Username: huge; password: file
Comment:
Possible Solution:
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)}
)

print urllib2.urlopen(req).read()

(Download this source code.)

Code word for next challenge:
balloons

By Holger Thölking, May 29th, 2007.