工具函数

1.由域名获取IP地址

import urllib
import httplib
import re
import urlparse
import json
def ip(host_addr,format="dot"):
    '''
    describe:
        function try to find the ip addr of the given host addr
    params:
        host_addr:the format like www.baidu.com or http://www.baidu.com
        format:the format of return value
                dot=>ip string like 123.121.121.21,
                num=>number format like 327382483
    return :IP string list or number list when num specified
    '''
    query_url='http://ip.chinaz.com/?'
    if host_addr.startswith("http"):
        req_url=urlparse.urlsplit(host_addr).netloc
    else:
        req_url=host_addr
    r=urllib.urlopen(query_url+"IP="+req_url)
    ipRex=r':\s+([\d\.]*)\s'
    try:
        if r !=None:
            context=r.read()
            rst=re.findall(ipRex,context)          
            if rst!=None:
                if format=="num":
                    rss=[]
                    for rs in rst:                    
                        sum=0
                        for  f in enumerate(rs.split('.')):
                            sum+=int(f[1])<<((3-f[0])*8)
                        rss.append(str(sum))
                    del rst
                    rst=rss
                return rst
        return None
    except :
        return None
print ip('www.baidu.com')
print ip('www.baidu.com','num')

 

测试结果:

['220.181.111.147']
['3702878099']

参考:http://www.oschina.net/code/snippet_79695_16093

 

 

2. IP地址和整数之间的转换函数


 

import socket
import struct

def str2uint(str1):     
    return socket.ntohl(struct.unpack("I",socket.inet_aton(str1))[0])  
    # 得到始终是正数    
def str2int(str1):  
    uint = socket.ntohl(struct.unpack("I",socket.inet_aton(str1))[0])  
    return struct.unpack("i", struct.pack('I', uint))[0]  # 先得到负数,再转换一下    
def num2str(ip):  
    if ip < 0:  
        ip = struct.unpack("I", struct.pack('i', ip))[0]  
    return socket.inet_ntoa(struct.pack('I',socket.htonl(ip)))  


测试结果:

print str2uint("10.7.11.213")
print str2int("10.7.11.213")
print num2str(168233941)


转自:http://blog.csdn.net/rainharder/article/details/6107154

3.判断文件编码并获取文件内容

__author__ = 'soso_fy'

#codeing:utf-8


# 写python脚本经常要用到的一些函数
# 免得每次都重写蛋疼
# require python 3.2 or later

import os
import codecs

# 读取文本文件函数,支持bom-utf-8,utf-8,utf-16,gbk,gb2312
# 返回文件内容
def ReadTextFile(filepath):
    try:
        file = open(filepath, 'rb')
    except IOError as err:
        print('读取文件出错 in ReadFile', err)
    bytes = file.read()
    file.close()
    if bytes[:3] == codecs.BOM_UTF8:
        content = bytes[3:].decode('utf-8')
    else:
        try:
            content = bytes.decode('gb2312')
        except UnicodeDecodeError as err:
            try:
                content = bytes.decode('utf-16')
            except UnicodeDecodeError as err:
                try:
                    content = bytes.decode('utf-8')
                except UnicodeDecodeError as err:
                    try:
                        content = bytes.decode('gbk')
                    except UnicodeDecodeError as err:
                        content = ''
                        print('不支持此种类型的文本文件编码', err)
    return content


测试结果:

print ReadTextFile('C://python2.7//myTest//kp.conf')运行结果
realserver 192.168.191.129 10447{
 weight 3
}realserver 192.168.191.130 80{
 weight 3
}


4.获取目录下的特定扩展名的文件名

__author__ = 'soso_fy'

#codeing:utf-8


# 获取指定路径下所有指定后缀的文件
# dir 指定路径
# ext 指定后缀,链表&不需要带点或者不指定。例子:['xml', 'java']
def GetFileFromThisRootDir(dir,ext = None):
    allfiles = []
    needExtFilter = (ext != None)
    if needExtFilter:
        ext = list(map(lambda x:x.lower(), ext))
    for root,dirs,files in os.walk(dir):
        for filespath in files:
            filepath = os.path.join(root, filespath).lower()
            extension = os.path.splitext(filepath)[1][1:]
            if needExtFilter and extension in ext:
                allfiles.append(filepath)
            elif not needExtFilter:
                allfiles.append(filepath)
    return allfiles


测试结果:
print GetFileFromThisRootDir('C://python2.7//myTest',['txt,conf'])

 

运行结果

['c://python2.7//mytest\\1.txt', 'c://python2.7//mytest\\conf_1000.conf', 'c://python2.7//mytest\\kp.conf', 'c://python2.7//mytest\\rs.conf', 'c://python2.7//mytest\\test.conf']

上面2个转自:http://www.oschina.net/code/snippet_272860_16194

5. unicode编码转换成utf8编码


