Created
September 5, 2008 09:24
-
-
Save qingfeng/8947 to your computer and use it in GitHub Desktop.
mini script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
""" | |
Created by yanxu on 2008-10-16. | |
Copyright (c) 2008 Sina.com. All rights reserved. | |
""" | |
from __future__ import with_statement | |
import string,re | |
import MySQLdb | |
IDC_DATA = { | |
"武汉":["221.235.54.0/26","59.175.132.0/25"] | |
} | |
conn = MySQLdb.connect(user='yanxu', | |
passwd='1E9njhAIZP', | |
host='10.210.136.79', | |
db='CMDB') | |
netmask = lambda n:2**32-2**(32-n) | |
def to_digit(addr): | |
lst = map(string.atoi, addr.strip().split('.')) | |
return reduce(lambda x,y:x*256+y,lst) | |
def belongs(addr,network): | |
""" 判断某一个IP是否在某一个网段 | |
>>> belongs("221.235.54.1","221.235.54.0/26") | |
True | |
>>> belongs("221.235.54.200","221.235.54.0/26") | |
False | |
>>> belongs("59.175.132.1","59.175.132.0/25") | |
True | |
>>> belongs("59.175.132.200","59.175.132.0/25") | |
False | |
>>> belongs("10.210.132.70","59.175.132.0/25") | |
False | |
""" | |
naddr,nm = network.split('/') | |
mask = netmask(string.atoi(nm)) | |
return to_digit(addr) & mask == to_digit(naddr) | |
def belongs_city(idc,ip): | |
""" | |
>>> belongs_city("武汉","221.235.54.1") | |
True | |
>>> belongs_city("武汉","221.235.54.200") | |
False | |
>>> belongs_city("武汉","222.235.54.1") | |
False | |
""" | |
ips=IDC_DATA.get(idc,"") | |
if ips=="":return False | |
isIpRange=filter(lambda ipdata:len(ipdata)>0, | |
filter(lambda idc_ip:belongs(ip,idc_ip),ips) | |
) | |
if len(isIpRange)>0:return True | |
return False | |
def getData(idc,idc_ips): | |
okip=[] | |
with conn as cursor: | |
cursor.execute("SET NAMES 'utf8'") | |
sql='''SELECT IP地址,盘点号,所属产品线,类别 FROM view_all_unit | |
WHERE 所在城市=%s | |
AND 类别="交换机" | |
''' | |
cursor.execute(sql,(idc,)) | |
rows=cursor.fetchall() | |
for ips,pandian,sv,dtype in rows: | |
if ips==None:continue | |
ips=ips.strip().split(" ") | |
ips=filter(lambda x:x!="",ips) | |
for ip in ips: | |
if belongs_city(idc,ip): | |
okip.append( (ip,pandian,sv,dtype) ) | |
continue | |
return okip | |
def getsv(ip,port): | |
sql = """SELECT 目标设备产品线 FROM view_connection | |
WHERE 源设备IP地址 LIKE %s | |
AND 源设备端口号=%s | |
AND 源设备类别="交换机" | |
AND 目标设备类别="服务器" | |
AND 源设备IP地址<>"" AND 目标设备IP地址<>"" | |
""" | |
with cursor in conn: | |
cursor.execute(sql,("%%%s%%"%ip,port)) | |
rows=cursor.fetchall() | |
for r in rows: | |
pass | |
def getPortName(switchtype,port): | |
""" | |
>>> getPortName("e1200-123","12345a") | |
'' | |
>>> getPortName("WS-C3750G-24TS-S","323") | |
'Gi-1/0/23' | |
>>> getPortName("WS-C3750G-24TS-S","212") | |
'Fa-1/0/12' | |
>>> getPortName("WS-C3750G-24TS-S","30203") | |
'Gi-2/0/3' | |
>>> getPortName("WS-C3750G-24TS-S","20105") | |
'Fa-1/0/5' | |
>>> getPortName("WS-C3550-48-SMI","319") | |
'Gi-0/19' | |
>>> getPortName("WS-C3550-48-SMI","202") | |
'Fa-0/2' | |
""" | |
switchtype=switchtype.strip() | |
ignore_port = [] | |
ignore_switch = ['LS-5500-28C-SI','e1200','Extreme','SW-STACK'] | |
if len(filter(lambda p_re:re.search(p_re,port),ignore_port))>0:return "" | |
if len(filter(lambda s_re:re.search(s_re,switchtype),ignore_switch))>0:return "" | |
def fsm(sw,length,first,porttype,portend): | |
if re.search(sw,switchtype) or sw=="": | |
if len(port)==length or length=="": | |
if re.search(first,port): | |
return porttype,portend | |
return "0","0" | |
switch_fsm = [ | |
["WS-C3750",5,r"^3","Gi-%s/0/"%port[2],port[3:]], | |
["WS-C3750",5,r"^2","Fa-%s/0/"%port[2],port[3:]], | |
["WS-C3750",3,r"^3","Gi-1/0/",int(port)-300], | |
["WS-C3750",3,r"^2","Fa-1/0/",int(port)-200], | |
["","",r"^3","Gi-0/",int(port)-300], | |
["","",r"^2","Fa-0/",int(port)-200], | |
] | |
for sw,length,first,porttype,portend in switch_fsm: | |
portype,newport=fsm(sw,length,first,porttype,portend) | |
if portype!="0":break | |
return "".join( (portype,"%s"%int(newport)) ) | |
def main(): | |
""" | |
武汉外网网段: | |
221.235.54.0/26 | |
59.175.132.0/25 | |
""" | |
# for idc,idc_ips in IDC_DATA.items(): | |
# okip = getData(idc,idc_ips) | |
# for ip,pandian,sv,dtype in okip: | |
# print ip,pandian,sv,dtype | |
conn.close() | |
if __name__ == '__main__': | |
import doctest | |
doctest.testmod() | |
main() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
# -*- coding: utf-8 -*- | |
from __future__ import division | |
from operator import mul | |
def P(m,n): | |
''' | |
基本概率计算: P(E) | |
''' | |
return m/n | |
def per(n): | |
''' | |
计算阶乘 | |
''' | |
return reduce(mul,range(n,0,-1)) | |
def combination(n,r): | |
''' | |
计算组合 | |
''' | |
return per(n)/( per(r)*per(n-r) ) | |
import unittest | |
class TestPermutations(unittest.TestCase): | |
def setUp(self): | |
pass | |
def testPer(self): | |
self.assertEqual(per(5),5*4*3*2*1) | |
def testComb(self): | |
self.assertEqual(combination(n=4,r=2),6) | |
self.assertEqual(int(round(combination(n=49,r=6))),13983816) | |
def testBasicpe(self): | |
''' | |
P(掷一公平骰子得出6)=1/6 | |
''' | |
self.assertEqual(P(1,6),1.0/6.0) | |
def testAdd(self): | |
''' | |
加法定理验证 | |
如果掷两粒公平骰子,擲出2和4的概率分别是多少? | |
''' | |
self.assertEqual( P(1,36),1/36 ) | |
self.assertEqual( P(1,36)+P(1,36)+P(1,36),3/36 ) | |
def testMultiplication(self): | |
''' | |
乘法定理验证 | |
假设掷三粒公平骰子所得出数字之积为k。P(k是奇数)是多少? | |
P(k是奇数)=P(第一个数是奇数)*P(第二个数是奇数)*P(第三个数是奇数)=3/6*3/6*3/6=1/8 | |
''' | |
self.assertEqual( P(3,6)*P(3,6)*P(3,6),1/8 ) | |
if __name__ == '__main__': | |
unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import md5 | |
def main(): | |
name="播客后台" | |
namegbk=name.decode("utf8").encode("gb2312") | |
namelatin=name.decode("utf8").encode("latin-1","ignore") | |
md5_php_utf8 = "aad4cda5e6d61bcbed956adf50150899" | |
md5_python_utf8 = md5.md5(name).hexdigest() | |
print md5_python_utf8 | |
print md5_python_utf8==md5_php_utf8 | |
md5_php_gbk = "f0e816868794151ea78b28348285918a" | |
md5_python_gbk = md5.md5(namegbk).hexdigest() | |
print md5_python_gbk | |
print md5_python_gbk==md5_php_gbk | |
md5_php_latin = "4f6ffe13a5d75b2d6a3923922b3922e5" | |
md5_python_latin = md5.md5(namelatin).hexdigest() | |
print md5_python_latin | |
print md5_python_latin==md5_php_latin | |
if __name__ == '__main__': | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import re | |
import urllib2 | |
def main(): | |
html = urllib2.urlopen("http://news.sina.com.cn/c/2008-09-05/151416239742.shtml").read() | |
title = re.findall(r'<h1 id="artibodyTitle">(.*)</h1>',html)[0] | |
print title | |
c=re.compile(r'''<div class="blkContainerSblkCon" id="artibody">(.*)</div></td></tr></table></div>''',re.S) | |
print c.findall(html)[0].decode("gbk").encode("utf8") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment