ajout code

This commit is contained in:
Your Name 2022-09-29 09:54:43 +00:00
parent b0db300979
commit 76ae24a4f1

25
sio2/sisr2/30-Python/regex.py Executable file

@ -0,0 +1,25 @@
#!/usr/bin/python3
import sys
import re
volip={}
hostip={}
regexp = "^(\S+) (\S+) (\S+) \[([^]]+)\] \"(\w+) (\S+).*\" (\d+) (\S+)"
for line in sys.stdin: # on lit sur lentrée standard
line = line.rstrip () # on enleve le retour ligne
res = re.match (regexp, line)
if res:
(host, rfc931, user, date, request, url, status, byte) = res.groups()
host = res.group (1)
byte = int(res.group (8))
print ('host : ', host, ' byte : ', byte)
#calcul nb ip apparitions
if host not in hostip:
hostip[host] = 1
volip[host] = byte
else:
hostip[host] = hostip[host]+1
volip[host] = volip[host]+byte
for addr in sorted(hostip.keys()):
print(addr, " : ", hostip[addr])