1 Star 0 Fork 0

dzc/Python-1

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Monitor Apache 934 Bytes
一键复制 编辑 原始数据 按行查看 历史
RISHAV KUMAR 提交于 2017-08-03 22:18 +08:00 . Create Monitor Apache
Monitor Apache / Nginx Log File
Count the number of hits in a Apache/Nginx
This small script will count the number of hits in a Apache/Nginx log file.
How it works
This script can easily be adapted to any other log file.
The script starts with making an empty dictionary for storing the IP addresses andcount how many times they exist.
Then we open the file (in this example the Nginx access.log file) and read the
content line by line.
The for loop go through the file and splits the strings to get the IP address.
The len() function is used to ensure the length of IP address.
If the IP already exists , increase by 1.
ips = {}
fh = open("/var/log/nginx/access.log", "r").readlines()
for line in fh:
ip = line.split(" ")[0]
if 6 < len(ip) <=15:
ips[ip] = ips.get(ip, 0) + 1
print ips
Test it out
If you now browse to your website, and run the python script, you should see your IP address + the counts.
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sdredboy/Python-1.git
git@gitee.com:sdredboy/Python-1.git
sdredboy
Python-1
Python-1
master

搜索帮助