From b6c69ab907a4b33f5650eff9b5ddc93026e4ad64 Mon Sep 17 00:00:00 2001 From: serenith Date: Sun, 2 Apr 2023 18:22:16 +0100 Subject: [PATCH 1/2] allow users to pass in any port from command line --- listen.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/listen.py b/listen.py index 78800f4..8545677 100755 --- a/listen.py +++ b/listen.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # Original script written by Stuart Colville: http://muffinresearch.co.uk/archives/2010/10/15/fake-smtp-server-with-python/ """A noddy fake smtp server.""" - +import sys import smtpd import asyncore import time @@ -10,7 +10,7 @@ class FakeSMTPServer(smtpd.SMTPServer): """A Fake smtp server""" def __init__(*args, **kwargs): - print "Running fake smtp server on port 25" + print "Running fake smtp server on " + str(locals()['args'][1][0]) + ":" + str(locals()['args'][1][1]) smtpd.SMTPServer.__init__(*args, **kwargs) def process_message(*args, **kwargs): @@ -21,7 +21,14 @@ def process_message(*args, **kwargs): pass if __name__ == "__main__": - smtp_server = FakeSMTPServer(('localhost', 25), None) + port = 25 + if len(sys.argv) == 2: + try: + port = int(sys.argv[1]) + except: + print("port must be integer, unknown value("+sys.argv[1]+") provided. default port 25 will be used") + + smtp_server = FakeSMTPServer(('localhost', port), None) try: asyncore.loop() except KeyboardInterrupt: From f2fc6466fdbfaec7227d652d86dd5794eb0de8eb Mon Sep 17 00:00:00 2001 From: serenith Date: Sun, 2 Apr 2023 18:36:17 +0100 Subject: [PATCH 2/2] use absolute path to store files When an alias is created for this program, file writing would fail when the alias is called from outside this directory. --- listen.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/listen.py b/listen.py index 8545677..a2e6976 100755 --- a/listen.py +++ b/listen.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # Original script written by Stuart Colville: http://muffinresearch.co.uk/archives/2010/10/15/fake-smtp-server-with-python/ """A noddy fake smtp server.""" +import os import sys import smtpd import asyncore @@ -10,12 +11,13 @@ class FakeSMTPServer(smtpd.SMTPServer): """A Fake smtp server""" def __init__(*args, **kwargs): - print "Running fake smtp server on " + str(locals()['args'][1][0]) + ":" + str(locals()['args'][1][1]) + print "Running fake smtp server on smtp://" + str(locals()['args'][1][0]) + ":" + str(locals()['args'][1][1]) smtpd.SMTPServer.__init__(*args, **kwargs) def process_message(*args, **kwargs): - mail = open("mails/"+str(time.time())+".eml", "w") - print "New mail from " + args[2] + fileLoc = os.path.dirname(__file__)+"/mails/"+str(time.time())+".eml" + mail = open(fileLoc, "w") + print "New mail from " + args[2] + " => " + fileLoc mail.write(args[4]) mail.close pass