bcoe / secure-smtpd

Fork of Python's standard SMTP server. Adding support for various extensions to the protocol.
ISC License
128 stars 42 forks source link

example from README does not work as-is - syntax error #30

Open emanuil-tolev opened 8 years ago

emanuil-tolev commented 8 years ago
(fosdem-volunteers)emanuil@midori:~/software/secure-smtpd$ python --version
Python 2.7.6

(fosdem-volunteers)emanuil@midori:~/software/secure-smtpd$ pip freeze | grep smtpd
secure-smtpd==3.0.0

(fosdem-volunteers)emanuil@midori:~/software/secure-smtpd$ cat run.py 
from secure_smtpd import SMTPServer, FakeCredentialValidator
SMTPServer(
self,
('0.0.0.0', 465),
None,
require_authentication=True,
ssl=True,
certfile='examples/server.crt',
keyfile='examples/server.key',
credential_validator=FakeCredentialValidator(),
)
asyncore.loop()

(fosdem-volunteers)emanuil@midori:~/software/secure-smtpd$ python run.py                                                                                                                
Traceback (most recent call last):                                                                                                                                                      
  File "run.py", line 3, in <module>                                                                                                                                                    
    self,                                                                                                                                                                               
NameError: name 'self' is not defined
emanuil-tolev commented 8 years ago

Ah, googled around and found your blog post: http://bencoe.tumblr.com/post/12753680108/writing-a-secure-smtp-server-in-python

There is actually a problem with the code in there too - seems like the debug keyword argument was removed from SMTPServer's constructor at some point, so that also gives a syntax error. Removing the debug=true arg from your blog post code seems to run it though (will test if working now). Might be worth including the snippet below in the README and therefore in PyPI - I'll make a PR after confirming it actually works as well.

The final code I used to run it was

import asyncore
from secure_smtpd import SMTPServer, FakeCredentialValidator

class SSLSMTPServer(SMTPServer):

    def __init__(self):
        pass

    def process_message(self, peer, mailfrom, rcpttos, message_data):
        print message_data

    def start(self):
        SMTPServer.__init__(
            self,
            ('0.0.0.0', 25465),  # note if you want to run it on the default port 465 for secure SSL SMTP you will need sudo rights as all ports below 1024 are privileged
            None,
            require_authentication=True,
            ssl=True,
            certfile='examples/server.crt',
            keyfile='examples/server.key',
            credential_validator=FakeCredentialValidator(),
        )
        asyncore.loop()

server = SSLSMTPServer()
server.start()
emanuil-tolev commented 8 years ago

When an app tries to initiate a connection to that port, I just get

_accept_subprocess(): smtp connection accepted within subprocess.

and it dies immediately, no leftover python processes running secure-smtpd. Tried setting maximum_execution_time to 600 as well, but still did it (besides, I tried within 30 seconds of running it anyway). I'm looking to just dump the emails to stdout - basically I'd like to have a local secure SMTP running as the app expects one to be configured or it errors out.

Anyway, if there is something else I should modify in the run.py example to get better results, let me know - otherwise I'll wait a bit and submit a PR to update the README so we have a runnable copy/pastable example :).

bcoe commented 8 years ago

@emanuil-tolev I would love your update to the README. I unfortunately am not writing too much Python anymore, and I've been relying on the community to keep my old Python projects is a healthy state -- greatly appreciate your debugging.

thijstriemstra commented 7 years ago

Probably a different issue, but all I see is:

  File "/Users/foo/bar/src/secure-smtpd/secure_smtpd/smtp_channel.py", line 9, in <module>
    from smtpd import NEWLINE, EMPTYSTRING
ImportError: cannot import name 'EMPTYSTRING'