Hola Pitonistas de MadriZ :-)<br>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.<br><br>Comparto con vosotros mi primer programa. Lo que hace es leer el log de EXIM, filtrar los rebotes y enchufarlos en una BBDD:<br>
<br><br><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">#!/usr/bin/env python<br><br>import re # regex<br>import MySQLdb # mysql<br><br>DEBUG=1 # if 1 prints full sql statement<br>
<br># Logfile full path<br>LOGF = r'/var/log/exim4/mainlog.1'<br><br># connect<br>db = MySQLdb.connect(host="1.2.3.4", user="bounces", passwd="pass",db="db")<br>
cursor = db.cursor()<br><br>with open('/var/log/exim4/mainlog.1','r') as mainlog:<br> for line in mainlog: #reading the log line by line<br> if re.search (" \*\* ", line): # searching for matching string **, that in the exim log language means bounced email<br>
date = line.split(' ')[0] # we need the date of the bounced email<br> email = line.split(' ')[4] # ... and the email (obviously :-) )<br> domain = email.split('@')[-1] # just another column in the db for grouping and statistics<br>
# Execute insert<br> cursor.execute("INSERT INTO db.dbtable.bounces (email, date, domain, processed) values ('%s','%s','%s',0);" % (email, date, domain) )<br>
db.commit()<br> if DEBUG == 1:<br> print (("INSERT INTO db.dbtable (email, date, domain, processed) values ('%s','%s','%s',0);" % (email, date, domain) ))<br>
<br><br><br>db.close()<br></blockquote><br><br>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)<br><br><br><br>Salud<br><br><br>
-- <br>Best Regards<br>Oldzhay Saroolu<br>tel: <a href="tel:%2B%20%2834%29%206%20444%207%20333%201" value="+34644473331" target="_blank">+ (34) 6 444 7 333 1</a><br>
<span style="background-color:rgb(51,102,255)"> <font color="#FFFFFF">in</font> </span>: <a href="http://es.linkedin.com/in/oldzhaysaroolu" target="_blank">oldzhaysaroolu</a><br>
<br><div><p style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif;margin:0px"><span style="font-size:10pt;font-family:Arial,sans-serif" lang="EN-IE">This communication contains information which is confidential and may also be privileged. It is for the exclusive use of the intended recipient(s).</span><span style="font-size:12pt;font-family:Arial,sans-serif" lang="EN-IE"><u></u><u></u></span></p>
<p style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif;margin:0px"><span style="font-size:10pt;font-family:Arial,sans-serif" lang="EN-IE">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.</span><span style="font-size:12pt;font-family:Arial,sans-serif" lang="EN-IE"><u></u><u></u></span></p>
<p style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif;margin:0px"><span style="font-size:10pt;font-family:Arial,sans-serif" lang="EN-IE">If you have received this communication in error, please notify the sender immediately and then destroy any copies of it.</span></p>
</div>