一个学习python好玩的网站
Thursday, January 28, 2010
http://www.pythonchallenge.com/
通过网页提示也好,回答问题,找到下一个链接的入口。
Level 0
http://www.pythonchallenge.com/pc/def/0.html
>>> 2**38
274877906944L
Level 1
http://www.pythonchallenge.com/pc/def/map.html
先解决hint,之前的想法是list后再用映射对每个字符ord后再加2,然后chr一下,最后jion出来,不过显然不对题意,因为z->b,y->a,并且字符不需要替换。
hint推荐的方法是string.maketrans()
>>> from string import maketrans
>>> str="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."
>>> print str.translate(maketrans("yzabcdefghijklmnopqrstuvwx","abcdefghijklmnopqrstuvwxyz"))
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.
>>> str="map"
>>> print str.translate(maketrans("yzabcdefghijklmnopqrstuvwx","abcdefghijklmnopqrstuvwxyz"))
ocr
Level 2
http://www.pythonchallenge.com/pc/def/ocr.html
>>> import re
>>> str = "网页源码中的一票乱字符,记得先把换行符替换掉"
>>> re.sub(r'\W|_','',str)
'equality'
Level 3
http://www.pythonchallenge.com/pc/def/equality.html
>>> import re
>>> str = "网页源码中的一票乱字符,记得先把换行符替换掉"
>>> print "".join(["%s" % b for b in re.findall('^|[a-z]{1,1}[A-Z]{3,3}([a-z])[A-Z]{3,3}[a-z]{1,1}|$',str)])
linkedlist
Level 4
http://www.pythonchallenge.com/pc/def/linkedlist.php
一开始没有明白什么意思,就写了一个搓的
import re
import urllib2
running = True
url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=46059'
while running:
response = urllib2.urlopen(url)
result = response.read()
mathes= re.findall("\d",result)
if len(mathes) == 0:
print result
running = False
else :
url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='+"".join([elem for elem in re.findall("\d",result)])
print result
print url
监视了屏幕的输出,才发现里面还是有玄机的。
import urllib2
nothing = "12345"
while nothing:
print nothing
html = urllib2.urlopen('http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=' + nothing).read()
if "Divide" in html:
nothing = str(int(nothing) / 2)
else:
nothing = "".join([item for item in html.split() if item.isdigit()])
print html
peak.html
Level 5
http://www.pythonchallenge.com/pc/def/peak.html
import pickle
import urllib2
url = "http://www.pythonchallenge.com/pc/def/banner.p"
response = urllib2.urlopen(url).read()
banner = pickle.loads(response)
print '\n'.join(["".join([i[0]*i[1] for i in b]) for b in banner])
channel
Level 6
http://www.pythonchallenge.com/pc/def/channel.html
import zipfile,re
file = zipfile.ZipFile("channel.zip","r")
idx = "90052"
history = []
while True:
history.append(idx)
data = file.read(idx+".txt")
#print "File:"+idx+".txt\t"+data
idx = "".join(re.findall('\d',data))
if len(idx)==0:
break
print "".join(file.getinfo(idx+".txt").comment for idx in history)
Level 7
http://www.pythonchallenge.com/pc/def/oxygen.html
import Image,re
im = Image.open("oxygen.png")
print "Image Info:", im.format, im.size, im.mode
row = [im.getpixel((x, (im.size[1]-1)/2)) for x in range(0, im.size[0], 7)]
ords = [r for r, g, b, a in row if r == g == b]
print "".join(map(chr,ords))
L = re.findall('\d+',"".join(map(chr,ords)))
print "".join(map(chr,map(int,L)))
Image Info: PNG (629, 95) RGBA
smart guy, you made it. the next level is [105, 110, 116, 101, 103, 114, 105, 116, 121]
integrity
提示:
1、使用Image模块
2、灰色部分隐藏了提示
3、灰色部分在图片的当中位置(im.size[1]-1)/2
4、RGB灰色为R,G,B三个值都相等r for r, g, b, a in row if r == g == b
5、仔细看图片,以及得出的list,发现只有第1个灰色块占据了5个像素,后面基本为7个像素,所以步长为7可以得到所有的灰度R,G,B值range(0, im.size[0], 7)
6、map是个好函数
Level 8
http://www.pythonchallenge.com/pc/def/integrity.html
import urllib2,re
url = "http://www.pythonchallenge.com/pc/def/integrity.html"
data = urllib2.urlopen(url).read()
unraw = "".join(re.findall(r'un: \'(.*)[\'$]',data))
pwraw = "".join(re.findall(r'pw: \'(.*)[\'$]',data))
print "user:",unraw.decode("string_escape").decode("bz2")
print "pass:",pwraw.decode("string_escape").decode("bz2")
Level 9
http://www.pythonchallenge.com/pc/return/good.html
import urllib2,Image,ImageDraw,re
url = "http://www.pythonchallenge.com/pc/return/good.html"
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, url, "huge", "file")
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
page = opener.open(url).read().replace("\n","")
first = re.findall('\d+',re.findall(r'first:(.*)second',page)[0])
second = re.findall('\d+',re.findall(r'second:(.*)-->',page)[0])
img = Image.new('RGB', (500,500))
draw = ImageDraw.Draw(img)
draw.line(map(int,first))
draw.line(map(int,second))
img.save("9.jpg")
img.show()
图片显示的是公牛(bull)哦!
Level 10
http://www.pythonchallenge.com/pc/return/bull.html
求a = [1, 11, 21, 1211, 111221, 这个序列的第30个字串的长度
首先看这个序列的规律
方法1:
strings = ['1','11']
for i in range(1,31):
j = 0
string = ''
while j < len(strings[i]):
count = 1
while j<(len(strings[i])-1) and (strings[i][j]==strings[i][j+1]):
j = j + 1
count = count + 1
string = '%s%d%c' % (string,count,strings[i][j])
j = j + 1
strings.append(string)
print len(strings[30])
5808
方法2:
import re
def describe(s):
return "".join([str(len(m.group(0))) + m.group(1) for m in re.finditer(r"(\d)\1*", s)])
s = "1"
for dummy in range(30):
s = describe(s)
print len(s)
5808
注意:m.group(0)记忆的是正则表达式的本身,m.group(1)记忆的是圆括号中匹配到内容
Level 11
http://www.pythonchallenge.com/pc/return/5808.html
import Image
im = Image.open("cave.jpg")
print "Image Info", im.format, im.size, im.mode
w,h=im.size[0],im.size[1]
new = Image.new(im.mode,(w//2, h//2))
for x in range(1,w,2):
for y in range(1,h,2):
new.putpixel((x//2, y//2), im.getpixel((x, y)))
new.save("odd.png", "PNG")
evil
Level 12
http://www.pythonchallenge.com/pc/return/evil.html
import urllib2
url = "http://www.pythonchallenge.com/pc/return/evil2.gfx"
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, url, "huge", "file")
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
page = opener.open(url).read()
for i in range(5):
file = open("evil_0%d.jpg" % i, "wb")
file.write(page[i::5])
file.close()
运行代码,查看5个图片,按顺序可以得到"dis","pro","port","ional","ity",但最后一个单词被划去了,因此我们得到单词 disproportional
注意:使用了::操作符,下面是用法
>>> s = 'abcdefgh1234dazadfa'
>>> s[0::5]
'af3a'
>>> s[0:len(s):5]
'af3a'
Level 13
http://www.pythonchallenge.com/pc/return/disproportional.html
import xmlrpclib,urllib2
url = "http://www.pythonchallenge.com/pc/return/evil4.jpg"
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, url, "huge", "file")
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
page = opener.open(url).read()
xmlrpc = "http://www.pythonchallenge.com/pc/phonebook.php"
service = xmlrpclib.ServerProxy(xmlrpc)
print service.phone(page[:4])
555-ITALY
Level 14
http://www.pythonchallenge.com/pc/return/italy.html
Nagios 被动模式
Wednesday, January 27, 2010
背景:公司有台内网的机器需要通过外网的Nagios来监控,并发送报警信息。
前提:内网部署的Nagios主机可以连外网。
方案:在内网部署Nagios服务器,并通过NSCA Client端发送消息到外网Nagios主机。
步骤:
1、在外网的Nagios服务器上安装NSCA服务端,并开启NSCA服务端,监听端口为5667
2、配置nagios.cfg文件修改
accept_passive_service_checks=1
accept_passive_host_checks=1
3、配置commond.cfg添加
# 'check_dummy' command definition
define command{
command_name check_dummy
command_line $USER1$/check_dummy $ARG1$
}
4、定义被动模式的服务器模板
define host {
name passive_host
check_period 24x7
check_command check_dummy!2
contact_groups nagiosadmin
notification_period 24x7
check_interval 5
retry_interval 1
max_check_attempts 10
active_checks_enabled 0
passive_checks_enabled 1
obsess_over_host 1
event_handler_enabled 1
low_flap_threshold 0.000000
high_flap_threshold 0.000000
flap_detection_enabled 1
flap_detection_options o,d,u
freshness_threshold 0
check_freshness 0
notification_options d,u,r
notifications_enabled 1
notification_interval 30
stalking_options n
process_perf_data 1
failure_prediction_enabled 1
retain_status_information 1
retain_nonstatus_information 1
check_freshness 1
freshness_threshold 600
register 0
}
5、定义被动模式的服务模板
define service {
name passive_service
check_period 24x7
check_command check_dummy!2
contact_groups nagiosadmin
notification_period 24x7
check_interval 10
retry_interval 2
max_check_attempts 3
parallelize_check 1
active_checks_enabled 0
passive_checks_enabled 1
obsess_over_service 1
event_handler_enabled 1
low_flap_threshold 0.000000
high_flap_threshold 0.000000
flap_detection_enabled 1
flap_detection_options o,w,u,c
freshness_threshold 0
check_freshness 0
notification_options u,w,c,r
notifications_enabled 1
notification_interval 10
stalking_options n
process_perf_data 1
failure_prediction_enabled 1
retain_status_information 1
retain_nonstatus_information 1
check_freshness 1
freshness_threshold 600
register 0
}
注:
# 服务对象定义里的check_freshness选项设为1,这将打开针对该服务的"刷新检测"特性;
# 服务对象定义里的freshness_threshold选项须设定为一个以秒为单位的数值,该值反应出由分布式服务器所提供的检测数据将应该以什么样频度来提供出来,一般是分布式服务器normal——check_interval的2倍;
6、配置内网的nagios服务器只是发消息到外网nagios服务器上,所以并不需要apache而且只需要编译一下nsca的安装包,并把src目录下的send_nsca复制到nagios plugins的目录,sample-config目录下的send_nsca.cfg复制到nagios的etc目录下
7、配置内网nagios的nagios.cfg修改
obsess_over_services=1
ocsp_command=submit_check_result
obsess_over_hosts=1
ochp_command=submit_host_alive
enable_notifications=0
8、配置commond.cfg添加
# 'submit_check_result' command definition
define command{
command_name submit_check_result
command_line $USER1$/submit_check_result.sh $HOSTNAME$ '$SERVICEDESC$' $SERVICESTATE$ '$SERVICEOUTPUT$' '$SERVICEPERFDATA$'
}
# 'submit_host_alive' command definition
define command{
command_name submit_host_alive
command_line $USER1$/submit_host_alive.sh $HOSTNAME$ $HOSTSTATE$ '$HOSTOUTPUT$' '$HOSTPERFDATA$'
}
9、建立$USER1$/submit_check_result.sh并修改权限为755,修改执行用户和组为nagios
#!/bin/sh
# Arguments:
# $1 = host_name (Short name of host that the service is
# associated with)
# $2 = svc_description (Description of the service)
# $3 = state_string (A string representing the status of
# the given service - "OK", "WARNING", "CRITICAL"
# or "UNKNOWN")
# $4 = plugin_output (A text string that should be used
# as the plugin output for the service checks)
# $5 = perdata
# Convert the state string to the corresponding return code
return_code=-1
case "$3" in
OK)
return_code=0
;;
WARNING)
return_code=1
;;
CRITICAL)
return_code=2
;;
UNKNOWN)
return_code=-1
;;
esac
# pipe the service check info into the send_nsca program, which
# in turn transmits the data to the nsca daemon on the central
# monitoring server
/usr/bin/printf "%b" "$1\t$2\t$return_code\t$4|$5 \n" | /usr/local/nagios/libexec/send_nsca 外网nagios地址 -c /usr/local/nagios/etc/send_nsca.cfg
10、建立submit_host_alive.sh并修改权限为755,修改执行用户和组为nagios
#!/bin/sh
# Arguments:
# $1 = host_name (Short name of host)
# $2 = state_string (A string representing the status of
# the given service - "UP", "DOWN", "UNREACHABLE")
# $3 = plugin_output (A text string that should be used
# as the plugin output for the service checks)
# $4 = perdata
# Convert the state string to the corresponding return code
return_code=-1
case "$2" in
UP)
return_code=0
;;
DOWN)
return_code=1
;;
UNREACHABLE)
return_code=2
;;
esac
# pipe the service check info into the send_nsca program, which
# in turn transmits the data to the nsca daemon on the central
# monitoring server
/usr/bin/printf "%b" "$1\t$return_code\t$3|$4 \n" | /usr/local/nagios/libexec/send_nsca 外网nagios地址 -c /usr/local/nagios/etc/send_nsca.cfg
后续:外网nagios服务器上和内网nagios服务器定义相同的服务器和服务即可,内网的使用主动模式检测服务器或服务,所有服务消息会通过ocsp_command=submit_check_result,所有的服务器消息会通过ochp_command=submit_host_alive提交到外网nagios服务器,在外网nagios服务器上可以通过查看message日志得到提交的消息日志,前提是nsca.cfg的debug日志开启。
通过这种部署,可以把多台nagios主机的消息汇总到一台上来展现。
linux下查看内存使用情况(转)
Friday, January 22, 2010
老是忘,贴一篇长长记性
在Linux下查看内存我们一般用free命令:
[root@scs-2 tmp]# free
total used free shared buffers cached
Mem: 3266180 3250004 16176 0 110652 2668236
-/+ buffers/cache: 471116 2795064
Swap: 2048276 80160 1968116
下面是对这些数值的解释:
total:总计物理内存的大小。
used:已使用多大。
free:可用有多少。
Shared:多个进程共享的内存总额。
Buffers/cached:磁盘缓存的大小。
第三行(-/+ buffers/cached):
used:已使用多大。
free:可用有多少。
第四行就不多解释了。
区别:第二行(mem)的used/free与第三行(-/+ buffers/cache) used/free的区别。 这两个的区别在于使用的角度来看,第一行是从OS的角度来看,因为对于OS,buffers/cached 都是属于被使用,所以他的可用内存是16176KB,已用内存是3250004KB,其中包括,内核(OS)使用+Application(X, oracle,etc)使用的+buffers+cached.
第三行所指的是从应用程序角度来看,对于应用程序来说,buffers/cached 是等于可用的,因为buffer/cached是为了提高文件读取的性能,当应用程序需在用到内存的时候,buffer/cached会很快地被回收。
所以从应用程序的角度来说,可用内存=系统free memory+buffers+cached。
如上例:
2795064=16176+110652+2668236
接下来解释什么时候内存会被交换,以及按什么方交换。 当可用内存少于额定值的时候,就会开会进行交换。
如何看额定值:
cat /proc/meminfo
[root@scs-2 tmp]# cat /proc/meminfo
MemTotal: 3266180 kB
MemFree: 17456 kB
Buffers: 111328 kB
Cached: 2664024 kB
SwapCached: 0 kB
Active: 467236 kB
Inactive: 2644928 kB
HighTotal: 0 kB
HighFree: 0 kB
LowTotal: 3266180 kB
LowFree: 17456 kB
SwapTotal: 2048276 kB
SwapFree: 1968116 kB
Dirty: 8 kB
Writeback: 0 kB
Mapped: 345360 kB
Slab: 112344 kB
Committed_AS: 535292 kB
PageTables: 2340 kB
VmallocTotal: 536870911 kB
VmallocUsed: 272696 kB
VmallocChunk: 536598175 kB
HugePages_Total: 0
HugePages_Free: 0
Hugepagesize: 2048 kB
用free -m查看的结果:
[root@scs-2 tmp]# free -m
total used free shared buffers cached
Mem: 3189 3173 16 0 107 2605
-/+ buffers/cache: 460 2729
Swap: 2000 78 1921
查看/proc/kcore文件的大小(内存镜像):
[root@scs-2 tmp]# ll -h /proc/kcore
-r-------- 1 root root 4.1G Jun 12 12:04 /proc/kcore
备注:
占用内存的测量
测量一个进程占用了多少内存,linux为我们提供了一个很方便的方法,/proc目录为我们提供了所有的信息,实际上top等工具也通过这里来获取相应的信息。
/proc/meminfo 机器的内存使用信息
/proc/pid/maps pid为进程号,显示当前进程所占用的虚拟地址。
/proc/pid/statm 进程所占用的内存
[root@localhost ~]# cat /proc/self/statm
654 57 44 0 0 334 0
输出解释
CPU 以及CPU0。。。的每行的每个参数意思(以第一行为例)为:
参数 解释 /proc//status
Size (pages) 任务虚拟地址空间的大小 VmSize/4
Resident(pages) 应用程序正在使用的物理内存的大小 VmRSS/4
Shared(pages) 共享页数 0
Trs(pages) 程序所拥有的可执行虚拟内存的大小 VmExe/4
Lrs(pages) 被映像到任务的虚拟内存空间的库的大小 VmLib/4
Drs(pages) 程序数据段和用户态的栈的大小 (VmData+ VmStk )4
dt(pages) 04
查看机器可用内存
/proc/28248/>free
total used free shared buffers cached
Mem: 1023788 926400 97388 0 134668 503688
-/+ buffers/cache: 288044 735744
Swap: 1959920 89608 1870312
我们通过free命令查看机器空闲内存时,会发现free的值很小。这主要是因为,在linux中有这么一种思想,内存不用白不用,因此它尽可能的cache和buffer一些数据,以方便下次使用。但实际上这些内存也是可以立刻拿来使用的。
所以 空闲内存=free+buffers+cached=total-used
nagios 配合 pnp 作图xml error问题
Monday, January 18, 2010
nagios配合pnp作图,只要nagios吐出perfdata就可以了。
在nagios为了能在页面上显示中文,配置了主机的hostgroup中的alias为中文,pnp作图问题就来了
打开被设置过中文的服务器pnp页面显示
XML error: Invalid character at line 107 in /var/spool/nagios/pnp/rrd/10.80.0.10/_HOST_.xml
而其他的没有设置过中文的能正常显示图形
查看了/var/spool/nagios/pnp/rrd/10.80.0.10/_HOST_.xml文件的第107行,有中文字符
上服务器,到pnp的web路径下
grep "XML error" * -rn
找到
include/function.inc.php:1510: $debug->doCheck("xml_err","XML error: ".xml_error_string(xml_get_error_code($xml_parser))." at line ".xml_get_current_line_number($xml_parser)." in ".$rrddef);
打开include/function.inc.php
if (!xml_parse_into_struct($xml_parser, $data, $vals, $index)) {
$debug->doCheck("xml_err","XML error: ".xml_error_string(xml_get_error_code($xml_parser))." at line ".xml_get_current_line_number($xml_parser)." in ".$rrddef);
}
原来在xml_parse_into_struct时有错误抛出,所以就无法作图,分析是$data中有中文字符造成。
那就用iconv转一下字符喽
$xml_parser = xml_parser_create();
if(($handle = fopen($rrddef, "rb")) === false ){
return $NAGIOS;
}
$contents = '';
while (!feof($handle)) {
$data .= fread($handle, 8192);
}
fclose($handle);
$data=iconv("GB2312","UTF-8",$data);
if (!xml_parse_into_struct($xml_parser, $data, $vals, $index)) {
$debug->doCheck("xml_err","XML error: ".xml_error_string(xml_get_error_code($xml_parser))." at line ".xml_get_current_line_number($xml_parser)." in ".$rrddef);
}
好了,大功告成,现在中文别名也能作图了。
用Gmail来写blogger测试
Saturday, January 9, 2010
网上查了一下用Gmail直接可以写自己的blog,那就试试看,希望通过这个,更新blog会勤一些。