File: mail_debug.py

package info (click to toggle)
python-django-extensions 0.4.2pre%2Bgit201004211325-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 768 kB
  • ctags: 739
  • sloc: python: 4,197; makefile: 76
file content (41 lines) | stat: -rw-r--r-- 1,183 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from django.core.management.base import BaseCommand
import sys
import smtpd
import asyncore

class Command(BaseCommand):
    help = "Starts a test mail server for development."
    args = '[optional port number or ippaddr:port]'

    requires_model_validation = False

    def handle(self, addrport='', *args, **options):
        if args:
            raise CommandError('Usage is runserver %s' % self.args)
        if not addrport:
            addr = ''
            port = '1025'
        else:
            try:
                addr, port = addrport.split(':')
            except ValueError:
                addr, port = '', addrport
        if not addr:
            addr = '127.0.0.1'

        if not port.isdigit():
            raise CommandError("%r is not a valid port number." % port)
        else:
            port = int(port)

        quit_command = (sys.platform == 'win32') and 'CTRL-BREAK' or 'CONTROL-C'

        def inner_run():
            print "Now accepting mail at %s:%s" % (addr, port)
            server = smtpd.DebuggingServer((addr,port), None)
            asyncore.loop()

        try: 
            inner_run()
        except KeyboardInterrupt:
            pass