Get the App
SLTechnology News&Howtos  ›  Database  › 

Python analyzes the ip of the nginx log (source)

Shulou Source: shulou.com Published: 2022-06-01 14:52:02 09月27日 Update

#! / usr/bin/env python#_*_coding:utf-8 _ * _ _ author__ = 'gaogd'import datetime,threadingimport sys, os, urllib2, jsonreload (sys) sys.setdefaultencoding (' utf8') ips = {} # ip as the key of the dictionary, valueiplist = [] # iterate through the ip in the log, and the same ip will also be recorded in the list Insert database fh = open (". / ip.txt", "r"). Readlines () # I put the log and code in a directory for line in fh: ip = line.split (") [0] if 6

< len(ip) use intest;Database changedmysql>

Create table ipinfo (id int auto_increment primary key,ipaddress varchar, countip int); Query OK, 0 rows affected (0.22 sec) mysql > create table whereip (id int primary key auto_increment,country varchar, area varchar, region varchar, city varchar, ip varchar, time datetime); Query OK, 0 rows affected (0.20 sec) mysql > mysql > desc ipinfo +-+ | Field | Type | Null | Key | Default | Extra | + -+ | id | int (11) | NO | PRI | NULL | auto_increment | | ipaddress | varchar | YES | | NULL | | countip | int (11) | YES | | NULL | | +- -+-+ 3 rows in set (0.00 sec) mysql > desc whereip +-+ | Field | Type | Null | Key | Default | Extra | +- -+ | id | int (11) | NO | PRI | NULL | auto_increment | | country | varchar | YES | | NULL | | area | varchar | YES | | NULL | | region | varchar | YES | NULL | | | | city | varchar | YES | | NULL | ip | varchar | YES | | NULL | | time | datetime | YES | | NULL | | +-| -+-+ 7 rows in set (0.00 sec) mysql >

After analyzing the log, view the contents of the database:

Mysql > select * from whereip limit 10 +-- + | id | country | area | region | city | ip | time | + -+ | 1 | United States | 66.249.65.133 | 2016-09-01 10:53:01 | | 2 | USA | 23.251.63.45 | 2016-09-01 10:53:01 | 3 | United States | 23.251.63.45 | 2016-09-01 10:53:01 | 4 | China | South China | Guangdong Province | Guangzhou | 119.130.71.153 | 2016-09-01 10:53:01 | | 5 | United States | 66.249.65.183 | 2016-09-01 10:53:01 | | 6 | United States | 66.249.65.180 | 2016-09-01 10:53:01 | 7 | United States | | | 66.249.65.142 | 2016-09-01 10:53:01 | | 8 | United States | 107.151.226.203 | 2016-09-01 10:53:01 | | 9 | United States | 107.151.226.203 | 2016-09-01 10:53:01 | 10 | United States | | | 66.249.65.134 | 2016-09-01 10:53:01 | +-- +-- -- + 10 rows in set (0.00 sec) mysql >

Tags: United States data utf-8 Log Database Dictionary same Code content format Frequency Catalog China South China Guangdong Guangdong Analysis Information Telecom script Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Huawei Docker Apple OPPO Reno