Wednesday, September 26, 2012

ubuntu 10.04 upstart use other user instead of root

example
exec su -s /bin/sh -c 'exec "$0" "$@" > /tmp/easypaas.log 2>&1' ubuntu -- /home/ubuntu/cloudfoundry/vcap/dev_setup/bin/vcap_dev start

Saturday, September 1, 2012

Postfix bounces email handler by python script

#!/usr/bin/python
# -*- coding: utf-8 -*-
# export PYTHONIOENCODING=utf-8
import MySQLdb
import string
import email
from cStringIO import StringIO
from email import message_from_string
from flufl.bounce import scan_message
import re, sys, os

class DB:
conn = None

def connect(self):
self.conn = MySQLdb.connect(host='10.0.0.1', user='xx_rw', passwd='123456', db='xxx', use_unicode=True, charset="utf8")

def query(self, sql, string):
try:
cursor = self.conn.cursor()
cursor.execute(sql,string)
except (AttributeError, MySQLdb.OperationalError):
self.connect()
cursor = self.conn.cursor()
cursor.execute(sql,string)
return cursor


def main():
db = DB()
sender=sys.argv[2]
recipient=sys.argv[1]
email_input = sys.stdin.readlines()
parser = email.FeedParser.FeedParser()
msg = None
for msg_line in email_input:
msg = parser.feed(msg_line)
msg = parser.close()

#filter_recipient = scan_message(message_from_string(string.join(message, ' ')))
filter_recipient = scan_message(msg)
if (len(filter_recipient)>0):
db.query("""INSERT INTO email_bounces (`sender`, `recipient`) VALUES(%s, %s)""", (filter_recipient, recipient))

if __name__ == "__main__":
main()