python challenge 18解题总结

 

python challenge 18的网址是:www.pythonchallenge.com/pc/return/balloons.html,打开网页后会出现如下的图片,并且网页源代码会给出提示:can you tell the difference? 仔细对比两个图片会发现这两个图片的亮度显然不同,于是打开www.pythonchallenge.com/pc/return/brightness.html,再仔细看网页源代码,结果发现另外一个提示:maybe consider deltas.gz,于是把deltas.gz给下载下来(www.pythonchallenge.com/pc/return/deltas.gz)。

 

分析下载下来的文件的源代码:

import gzip, difflib  
   
h = gzip.open("deltas.gz")  
d = difflib.Differ()  
  
part_1, part_2 = [], []  
file_1, file_2, file_3 = [], [], []  
  
for line in h:  
    part_1.append(line[0:53])  
    part_2.append(line[56:-1])  
  
h.close()  
  
for line in list(d.compare(part_1, part_2)):  
    if line[0] == "+":  
        file_1.append(line[2:])  
    elif line[0] == "-":  
        file_2.append(line[2:])  
    else:  
        file_3.append(line[2:])  
  
for n, data in enumerate((file_1, file_2, file_3)):  
    temp = []  
  
    for line in data:  
        temp.extend([chr(int(o, 16)) for o in line.strip().split(" ") if o])  
  
    h = open("%s.png" % (n + 1), "wb")  
    h.writelines(temp)  
    h.close()  
print urllib2.urlopen(req).read()  

下一关的网址:www.pythonchallenge.com/pc/hex/bin.html(用户名:butter 密码:fly)