/fragapy/amazon/smtpreldaemon.py

https://github.com/fragaria/fragapy
Python | 59 lines | 40 code | 13 blank | 6 comment | 9 complexity | 8a5e4664482819dbe068fccf9f1b0d0f MD5 | raw file
  1. #!/usr/bin/env python
  2. '''
  3. Created on 20.10.2011
  4. @author: xaralis
  5. '''
  6. import asyncore, sys, os, ConfigParser
  7. from fragapy.unix.deamonize import Daemon
  8. from fragapy.amazon.smtp_relay import SESRelaySMTPServer
  9. class SESRelaySMTPDaemon(Daemon):
  10. def __init__(self, pidfile):
  11. config = ConfigParser.SafeConfigParser()
  12. cfg_path = os.path.expanduser('~/.smtpreldaemon.cfg')
  13. if not os.path.exists(cfg_path):
  14. sys.stderr.write(u"Missing configuration file, %s does not exist." % cfg_path)
  15. sys.stderr.flush()
  16. sys.exit(2)
  17. config.readfp(open(cfg_path))
  18. self.ip_addr = config.get('remote', 'ip_addr')
  19. self.port = config.getint('remote', 'port')
  20. self.key = config.get('credentials', 'key')
  21. self.secret = config.get('credentials', 'secret')
  22. stderr_log = None
  23. stdout_log = None
  24. if config.has_section('log'):
  25. if config.has_option('log', 'errors'):
  26. stderr_log = config.get('log', 'errors')
  27. if config.has_option('log', 'stdout'):
  28. stdout_log = config.get('log', 'stdout')
  29. if stderr_log is None:
  30. stderr_log = '/var/log/amazon/error.log'
  31. if stdout_log is None:
  32. stdout_log = '/dev/null'
  33. super(SESRelaySMTPDaemon, self).__init__(pidfile, stdout=stdout_log, stderr=stderr_log)
  34. if not self.key and not self.secret:
  35. sys.stderr.write("Missing credentials to use for AWS")
  36. sys.stderr.flush()
  37. sys.exit(2)
  38. def run(self):
  39. sys.stdout.write(u"Running SMTP relay service on %s:%s ..." % (self.ip_addr, self.port))
  40. sys.stdout.flush()
  41. SESRelaySMTPServer((self.ip_addr, self.port), None, self.key, self.secret)
  42. asyncore.loop()
  43. if __name__ == '__main__':
  44. d = SESRelaySMTPDaemon('/tmp/smtpreldaemon.pid')
  45. d.start()