[Py-MAD] Mi primer programa en python
Oldzhay Saroolu
oldzhay.saroolu at gmail.com
Fri Feb 22 14:03:35 CET 2013
Hola Pitonistas de MadriZ :-)
Llevo un buen rato leyendo sobre Python, viendo presentaciones y yendo a
las quedadas y al final dije que era hora de empezar a escribir y no solo a
leer.
Comparto con vosotros mi primer programa. Lo que hace es leer el log de
EXIM, filtrar los rebotes y enchufarlos en una BBDD:
#!/usr/bin/env python
>
> import re # regex
> import MySQLdb # mysql
>
> DEBUG=1 # if 1 prints full sql statement
>
> # Logfile full path
> LOGF = r'/var/log/exim4/mainlog.1'
>
> # connect
> db = MySQLdb.connect(host="1.2.3.4", user="bounces", passwd="pass",db="db")
> cursor = db.cursor()
>
> with open('/var/log/exim4/mainlog.1','r') as mainlog:
> for line in mainlog: #reading the log
> line by line
> if re.search (" \*\* ", line): # searching for
> matching string **, that in the exim log language means bounced email
> date = line.split(' ')[0] # we need the date
> of the bounced email
> email = line.split(' ')[4] # ... and the
> email (obviously :-) )
> domain = email.split('@')[-1] # just another
> column in the db for grouping and statistics
> # Execute insert
> cursor.execute("INSERT INTO db.dbtable.bounces
> (email, date, domain, processed) values ('%s','%s','%s',0);" % (email,
> date, domain) )
> db.commit()
> if DEBUG == 1:
> print (("INSERT INTO db.dbtable (email,
> date, domain, processed) values ('%s','%s','%s',0);" % (email, date,
> domain) ))
>
>
>
> db.close()
>
Igual vosotros hacéis esto en 4 minutos, pero yo tarde 4 horas :-) así que
ha sido un buen curro... y si, se aceptan criticas :-) (por favor)
Salud
--
Best Regards
Oldzhay Saroolu
tel: + (34) 6 444 7 333 1
in : oldzhaysaroolu <http://es.linkedin.com/in/oldzhaysaroolu>
This communication contains information which is confidential and may also
be privileged. It is for the exclusive use of the intended recipient(s).****
If you are not the intended recipient(s), please note that any
distribution, copying or use of this communication or the information in it
is strictly prohibited.****
If you have received this communication in error, please notify the sender
immediately and then destroy any copies of it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.es.python.org/pipermail/madrid/attachments/20130222/b7b54d1e/attachment.html>
More information about the Madrid
mailing list