玩蛇网提供最新Python编程技术信息以及Python资源下载!

从日志文件中提取ip并找到归属地完成输出

python 培训

从日志文件中提取ip并找到归属地完成输出。意思是从apache log的文件里有许多ip地址,从这些ip找到归属地,之后把信息写到文件里的操作

import os,re
from getIpFrom import Httpsocket
httpIP = Httpsocket()

LogFilePath = "./logs/logs/"
OutPutPath = "./outs/"
LogFileList = [
"site1-access_log",
"site1-error_log",
"site2-access_log",
"site2-error_log",
"site3-access_log",
"site3-error_log"]

def CheckLogFile():
    for f in LogFileList:
      if not os.path.isfile(LogFilePath+f):
         print "logfile is not existed  %s" %f
         raise SystemExit

def GetIpList(logfile):
    temp = []

    fd = open(LogFilePath+logfile,'rb')

    line = fd.readline()

    while(line):
        r = re.findall(r'(\d+\.\d+\.\d+\.\d+).*', line)
        if len(r) == 1:
           if r[0] not in temp:
              temp.append(r[0])
        else:
           print "GetIpList occuer error ip list r size:" + str(len(r))
           raise SystemExit
        line = fd.readline();
    fd.close()

    if not os.path.isdir(OutPutPath):
       os.mkdir(OutPutPath)

    op = open(OutPutPath+logfile+"_IP",'wb')
    ipstr='';

    for ip in temp:
        httpIP.setbody(ip)
        (code,ipaddress) = httpIP.getIP()
        if (code == 0):
            ipstr = ip+"\t"+ipaddress
        else:
            ipstr = ip+"\tÎÞ·¨È·¶¨"
        op.write(ipstr+os.linesep);
    op.close()

def process():
    CheckLogFile()

    for f in LogFileList:
      print "log file:"+f

      if not os.path.isfile(LogFilePath+f):
         print "logfile is not existed  %s" %f

      else:
         GetIpList(f)


if __name__ == '__main__':
   process()


python方法从日志文件中提取ip并找到归属地完成输出

import re,urllib,urllib2,cookielib

ipmat = re.compile('<li>本站主数据:.*</li>', re.M)

class Httpsocket:
    """ Build for Make a full HttpRequest via POST/GET """
    isok = 0  #default 0
    ip_url = 'http://www.ip138.com/ips.asp'
    ip_send = 'http://www.ip138.com/ips8.asp'
    ip_body = []

    def __init__(self):
        self.cookies = urllib2.HTTPCookieProcessor()
        self.opener  = urllib2.build_opener(self.cookies)
        urllib2.install_opener(self.opener)

    def connect(self,ip_url,param={},header={}):
        encodeparam = urllib.urlencode(param)
        urllib2.urlopen(urllib2.Request(ip_url,encodeparam,header) )

    def openurl(self,url,param={},header={}):
        encodeparam = urllib.urlencode(param)
        req = urllib2.Request(url,encodeparam,header)
        return urllib2.urlopen(req) 

    def setbody(self,ip):
        self.ip_body = [('ip',ip),('action','2'),]

    def getIP(self):
        r1 = (0,'')
        body = self.ip_body[:]
        try:
           self.connect(self.ip_url)
        except Exception,e:
           return (1,'')
        try:
           u = self.openurl(self.ip_url)
           data = u.read()
           if "您的IP地址是" in data:
               u2 = self.openurl(self.ip_send,param=body)
               data2 = u2.read()
               r1 = ipmat.findall(data2)
               if (len(r1)==1):
                  r1 = r1[0];
                  r1=r1.replace("</li><li>","\t2");
                  r1=r1.replace("<li>","1");
                  r1=r1.replace("</li>","1");
                  r1=(0,r1)
           else:
               return (1,'')
        except Exception,e:
           return (1,'')
        retu
2000
rn r1

玩蛇网原创,转载请注明文章出处和来源网址:http://www.iplaypython.com/code/base/b2454.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

玩蛇网Python新手QQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
文章发布日期:2016-04-09 20:56 玩蛇网 www.iplaypython.com

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
相关文章推荐
别人正在看
特别推荐
去顶部去底部