圆月山庄资源网 Design By www.vgjia.com

本文实例讲述了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'("共解析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.jb51.net/regex/javascript

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

更多关于Python相关内容可查看本站专题:《Python正则表达式用法总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

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

标签:
Python,log日志,提取,ip,正则

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。