def h2b(x):
	"""十六进制转二进制"""
	result = []
	if x==0:
		return ['0']
	while x != 1:
		result.append(str(x%2))
		x /= 2
	result.append('1')
	result.reverse()
	if len(result) % 4 != 0:
		for i in range(0,(4 - len(result) % 4)):
			result.insert(0,'0')
	return "".join(result)

def c2u(x):
	"""unicode 转 utf8"""
	result = ''
	if 0x0 <= x and x <= 0x7F:
		result =  h2b(x)
	elif 0x80 <= x and x <= 0x7FF:
		if len(h2b(x)) == 8:
			tmp = '000' + h2b(x)
		else:
			tmp = h2b(x)[1:]
		result = '110'+ tmp[0:5] + '10' + tmp[5:]
	elif 0x800 <= x and x <= 0xFFFF:
		if len(h2b(x)) == 12:
			tmp = '0000' + h2b(x)
		else:
			tmp = h2b(x)
		result = '1110'+ tmp[0:4] + '10' + tmp[4:10] + '10' + tmp[10:]
	return b2h(result)

def b2h(x):
	"""二进制转十六进制"""
	hex = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9' , 'A' , 'B' , 'C' , 'D' , 'E' , 'F']
	result = []
	def getValue(bin):
		value = 0
		for i in range(4):
			value += int(bin[i]) * 2**(3-i)
		return value
	for i in range(len(x)/4):
		result.append(hex[getValue(x[i*4:(i+1)*4])])
	return result


转自:http://www.oschina.net/code/snippet_223232_15787

6.统计日志中IP出现的次数

#-*- coding:utf-8 -*-

import re,time

def mail_log(file_path):
    global count
    log=open(file_path,'r')
    C=r'\.'.join([r'\d{1,3}']*4)
    find=re.compile(C)
    count={}
    for i in log:
        for ip in find.findall(i):
            count[ip]=count.get(ip,1)+1
if __name__ == '__main__':
    print time.clock()
    num=0
    mail_log(r'e:\MDaemon-20110329-all.log')
    R=count.items()
    for i in R:
        if i[1]>0: #提取出现次数大于0的IP
            print i
            num+=1
    print '符合要求数量:%s耗时(%s)'%(num,time.clock())
             


 

测试结果:(这脚本可用于多种日志类型,本人测试MDaemon的all日志文件大小1.23G左右,分析用时2~3分钟
代码很简单,很适合运维人员,有不足的地方请大家指出哦)

('206.220.200.250', 8)
('66.40.52.37', 10)
('66.40.52.36', 5)
('207.115.11.41', 4)
('96.47.193.25', 9)
('96.47.193.24', 5)
('96.47.193.23', 17)
('72.32.181.92', 5)
http://www.oschina.net/code/snippet_591089_12662

 

 

 5.subprocess函数的使用测试

import subprocess

def disk_report():
    p = subprocess.Popen("df -h ",shell=True,stdout=subprocess.PIPE)
    return p.stdout.readlines()
print disk_report()


 结果:

['Filesystem            Size  Used Avail Use% Mounted on\n', '/dev/sda2              19G  9.9G  8.2G  55% /\n', '/dev/sda5             414G  6.4G  386G   2% /var\n', '/dev/sda1              99M   11M   83M  12% /boot\n', 'tmpfs                 4.0G     0  4.0G   0% /dev/shm\n']


 

 6.复制文件夹

def CopyFolderOs(sFolder,tFolder):
    sourcePath = sFolder
    destPath = tFolder
    for root, dirs, files in os.walk(sourcePath):

        #figure out where we're going
        dest = destPath + root.replace(sourcePath, '')

        #if we're in a directory that doesn't exist in the destination folder
        #then create a new folder
        if not os.path.isdir(dest):
            os.mkdir(dest)
            print 'Directory created at: ' + dest

        #loop through all files in the directory
        for f in files:

            #compute current (old) & new file locations
            oldLoc = root + '\\' + f
            newLoc = dest + '\\' + f

            if not os.path.isfile(newLoc):
                try:
                    shutil.copy2(oldLoc, newLoc)
                    print 'File ' + f + ' copied.'
                except IOError:
                    print 'file "' + f + '" already exists'


测试结果:

CopyFolderOs("C://python2.7//myTest","C://python2.7//myTestbak")


 

Directory created at: C://python2.7//myTestbak
File Conf_1000.conf copied.
File ip.log copied.
File kp.conf copied.
File RS.conf copied.
File test.conf copied.

 

 7.复制文件夹里面的文件

 

def RemoveFolderOs(sourceDir,localAppDataPath):
    for root, dirs, files in os.walk(sourceDir):
        for f in files:
            os.unlink(os.path.join(root, f))
        for d in dirs:
            shutil.rmtree(os.path.join(root, d))


测试结果:

RemoveFolderOs("C://python2.7//myTestbak")

