Commit 87eab521 authored by Rémy Mozul's avatar Rémy Mozul

add students python file

parent 583583f7
......@@ -4,5 +4,8 @@ Exemple
Exemple de projet pour utiliser git
Ne contient pour l'instant que ce README...
Ne contient qu'un fichier *students.py*
qui doit analyser un fichier du type *gitlab_access.log*
import re
from collections import defaultdict
NTEST = 3
def readFile(input_file, debug=False):
"""
read a log file to get ip accessing the server
"""
pattern = re.compile(r"\A(?P<ip>(\d{1,3}\.?){4})")
list_ip = []
with open(input_file, 'rt', encoding="utf8") as file_in:
i_line = 0
for line in file_in:
found = pattern.match(line)
if found:
list_ip.append( found['ip'] )
# temporary to check the regex
i_line += 1
if debug and i_line >= debug:
break
return list_ip
def countIp( list_ip ):
"""
make a dictionnary of ip and number of
occurences
"""
count_ip = defaultdict(int)
for ip in list_ip:
#if ip not in count_ip.keys():
# count_ip[ip] = 0
count_ip[ip] += 1
return count_ip
#l_ip = readFile('gitlab_access.log',debug=5)
l_ip = readFile('gitlab_access.log')
count = countIp(l_ip)
print( f"number of lines : {len(l_ip)}" )
print( f"number of ip : {len(count)}" )
print( f"max number of oc. : {max(count.values())}" )
print( f"sum of occurences: {sum(count.values())}" )
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment