Python实现从log日志中提取ip的方法【正则提取】

掉漆的栏杆上,反射着夕阳的余晖渐渐消逝。我们的青春就随这样随着夕阳落幕。绮丽多姿扮靓妆,繁花锦簇沁心香。长歌游宝地,徒倚对珠林。家门口的风景,其实眼前的就是最好的,爱你选择的一切!

本文实例讲述了Python实现从log日志中提取ip的方法。分享给大家供大家参考,具体如下:

log日志内容如下(myjob.log):

124.90.53.68 - - [05/Feb/2018 11:37:07] "GET /favicon.ico HTTP/1.1" 404 -
61.148.245.145 - - [05/Feb/2018 12:37:44] "GET / HTTP/1.1" 200 -
61.148.245.145 - - [05/Feb/2018 12:37:44] "GET /apple-touch-icon-120x120-precomposed.png HTTP/1.1" 404 -
61.148.245.145 - - [05/Feb/2018 12:37:44] "GET /apple-touch-icon-120x120.png HTTP/1.1" 404 -
61.148.245.145 - - [05/Feb/2018 12:37:45] "GET /apple-touch-icon-precomposed.png HTTP/1.1" 404 -
61.148.245.145 - - [05/Feb/2018 12:37:45] "GET /apple-touch-icon.png HTTP/1.1" 404 -
61.148.245.145 - - [05/Feb/2018 12:37:45] "GET /static/favicon.ico HTTP/1.1" 200 -
101.226.33.218 - - [05/Feb/2018 13:07:39] "GET / HTTP/1.1" 200 -
101.226.33.219 - - [05/Feb/2018 13:09:46] "GET / HTTP/1.1" 200 -
101.226.33.219 - - [05/Feb/2018 13:09:46] "GET /static/youkulogo.png HTTP/1.1" 200 -
101.226.33.219 - - [05/Feb/2018 13:09:46] "GET /static/iqiyi.png HTTP/1.1" 200 -
101.226.33.219 - - [05/Feb/2018 13:09:46] "GET /static/qqlogo.png HTTP/1.1" 200 -
124.202.223.62 - - [05/Feb/2018 14:29:45] "GET / HTTP/1.1" 200 -
124.202.223.62 - - [05/Feb/2018 14:29:47] "GET /static/youkulogo.png HTTP/1.1" 200 -
124.202.223.62 - - [05/Feb/2018 14:29:48] "GET /static/qqlogo.png HTTP/1.1" 200 -
124.202.223.62 - - [05/Feb/2018 14:29:48] "GET /static/iqiyi.png HTTP/1.1" 200 -
124.202.223.62 - - [05/Feb/2018 14:29:49] "GET /static/favicon.ico HTTP/1.1" 200 -

提取ip:

# encoding: utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import pandas as pd
import re
import time
import requests
time1=time.time()
######函数功能:能够提取ip地址,并且去重################
def read_file(input_file_name,output_file_name):
  _fLog = open(input_file_name)
  sep = '\n'
  ip_list=[]
  for each in _fLog:
    ip=re.findall(r'(?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d])',str(each),re.S)
    ip_list.append(ip[0])
  # 列表去重:通过set方法进行处理
  ids = list(set(ip_list))
  print "共解析ip个数:%s "% len(ids)
  ##写出数据到本地
  # 设置输出文件路径
  out = open(output_file_name, "a")
  # out.write("ip" + sep)
  for each in ids:
    print each
    out.write(each + sep)
  ##关闭连接
  out.close()
  _fLog.close()
  print "ip提取完毕~~"
####主函数################
if __name__ == '__main__':
  input_file_name = "C:/myjob.log"
  output_file_name = "c:/myjob.txt"
  read_file(input_file_name, output_file_name)
  time2 = time.time()
  print u'总共耗时:' + str(time2 - time1) + 's'

运行结果:

共解析ip个数:5
61.148.245.145
124.90.53.68
124.202.223.62
101.226.33.219
101.226.33.218
ip提取完毕~~
总共耗时:0.000999927520752s
Process finished with exit code 0

PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:

JavaScript正则表达式在线测试工具:
http://tools.haodaima.com/regex/javascript

正则表达式在线生成工具:
http://tools.haodaima.com/regex/create_reg

希望本文所述对大家Python程序设计有所帮助。

到此这篇关于Python实现从log日志中提取ip的方法【正则提取】就介绍到这了。与其担心未来,不如现在好好努力。人生没有彩排,每个人有每个人的。不是所有的人都会完美。岁月如歌,人生如潮。有一颗随缘心,你会更洒脱;有一颗平常心,你会更从容;有一颗慈悲心,你会更积善;有一颗感恩心,你会更幸福;有一颗因果心,你会更明理。更多相关Python实现从log日志中提取ip的方法【正则提取】内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

标签: Python log