The apache2 log files in Ubuntu are located in:
/var/log/apache2
Code:
#Coding=utf-8ImportSYS" "data 127.0.0.1--[10/jan/2017:10:08:16 +0800] "post/cgi-bin/login.py http/1.1" 335 "-" curl/7.35.0 "" "defDictify_logline (line): Split_line=Line.split ()return { "remote_address": split_line[0],"Status": split_line[8], "bytes_sent": split_line[9] }defGenerate_log_report (logfile): Report_dict= {} forLineinchlogfile:line_dict=Dictify_logline (line)Printline_dictTry: Bytes_send= Int (line_dict["bytes_sent"]) exceptValueError:Continue #Count each IP, corresponding to the number of bytes sentReport_dict.setdefault (line_dict["remote_address"], []). Append (bytes_send)returnreport_dictif __name__=="__main__": if notLen (SYS.ARGV) > 1: Sys.exit (1) Infile_name= Sys.argv[1] Try: infile= Open (Infile_name,'R') exceptIOError:Print 'You must specify a valid file to parse'Sys.exit (1) Log_report=Generate_log_report (infile)Printlog_report infile.close ()#Regular Expressions Extract dataImportRelog_line_re= Re.compile (" "(? p<remote_address>\s+) #IP address\s+ #whitespace \s+ #remote logname\s+ #whitepsace \s+ #remote user\s+ #whitespa Ce\[[^\[\]]+\] #time \s+ #whitespace "[^"]+ "#first line of request\s+ #whitesapce (? p<status>\d+) \s+ #whitespace (? p<bytes_sent>-|\d+) \s*" ", Re. VERBOSE)defDictify_logline (line): M=Log_line_re.match (line)ifm:groupdict=m.groupdict ()ifgroupdict['bytes_sent'] =='-': groupdict['bytes_sent'] ='0' returngroupdictElse: return { "remote_address": None,"Status": None,"bytes_sent":"0" }
Effect:
Read the Apache access log to see the number of bytes received per independent client connection