반응형
pymysql 을 이용한 mysql db로그 삽입하기.
import pymysql
conn = pymysql.connect(host='localhost', user='hello_mysql', password='1', db='logs', charset='utf8')
curs = conn.cursor()
sql = "insert into access_log(ip, access_time, method, uri, status)\
values (%s,%s, %s, %s,%s)"
logfile_path = '/경로/'
logfile_fullpath = logfile_path + '파일이름'
count = 0
with open(logfile_fullpath, 'r') as f:
for line in f:
count += 1
log_parse = line.split(' ')
ip = log_parse[1]
time = log_parse[4] + log_parse[5]
method = log_parse[6]
uri = log_parse[7]
status = log_parse[9]
status_s = int(status)
if status_s > 200:
continue
curs.execute(sql, (ip, time, method, uri, int(status)))
conn.commit()
if count > 10:
conn.close()
break
f.close()
반응형
'[ ★ ]Study > Programming' 카테고리의 다른 글
파이썬 cp949 에러 : python UnicodeEncodeError (0) | 2020.09.24 |
---|---|
Python TypeError : 파이썬 타입에러 (0) | 2020.09.18 |
파이썬 웹 크롤링(Web Crawler) (0) | 2020.08.01 |
gcc make : Nothing to be done for 'all' Error (0) | 2020.07.13 |
GetAsyncKeyState 함수와 GetKeyState 함수의 차이 (0) | 2020.03.13 |
댓글