python 系统时间 以及时间比较

文本内容如下:
192.168.100.125  UNKNOWN  w0100441  [03/Jun/2015:16:16:26 +0800] 226 119096 0.014
..........................
63.54.78.89         UNKNOWN  w0500465  [03/Sep/2015:16:16:26 +0800] 275 119098 0.019
..........................
61.30.89.6           UNKNOWN  kobe          [15/Oct/2015:09:15:35 +0800] 426 1109168 3.215
..........................
192.168.100.126  UNKNOWN  dingjunhui  [15/Mar/2016:09:15:35 +0800] 426 1109168 3.215
..........................
220.86.123.140    UNKNOWN  idenbo       [03/May/2016:09:41:27 +0800] 226 7068129 12.102

上面的文件是一个OS的登录日志,第一列是登录到服务器的客户端的IP地址,第三列是帐号,以w开头的是公司内网帐号,
其余的均为外网帐号。192.168.100的为内网IP,其余的均为外网IP,我现在想要找出三个月内,
以内网帐号和外网IP登录服务器的行(例如 63.54.78.89     UNKNOWN  w0500465),
和以外网帐号和内网IP登录服务器的行(例如: 192.168.100.126  UNKNOWN  dingjunhui),
用Python如何来实现?如果这里查找的时间是个变量,即找出四个月内,五个月内的信息(依次类推),用Python语言如何来写?

 

#!/usr/bin/env python
# -*- coding: utf-8 -*- 

import sys
import getopt
from datetime import datetime
import time
from dateutil.relativedelta import relativedelta
import re

__author__ = 'shengwei ma'
__author_email__ = 'shengweima@icloud.com'

input_file = ""
output_file = ""
a = ""

try:
    opts, args = getopt.getopt(sys.argv[1:], "hm:", ["input=", "output="])
except getopt.GetoptError as err:
    print(str(err))
for op, value in opts:
    if op == "--input":
        input_file = value
    elif op == "-m":
        a = int(value)
    elif op == "--output":
        output_file = open(value, 'w')
    elif op == "-h":
        print("python filter_by_month.py --input your_input_file -m 3 --output your_out_file")
        sys.exit()

months = datetime.now() + relativedelta(months=-a)
month = months.strftime("%d/%b/%Y %H:%M:%S")
print 'You will find after this time %s line!' % month

with open(input_file, 'r') as f:
    for num, line in enumerate(f):
        if num % 2 == 0:
            new_line = line.strip().split()
            date = new_line[3].lstrip('[').split(':')
            new_date = date[0] + ' ' + date[1] + ':' + date[2] + ':' + date[3]
            timeArray_b = time.strptime(new_date, "%d/%b/%Y %H:%M:%S")
            timeArray_a = time.strptime(month, "%d/%b/%Y %H:%M:%S")
            if timeArray_b > timeArray_a:
                if new_line[0].startswith('192') and re.findall(r'[^w]', new_line[2]):
                    output_file.write(line)
                elif new_line[2].startswith('w') and re.findall(r'[^192]', new_line[0]):
                    output_file.write(line)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中有多种处理时间的方式,下面是一些常用的时间比较方法: 1. 使用datetime模块:datetime模块提供了datetime类,可以用于表示日期和时间。可以通过比较两个datetime对象来进行时间比较。例如,可以使用datetime.now()获取当前时间,并与其他时间进行比较。 示例代码: ```python from datetime import datetime # 获取当前时间 now = datetime.now() # 创建一个指定时间的datetime对象 other_time = datetime(2022, 1, 1, 0, 0, 0) # 比较两个时间 if now > other_time: print("当前时间晚于指定时间") else: print("当前时间早于指定时间") ``` 2. 使用time模块:time模块提供了一些处理时间的函数,例如time.time()可以获取当前时间时间戳,可以通过比较两个时间戳来进行时间比较。 示例代码: ```python import time # 获取当前时间时间戳 now_timestamp = time.time() # 创建一个指定时间时间戳 other_timestamp = time.mktime((2022, 1, 1, 0, 0, 0, 0, 0, 0)) # 比较两个时间戳 if now_timestamp > other_timestamp: print("当前时间晚于指定时间") else: print("当前时间早于指定时间") ``` 3. 使用dateutil模块:dateutil模块是一个第三方库,提供了加灵活和方便的时间处理方法。可以使用dateutil.parser.parse()函数将字符串解析为datetime对象,然后进行比较。 示例代码: ```python from dateutil.parser import parse # 解析字符串为datetime对象 now = parse("2022-01-01 00:00:00") # 创建一个指定时间的datetime对象 other_time = parse("2022-01-01 00:00:00") # 比较两个时间 if now > other_time: print("当前时间晚于指定时间") else: print("当前时间早于指定时间") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值