ubuntu中apache2的日志文件位于:

/var/log/apache2

代码:

# coding=utf-8
import sys '''
数据
127.0.0.1 - - [10/Jan/2017:10:08:16 +0800] "POST /cgi-bin/login.py HTTP/1.1" 200 335 "-" "curl/7.35.0"
'''
def dictify_logline(line):
split_line = line.split()
return {
"remote_address": split_line[0],
"status": split_line[8],
"bytes_sent": split_line[9]
} def generate_log_report(logfile):
report_dict = {}
for line in logfile:
line_dict = dictify_logline(line)
print line_dict
try:
bytes_send = int(line_dict["bytes_sent"])
except ValueError:
continue
#统计每一个ip,对应发送的字节数
report_dict.setdefault(
line_dict["remote_address"],
[]).append(bytes_send)
return report_dict if __name__ == "__main__":
if not len(sys.argv) > 1:
sys.exit(1)
infile_name = sys.argv[1]
try:
infile = open(infile_name, 'r')
except IOError:
print 'You must specify a valid file to parse'
sys.exit(1) log_report = generate_log_report(infile)
print log_report
infile.close() #正则表达式提取数据
import re
log_line_re = re.compile('''(?P<remote_address>\S+) #IP ADDRESS
\s+ #whitespace
\S+ #remote logname
\s+ #whitepsace
\S+ #remote user
\s+ #whitespace
\[[^\[\]]+\] #time
\s+ #whitespace
"[^"]+" #first line of request
\s+ #whitesapce
(?P<status>\d+)
\s+ #whitespace
(?P<bytes_sent>-|\d+)
\s*
''', re.VERBOSE) def dictify_logline(line):
m = log_line_re.match(line)
if m:
groupdict = m.groupdict()
if groupdict['bytes_sent'] == '-':
groupdict['bytes_sent'] = ''
return groupdict
else:
return {
"remote_address": None,
"status": None,
"bytes_sent": ""
}

效果:

读取Apache访问日志,查看每一个独立客户端连接获得的字节数的更多相关文章

  1. Apache用户认证、域名跳转、Apache访问日志

    5月29日任务 课程内容: 11.18 Apache用户认证11.19/11.20 域名跳转11.21 Apache访问日志扩展 apache虚拟主机开启php的短标签 http://ask.apel ...

  2. Linux centos7 VMware Apache访问日志不记录静态文件、访问日志切割、静态元素过期时间

    一.Apache访问日志不记录静态文件 网站大多元素为静态文件,如图片.css.js等,这些元素可以不用记录 vim /usr/local/apache2.4/conf/extra/httpd-vho ...

  3. Linux CentOS7 VMware LAMP架构Apache用户认证、域名跳转、Apache访问日志

    一.Apache用户认证 vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //把111.com那个虚拟主机编辑成如下内容 <Virtu ...

  4. apache用户认证、域名跳转、Apache访问日志(两种格式)

    1.apache 设置,用户访问时 目录或文件的认证: 对目录的认证: <Directory /var/www/222> //指定认证的目录AllowOverride AuthConfig ...

  5. Python基础(三):简化除法判断、分析apache访问日志、扫描存活主机、利用多线程实现ssh并发访问

    一.简化除法判断 目标: 编写mydiv.py脚本,主要要求如下: 提示用户输入一个数字作为除数 如果用户按下Ctrl+C或Ctrl+D则退出程序 如果用户输入非数字字符,提示用户应该输入数字 如果用 ...

  6. Filebeat+Logstash+ElasticSearch+Kibana搭建Apache访问日志解析平台

    对于ELK还不太熟悉的同学可以参考我前面的两篇文章ElasticSearch + Logstash + Kibana 搭建笔记.Log stash学习笔记(一),本文搭建了一套专门访问Apache的访 ...

  7. apache 访问日志access_log 配置和解析 rotatelogs分割日志

    一.解析访问日志        apache 的访问日志记载着大量的信息,学会高效快捷的读出其中关键信息对我们的工作有极大帮助.       如果Apache的安装方式是默认安装,服务器一运行就会有两 ...

  8. 移动apache访问日志后如何立即生效

    一次偶然测试发现移动了apache下的access_log日志后或者修改了access_log的名称,该移动的日志或修改名称的日志文件仍记录了apache访问信息,即没有实时生效,后来查了资料才知道: ...

  9. 自制模仿apache访问日志文件格式的php日志类

    <?php // 访问日志写入类 @author 王伟 2011.12.14class Log{        //项目跟路径    private $root_path;        //日 ...

随机推荐

  1. Nodejs之mssql模块的封装

    在nodejs中,mssql模块支持sqlserver数据库操作.今天将mssql模块的某些功能封装为一个类,方便以后调用.封装的功能有执行存储过程,执行查询语句操作等.如果本篇文章对大家有帮助,那就 ...

  2. 不挣扎了,MVC验证错误信息汇总是酱紫的

    public static string GetModelErros(this ModelStateDictionary dic) { var sb = new StringBuilder(); va ...

  3. python3.6 django2.06 使用QQ邮箱发送邮件

    开通QQ邮箱IMAP/SMTP服务,忘记了,重新开通一下,记住密码串. import smtplib from email.mime.text import MIMEText # 收件人列表 mail ...

  4. C语言基础:分支语句和常见运算符 分类: iOS学习 c语言基础 2015-06-10 21:44 13人阅读 评论(0) 收藏

    if(判断条件){ 执行语句; }else if(判断条件){ 执行语句; } switch (整型表达式){  case 值1: 执行语句; break; case 值2: 执行语句; break; ...

  5. SSH 获取GET/POST参数

    在做项目的API通知接口的时候,发现在SSH框架中无法获取到对方服务器发来的异步通知信息.最后排查到的原因可能是struts2对HttpServletRequest进行了二次处理,那么该如何拿到pos ...

  6. PHP连接MYSQL 报错"No such file or directory"

    首先确定是mysql_connect()和mysql_pconnect()的问题,故障现象就是函数返回空,而mysql_error()返回“No such file or directory”. 写个 ...

  7. OK335xS U-boot GPIO control hacking

    /**************************************************************************************** * OK335xS ...

  8. 在windows下制作mac os x的启动安装U盘

    前几天有幸用了下Macbook pro,可在给它装win 7系统时,无知而又手贱地在windows系统下分区了:( 然后再重启就找不到Mac os x,只有win 7了.可进win 7也不正常,直接给 ...

  9. jQuery事件绑定汇总(包括一些无法获取事件的问题)

    ★ $(document).on('click', 'button[name=closeLayerOut2]', function () { ...... }); $(document).on('cl ...

  10. 【MVC】View与Control之间数据传递

    1. Controller向View传递数据 使用ViewData传递数据[弱类型,字典型ViewDataDictionary] ViewData[“Message_ViewData”] = “ He ...