def recv_mail(self):
print 'POP3: recv_mail'
pop = None
try:
pop = poplib.POP3_SSL(POP3_MAIL_IP, POP3_PORT)
except (poplib.error_proto, socket.error) as e:
print 'POP3: ERROR: %s : %s'%(POP3_MAIL_IP,e)
time.sleep(20)
if pop == None:
return
try:
pop.user(POP3_ADDR)
pop.pass_(POP3_PASS)
except poplib.error_proto, e:
print 'POP3: ERROR: %s : %s'%(POP3_ADDR, e)
time.sleep(20)
if pop.stat()[0] == 0:
print 'POP3: no messages'
pop.quit()
time.sleep(1) #long sleep
return
for i in xrange( 1,len(pop.list()[1])+1 ):
print 'NUM: %d'%i
status, lines, octets = pop.retr(i)
msg = email.message_from_string('\n'.join(lines))
#->> P2: validate_subject
if self.isCurrentMsg( msg ) == 0:
self.deleteMsg(pop, i)
#pop.quit()
continue
ip = self.getAgentIP( msg.get_all('Received')[2] )
timestamp = msg.get('Date')
for part in msg.walk():
if part.get_content_type():
if part.get_content_type() == "application/octet-stream":
body = part.get_payload(decode=True)
if body is not None:
agent_id = struct.unpack('<I', body[:4])[0]
data = self.P3Scheme.pack_agent_data(body)
#timestamp = datetime.utcnow().isoformat()
print 'POP3: Datas: %s'%body
meta_data = self.P3Scheme.pack_data(self.P3Scheme.separator.join([ip, timestamp]))
self.LocalStorage.save_data_from_agent(agent_id, meta_data + self.P3Scheme.separator + data)
self.Logger.log_message("POP3: Data saved!")
else:
print 'POP3: empty message'
self.deleteMsg(pop, i)
continue
if self.deleteMsg(pop, i) == 0:
print 'POP3: ERROR: Msg is\'t deleted'
pop.guit()
exit(1)
time.sleep(0.1)
break
pop.quit()
#get data from fs, send to MailServer
评论列表
文章目录