|
| 1 | +import http.cookiejar, urllib.request, urllib.parse |
| 2 | +import bz2 |
| 3 | +import http.cookies |
| 4 | + |
| 5 | + |
1 | 6 | def main():
|
2 |
| - pass |
| 7 | + cookie_jar = http.cookiejar.CookieJar() |
| 8 | + opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie_jar)) |
| 9 | + ary = [] |
| 10 | + next_number = open_url(cookie_jar, opener, "12345", ary) |
| 11 | + while True: |
| 12 | + if int(next_number) > 0: |
| 13 | + next_number = open_url(cookie_jar, opener, next_number, ary) |
| 14 | + else: |
| 15 | + break |
| 16 | + encoded_string = "".join(ary) |
| 17 | + print("ary length is {}".format(len(ary))) |
| 18 | + print("bs is {}", encoded_string) |
| 19 | + password = bz2.decompress(str.encode(encoded_string, "latin1")) |
| 20 | + print("pw is {}".format(password)) |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +def open_url(cookie_jar, opener, number, ary): |
| 25 | + url_base = "http://www.pythonchallenge.com/pc/def/linkedlist.php?busynothing=" |
| 26 | + next_number = request_url(cookie_jar, opener, url_base + number, ary) |
| 27 | + return next_number |
| 28 | + |
| 29 | + |
| 30 | +def request_url(cookie_jar, opener, url, ary): |
| 31 | + response = opener.open(url) |
| 32 | + for item in cookie_jar: |
| 33 | + print("{} {}".format(item.name, item.value)) |
| 34 | + value = item.value |
| 35 | + b = urllib.parse.unquote_plus(value, "latin1") |
| 36 | + ary.append(b) |
| 37 | + print("ary is {}".format(ary)) |
| 38 | + data = response.read() |
| 39 | + ary = data.decode() |
| 40 | + next_number = find_next_number(ary) |
| 41 | + return next_number |
| 42 | + |
| 43 | + |
| 44 | +def find_next_number(doc): |
| 45 | + flag = "and the next busynothing is " |
| 46 | + length = len(flag) |
| 47 | + if flag in doc: |
| 48 | + index = doc.index(flag) |
| 49 | + else: |
| 50 | + return "-1" |
| 51 | + number = doc[index + length:] |
| 52 | + return number |
3 | 53 |
|
4 | 54 |
|
5 | 55 | if __name__ == "__main__":
|
|
0 commit comments