Skip to content

Commit 8d573f3

Browse files
17 first step
1 parent b80a86b commit 8d573f3

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

17.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,55 @@
1+
import http.cookiejar, urllib.request, urllib.parse
2+
import bz2
3+
import http.cookies
4+
5+
16
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
353

454

555
if __name__ == "__main__":

18-2.py

Whitespace-only changes.

0 commit comments

Comments
 (0)