Python analyzes the nginx log IP address and saves it to the SQLite Database

Source: Internet
Author: User

Don't talk about anything. Go directlyCode

 

Import sqlite3 <br/> Import OS <br/> Import string <br/> Import fileinput <br/> path_log_nginx = R "e: /xxx-logs/20100904 "<br/> conn = sqlite3.connect ('Log. db') <br/> C = Conn. cursor () <br/> export c.exe cute ('''drop table log''') <br/> c.exe cute (''' create table log (IP text) ''') <br/> for file in OS. listdir (path_log_nginx): <br/> if not file. endswith (". log "): <br/> Print" Warn: % s is not a log file "% (File) <br/> continue <br/> Print" info: process file % s "% (File) <br/> for line in fileinput. input (OS. path. join (path_log_nginx, file): <br/> If line. startswith( "192.168. "): <br/> continue; <br/> c.exe cute (" insert into log (IP) values ('% s') "% (line [0: line. find ("--[")]) <br/> Conn. commit () <br/> export c.exe cute ("select IP, count (*) C from log group by IP having C> 1000 order by c desc ") <br/> # We can also close the cursor if we are done with it <br/> C. close () <br/> # <br/> Print "done... "<br/>

 

After completion

Sqlite3.exe log. DB

> Select IP, count (*) C from log group by IP having C> 1000 order by c desc;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.