web/lib/django_extensions/management/commands/mail_debug.py
changeset 3 526ebd3988b0
equal deleted inserted replaced
1:ebaad720f88b 3:526ebd3988b0
       
     1 from django.core.management.base import BaseCommand
       
     2 import sys
       
     3 import smtpd
       
     4 import asyncore
       
     5 
       
     6 class Command(BaseCommand):
       
     7     help = "Starts a test mail server for development."
       
     8     args = '[optional port number or ippaddr:port]'
       
     9 
       
    10     requires_model_validation = False
       
    11 
       
    12     def handle(self, addrport='', *args, **options):
       
    13         if args:
       
    14             raise CommandError('Usage is runserver %s' % self.args)
       
    15         if not addrport:
       
    16             addr = ''
       
    17             port = '1025'
       
    18         else:
       
    19             try:
       
    20                 addr, port = addrport.split(':')
       
    21             except ValueError:
       
    22                 addr, port = '', addrport
       
    23         if not addr:
       
    24             addr = '127.0.0.1'
       
    25 
       
    26         if not port.isdigit():
       
    27             raise CommandError("%r is not a valid port number." % port)
       
    28         else:
       
    29             port = int(port)
       
    30 
       
    31         quit_command = (sys.platform == 'win32') and 'CTRL-BREAK' or 'CONTROL-C'
       
    32 
       
    33         def inner_run():
       
    34             print "Now accepting mail at %s:%s" % (addr, port)
       
    35             server = smtpd.DebuggingServer((addr,port), None)
       
    36             asyncore.loop()
       
    37 
       
    38         try: 
       
    39             inner_run()
       
    40         except KeyboardInterrupt:
       
    41             pass