日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python将数据存入数据库_Python读取NGINX日志将其存入数据库

發布時間:2024/1/23 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python将数据存入数据库_Python读取NGINX日志将其存入数据库 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

日志數據:

#/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語句

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);

總結

以上是生活随笔為你收集整理的python将数据存入数据库_Python读取NGINX日志将其存入数据库的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。