`
JNstar
  • 浏览: 1519 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

PythonChallenge Level 0~4

阅读更多

PythonChallenge Level 0:    http://www.pythonchallenge.com/pc/def/0.html

    

 

    计算 2 的 38 次幂 将得到的结果替换URL中数字0

print 2**38

 

 

 

 

 

 

PythonChallenge Level 1:    http://www.pythonchallenge.com/pc/def/map.html


 

照笔记本中给出的规律解密图片下方给出的一串字符

g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj.
     用到两个函数

string.maketrans(fromto)

Return a translation table suitable for passing to translate(), that will map each character in from into the character at the same position in tofrom and to must have the same length.

##返回一个转换表 table( eg. 'A'-->'a', 'B'-->'b'), from 和 to 长度必须一致,在 from 中有的字符转换成 to 中对应位置的字符 

string.translate(stable[, deletechars])

Delete all characters from s that are in deletechars (if present), and then translate the characters using table, which must be a 256-character string giving the translation for each character value, indexed by its ordinal. If table is None, then only the character deletion step is performed.

##按照转换表 table,对字符串 s 进行转换

 

import string

#load the text 
text = '''g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp.
bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. 
sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj.
'''

#creat a translate table
table = string.maketrans(string.ascii_lowercase, string.ascii_lowercase[2:] + string.ascii_lowercase[:2])

#apply the translation table on string
print string.translate(text, table)

# Or you can "print text.translate(table)"

 if you don't know  "string.ascii_lowercase[2:]", "string.ascii_lowercase[:2]", "string.ascii_lowercase",  try to print it. 

 

执行结果:i hope you didnt translate it by hand. thats what computers are for.

doing it in by hand is inefficient and that's why this text is so long. 

using string.maketrans() is recommended. now apply on the url.(对Level 1 URL中 map 进行同样替换即可转至Level 2)

 

 

 

 

 

 

PythonChallenge Level 2:    http://www.pythonchallenge.com/pc/def/ocr.html

 

    

 

网页的左下角还有几条提示

General tips:

  • Use the hints. They are helpful, most of the times.
  • Investigate the data given to you.
  • Avoid looking for spoilers.

 We saw the hint “but MABYE they are in the page source” 

故执行: 右击网页 --> 查看网页源代码 

找到了这一关的谜题: find rare characters in the mess below:

 

用到一个 str.isalpha() 函数,判断是否字母

str.isalpha()

Return true if all characters in the string are alphabetic and there is at least one character, false otherwise.

For 8-bit strings, this method is locale-dependent.

 

import string

mess = ''' #copy the mess from Level.2's page source '''
ans = ''

for x in mess:
    if x.isaplha():
        ans += x

print ans

 

得到结果: equality

将其替换 Level 2 URL 中的 " ocr " 即转到level 3

 

 

 

 

 

PythonChallenge Level 3:    http://www.pythonchallenge.com/pc/def/equality.html


网页左下角内容

To see the solutions to the previous level, replace pc with pcc, i.e. go to: http://www.pythonchallenge.com/pcc/def/equality.html 

Join us on IRC: irc.freenode.net #pythonchallenge

网页左下角的内容与本题无关,但从句中'see the solutions to the previious level'中得到启发,同第二关一样查看了网页的源代码,其中果然给出了mess.

图片正下方提示

One small letter, surrounded by EXACTLY three big bodyguards on each of its sides.

一个小写字母两边都有三个大写字母,形如 XXXxXXX ,后经尝试并不能得到结果,搜索之,其他过关的人用来匹配的形式为 xXXXxXXXx ,虽然还没明白为何如此,但确实是到下一关的正确方向。

从给出的mess中找出xXXXxXXXx形式的字符,用到网页源代码获取模块Urllib2以及正则表达式re模块。

urllib2.urlopen(url[, data[, timeout[, cafile[, capath[, cadefault[, context]]]]])

Open the URL url, which can be either a string or a Request object.

data may be a string specifying additional data to send to the server, or None if no such data is needed. Currently HTTP requests are the only ones that use data; the HTTP request will be a POST instead of a GET when the data parameter is provided. data should be a buffer in the standard application/x-www-form-urlencoded format. The urllib.urlencode() function takes a mapping or sequence of 2-tuples and returns a string in this format. urllib2 module sends HTTP/1.1 requests with Connection:close header included.

The optional timeout parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used). This actually only works for HTTP, HTTPS and FTP connections.

If context is specified, it must be a ssl.SSLContext instance describing the various SSL options. See HTTPSConnection for more details.

The optional cafile and capath parameters specify a set of trusted CA certificates for HTTPS requests. cafile should point to a single file containing a bundle of CA certificates, whereas capath should point to a directory of hashed certificate files. More information can be found in ssl.SSLContext.load_verify_locations().

The cadefault parameter is ignored.

This function returns a file-like object with three additional methods:

  • geturl() — return the URL of the resource retrieved, commonly used to determine if a redirect was followed
  • info() — return the meta-information of the page, such as headers, in the form of an mimetools.Message instance (see Quick Reference to HTTP Headers)
  • getcode() — return the HTTP status code of the response.

Raises URLError on errors.

Note that None may be returned if no handler handles the request (though the default installed global OpenerDirector uses UnknownHandlerto ensure this never happens).

In addition, if proxy settings are detected (for example, when a *_proxy environment variable like http_proxy is set), ProxyHandler is default installed and makes sure the requests are handled through the proxy.

Changed in version 2.6: timeout was added.

Changed in version 2.7.9: cafilecapathcadefault, and context were added.

 

re.findall(patternstringflags=0)

Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result unless they touch the beginning of another match.

New in version 1.5.2.

Changed in version 2.4: Added the optional flags argument.

 

import urllib2
import re
import string

text = urllib2.urlopen('http://www.pythonchallenge.com/pc/def/equality.html').read()
matstr = re.findall(r'[a-z][A-Z]{3}[a-z][A-Z]{3}[a-z]', text)
ans = ''
i = 0
while i < len(matstr):
    ans += matstr[i][4]
    i += 1
print ans








 

基于找出的匹配字符需有意义的角度,最后的ans只提取了xXXXxXXXx形式中间的小写字母

代码运行结果为 linkedlist 按照前几题的经验,替换Level 3中equality回车得到提示“linkedlist.php”,按提示再次替换html为php即转至Level 4

 

 

 

 

 

PythonChallenge Level 4:    http://www.pythonchallenge.com/pc/def/linkedlist.php

给了一幅没怎么看得懂的图,查看是否有隐藏提示于网页源代码中,右击-->查看网页源代码  如下

<html>
<head>
  <title>follow the chain</title>
  <link rel="stylesheet" type="text/css" href="../style.css">
</head>
<body>
<!-- urllib may help. DON'T TRY ALL NOTHINGS, since it will never 
end. 400 times is more than enough. -->
<center>
<a href="linkedlist.php?nothing=12345"><img src="chainsaw.jpg" border="0"/></a>
<br><br><font color="gold"></center>
Solutions to previous levels: <a href="http://wiki.pythonchallenge.com/"/>Python Challenge wiki</a>.
<br><br>
IRC: irc.freenode.net #pythonchallenge
</body>
</html>

 主题“follow the chain”,注释“ urllib may help. DON'T TRY ALL NOTHINGS, since it will never end. 400 times is more than enough.”,另分析源代码可知网页中图片添加了超链接herf="linkedlist.php?nothing=12345",返回点击图片得到文本提示"and the next nothing is 44827"。

综上分析明白了Level 4 题面要做的就是递归更换 网页图片超链接URL中的nothing值

import urllib2
import string

urLstr = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='

def get_Nothings(uid):
    if uid.isdigit():
        text = urllib2.urlopen(urLstr+uid).read()
        # print text
        nextuid = ''
        for x in text:
            if x.isdigit():
                nextuid += x 
        # print urLstr + uid             
        print urllib2.urlopen(urLstr+uid).read(),'\n'
        return get_Nothings(nextuid)
    else:
        pass
get_Nothings("12345") 

 代码运行一小段时间出现非数字给出nothing值的情况,提示“Yes,Divide by two and keep going”

将在其前的nothing值 16044 除以 2 即 8022 作为代码中函数get_Nothings()的初始值继续运行。

最后得到Level 4的结果 peak.html 将其替换到URL中即转至Level 5

Level 5 :http://www.pythonchallenge.com/pc/def/peak.html

Level 5~10 解题过程会记录在我的下一篇博客中,谢谢!-------by 江南

 

                

 
 

 

 

 

 

  • 大小: 487.4 KB
  • 大小: 538.5 KB
  • 大小: 299.5 KB
  • 大小: 302.6 KB
  • 大小: 351.6 KB
  • 大小: 19.5 KB
  • 大小: 19.6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics