美文网首页程序员
Python Challenge[17]

Python Challenge[17]

作者: Recgat | 来源:发表于2017-02-13 00:28 被阅读0次

    [Level 17]


    Title: eat?

    图片就是提示了。饼干是cookies,cookie不仅仅是饼干。浏览器查看网页cookie,显示you+should+have+followed+busynothing...。busynothing,nothing,且左下角的图是第四关的图。从busynothing=12345开始,收集cookie中的info值。

    import httplib2,re
    h = httplib2.Http('.Cache')
    url, num = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?busynothing=', '12345'
    pattern = re.compile('info=(.*)')
    info = ''
    while num.isdigit():
      resp,content=h.request(url+num)
      info += pattern.search(resp['set-cookie'].split(';')[0]).groups()[0]
      num = content.decode('utf-8').split(' ')[-1]
    print(info)
    

    info最终是一段以BZh91AY开头的字符串,使用bz2解压,异常终止,显示OSError: Invalid data stream。搜了下,这段字符需要转换:

    import bz2,urllib.parse
    info = urllib.parse.unquote_to_bytes(info.replace('+','%20'))
    print(bz2.decompress(info).decode('utf-8'))#ascii
    

    得到这么一段信息:

    is it the 26th already? call his father and inform him that "the flowers are on their way". he'll understand.

    his father指的是十五关莫扎特的父亲Leopold Mozart,call?phone,十三关的phone:

    import xmlrpc.client
    xmlrpc = xmlrpc.client.ServerProxy('http://www.pythonchallenge.com/pc/phonebook.php')
    print(xmlrpc.phone('Leopold'))
    

    得到555-VIOLIN。然而VIOLIN.html不存在,violin.html显示no! i mean yes! but ../stuff/violin.php.violin.php显示一张照片,标题是it's me. what do you want?。前面提示inform him that "the flowers are on their way",怎样inform?卡了下,是要把网页cookie中的info值设置为the flowers are on their way

    url = 'http://www.pythonchallenge.com/pc/stuff/violin.php'
    headers = {'Cookie':'info=the flowers are on their way'}
    print(h.request(url,headers=headers)[1].decode('utf-8'))
    

    得到oh well, don't you dare to forget the balloons.,嗯,[Level 18]

    小结

    1. 使用httplib2库,cookie信息包含在httplib2.Http().request()返回的Response实例中。
    2. 直接遍历得到的最终info值需要转义,使用urllib.parse.unquote()urllib.parse.unquote_plus()方法返回的二进制对象均不能正常使用bz2解压。若要使用该方法,需要额外的参数:urllib.parse.unquote_plus(info,'latin1').encode('latin1')
    3. urllib.parse.unquote_to_bytes(string)方法转换返回的对象可以正常解压,该方法功能是将%xx转义替换为其等效的single-octet字符(unquote_plus()unquote()是单个字符),以字节对象返回。不像unquote_plus()能将+转为空格,unquote_to_bytes()需要手动替换。urllib库中文文档可参考Urllib库
    4. 使用Http.request()设置cookie,cookie包含在headers中。headers={'Cookie': 'cookie_value'}

    Python Challenge Wiki

    1. 使用urllib库获取cookie信息。

    import urllib.request
    h = urllib.request.urlopen(url)
    cookie = h.getheader('Set-Cookie')
    

    或直接获取info值:

    from urllib.request import build_opener,HTTPCookieProcessor,HTTPHandler
    import http.cookiejar
    cj = http.cookiejar.CookieJar()
    opener = build_opener(HTTPCookieProcessor(cj),HTTPHandler)
    f = opener.open(url)
    for cookie in cj:
      print(cookie.value)
    

    2. 设置cookie。

    from urllib.request import Request, urlopen
    from urllib.parse import quote_plus
    req = Request(url, headers = { "Cookie": "info=" + quote_plus(msg)})
    #print(urlopen(req).read().decode())
    

    或者这样:

    import urllib.request, urllib.parse
    o = urllib.request.build_opener()
    o.addheaders.append(('Cookie', 'info='+urllib.parse.quote_plus(msg)))
    res = o.open(url)
    #print(res.read().decode())
    

    More

    相关文章

      网友评论

        本文标题:Python Challenge[17]

        本文链接:https://www.haomeiwen.com/subject/wzboittx.html