日志数据:
代码语言:javascript复制#/usr/bin/env python
#-*-coding:UTF-8 -*-
from datetime import datetime
stat_days = []
import pymysql
#print(datetime.now().strftime("%Y-%m-%d %H:%M:%S")) #2018-05-25 22:23:44
#print(datetime.now().strftime("%d/%b-%Y %H:%M:%S")) #25/May-2018 22:23:44格式
#print(datetime.strptime('17/Jun/2017:12:11:16',"%d/%b/%Y:%H:%M:%S")) #格式转换
connect=pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='',
database='nginxlog',
charset="utf8"
)
cur = connect.cursor()
sql= "insert into nginxlog(ip,time,methods,source,protocol,status) values(%s,%s,%s,%s,%s,%s)"
with open("portal_ssl.access.log","r") as ngfile:
for line in ngfile:
_nodes = line.split()
IP= _nodes[0]
Time= _nodes[3][1:-1].replace(":"," ",1) #将时间转换为17/Jun/2017 12:43:4格式
Time = datetime.strptime(Time,"%d/%b/%Y %H:%M:%S")#将时间格式化为2017-06-17 12:43:04
Methods = _nodes[5][1:]
Source = _nodes[6]
Protocol = _nodes[7][:-1]
Status = _nodes[8]
print(IP,Time,Methods,Source,Protocol,Status)
cur.execute(sql,(IP,Time,Methods,Source,Protocol,Status))
connect.commit()
connect.close()
SQL语句
代码语言:javascript复制create database if not exists nginxlog default charset utf8;
create table nginxlog (
id int(10) primary key auto_increment,
ip varchar(30) not null default '',
time datetime,
methods varchar(10) not null default '',
source varchar(200) not null default '',
protocol varchar(10) not null default '',
status varchar(10) not null default ''
)
alter table nginxlog modify column source varchar(400);