Import and export csv for converting existing greylist database.
This commit is contained in:
@@ -41,6 +41,15 @@ class Greylist(object):
|
|||||||
self.dbp = shelve.open(dbname,'c',protocol=2)
|
self.dbp = shelve.open(dbname,'c',protocol=2)
|
||||||
self.lock = thread.allocate_lock()
|
self.lock = thread.allocate_lock()
|
||||||
|
|
||||||
|
def export_csv(self,fp):
|
||||||
|
"Export records to csv."
|
||||||
|
import csv
|
||||||
|
dbp = self.dbp
|
||||||
|
w = csv.writer(fp)
|
||||||
|
for key, r in dbp.iteritems():
|
||||||
|
ip,sender,recipient = key.split(':')
|
||||||
|
w.writerow([ip,sender,recipient,r.firstseen,r.lastseen,r.cnt,r.umis])
|
||||||
|
|
||||||
def clean(self,timeinc=0):
|
def clean(self,timeinc=0):
|
||||||
"Delete records past the retention limit."
|
"Delete records past the retention limit."
|
||||||
now = time.time() + timeinc
|
now = time.time() + timeinc
|
||||||
@@ -100,3 +109,10 @@ class Greylist(object):
|
|||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
self.dbp.close()
|
self.dbp.close()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import sys
|
||||||
|
g = Greylist(sys.argv[1])
|
||||||
|
try:
|
||||||
|
g.export_csv(sys.stdout)
|
||||||
|
finally: g.close()
|
||||||
|
|||||||
@@ -25,6 +25,19 @@ class Greylist(object):
|
|||||||
primary key (ip,sender,recipient))''')
|
primary key (ip,sender,recipient))''')
|
||||||
except: pass
|
except: pass
|
||||||
|
|
||||||
|
def import_csv(self,fp):
|
||||||
|
import csv
|
||||||
|
rdr = csv.reader(fp)
|
||||||
|
cur = self.conn.execute('begin immediate')
|
||||||
|
try:
|
||||||
|
for r in rdr:
|
||||||
|
cur.execute('''insert into
|
||||||
|
greylist(ip,sender,recipient,firstseen,lastseen,cnt,umis)
|
||||||
|
values(?,?,?,?,?,?,?)''', r)
|
||||||
|
self.conn.commit()
|
||||||
|
finally:
|
||||||
|
cur.close();
|
||||||
|
|
||||||
def clean(self,timeinc=0):
|
def clean(self,timeinc=0):
|
||||||
"Delete records past the retention limit."
|
"Delete records past the retention limit."
|
||||||
now = time.time() + timeinc - self.greylist_retain
|
now = time.time() + timeinc - self.greylist_retain
|
||||||
@@ -84,3 +97,10 @@ class Greylist(object):
|
|||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
self.conn.close()
|
self.conn.close()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import sys
|
||||||
|
g = Greylist(sys.argv[1])
|
||||||
|
try:
|
||||||
|
g.import_csv(sys.stdin)
|
||||||
|
finally: g.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user