myTestbak文件夹还存在

 

 转自 http://www.oschina.net/code/snippet_72895_1576

 

8.创建守护进程

def createDaemon():
    ”’Funzione che crea un demone per eseguire un determinato programma…”’
   
    import os
   
    # create - fork 1
    try:
        if os.fork() > 0: os._exit(0) # exit father…
    except OSError, error:
        print ‘fork #1 failed: %d (%s)’ % (error.errno, error.strerror)
        os._exit(1)

    # it separates the son from the father
    os.chdir(’/')
    os.setsid()
    os.umask(0)

    # create - fork 2
    try:
        pid = os.fork()
        if pid > 0:
            print ‘Daemon PID %d’ % pid
            os._exit(0)
    except OSError, error:
        print ‘fork #2 failed: %d (%s)’ % (error.errno, error.strerror)
        os._exit(1)

    funzioneDemo() # function demo
   
def funzioneDemo():

    import time

    fd = open('/tmp/demone.log', 'w')
    while True:
        fd.write(time.ctime()+'\n')
        fd.flush()
        time.sleep(2)
    fd.close()
   
if __name__ == '__main__':

    createDaemon()


转自:http://www.oschina.net/code/snippet_16840_1896

9.执行外部命令

通常我们调用os.system(cmd) 只能获得命令是否能执行成功。即结果为0或者非0标识是否执行成功。
而有时我们希望即获取到是否成功,同时也获取命令的执行结果。
这时就可以使用commands了,通过它可以同时获取命令的执行结果输出和结果。
这样ret就反馈是否执行成功,比如为0(成功) 或者非0(不成功)
output 用来获取ls命令的执行结果。

 

import commands  
ret, output = commands.getstatusoutput('ls')
print ret    
print output

转自:http://www.cnblogs.com/lovemdx/archive/2013/03/08/2950301.html

10.文件内容的查找和替换

查找

import re
myfile = file("C://python2.7//myTest//file.txt", "r+")
count = 0
for s in myfile.readlines():      
    li = re.findall("hello", s)   
    if len([li]) > 0:                     
        count =  count + li.count("hello")
print "find:" + str(count) + "times hello"  
myfile.close()

文件内容的查找:从hello.txt中查找字符串“hello”, 并统计“hello”出现的次数
结果:find:10times hello

替换:文件内容的替换:把hello.txt中的hello全部换为”hi“,并把结果保存到myhello.txt中。

f1 = file("C://python2.7//myTest//file.txt", "r")
f2 = file("C://python2.7//myTest//myhello.txt", "w")
for s in f1.readlines():
    f2.write(s.replace("hello","hi"))        
                                                                        
f1.close()
f2.close()


 11.目录的遍历

方法1:递归

import os
def VisitDir(path) :
    li = os.listdir(path) 
    for p in li:
        pathname = os.path.join(path, p)
        if not os.path.isfile(pathname) :
            VisitDir(pathname)
        else:
            print pathname

if __name__ == "__main__":
    path = r"C://python2.7//myTest"
    VisitDir(path)

2.walk()函数
os.walk()的执行效率最高,且不需要回调函数,容易使用。os.walk()的声明如下:
walk(top, topdown=ture, onerron= none)
(1)top表示需要遍历的目录树的路径。
(2)参数topdown的默认值为ture,表示先返回目录树下的文件,然后再遍历目录树的子目录,topdow为          false表示先遍历目录树下的子目录,然后返回根目录树的文件
(3)onerror为none表示忽略文件遍历时产生的错误。
(4)该函数返回一个元组,该元组有三个元素,分别是:每次遍历的路径名,目录列表和文件列表

def VisitDirWalk(path) :
    
    for root, dirs, files in os.walk(path):
        print 'root:',root
        print 'dirs:',dirs
        print 'files:',files
        for filepath in files:
            print os.path.join(root, filepath)


测试结果:

path = r"C://python2.7//myTest"
   VisitDirWalk(path)

root: C://python2.7//myTest
dirs: ['test']
files: ['Conf_1000.conf', 'file.txt', 'ip.log', 'kp.conf', 'myhello.txt', 'RS.conf', 'test.conf']
C://python2.7//myTest\Conf_1000.conf
C://python2.7//myTest\file.txt
C://python2.7//myTest\ip.log
C://python2.7//myTest\kp.conf
C://python2.7//myTest\myhello.txt
C://python2.7//myTest\RS.conf
C://python2.7//myTest\test.conf
root: C://python2.7//myTest\test
dirs: []
files: ['Conf_1000.conf']
C://python2.7//myTest\test\Conf_1000.conf


12.删除py中的注释

f = open('file1.py', 'r+')
line = f.readlines()
f.seek(0)
for l in line:
    for i in range(len(l)):
        if l[i]=='#':
            l=l[:i]+' '*(len(l)-i-1)+'\n'
            break
    f.write(l)
f.close()



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值