1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/tests/integration/targets/mail/files/smtpserver.py
Felix Fontein e4eead189b
Fix linting errors; fix some real bugs (#5111) (#5115)
* Fix linting errors.

* Fix bugs.

* Another linter error ignored.

* More fixes.

* Ignore sanity errors with older versions.

ci_complete

* Forgot to commit more changes.

(cherry picked from commit a54af8909c)
2022-08-12 14:37:34 +02:00

68 lines
2 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright: (c) 2018, Dag Wieers (@dagwieers) <dag@wieers.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import asyncore
import os.path
import ssl
import sys
# Handle TLS and non-TLS support
try:
import smtpd_tls
HAS_TLS = True
except ImportError:
import smtpd
HAS_TLS = False
print('Library smtpd-tls is missing or not supported, hence starttls is NOT supported.')
# Handle custom ports
port = '25:465'
if len(sys.argv) > 1:
port = sys.argv[1]
ports = port.split(':')
if len(ports) > 1:
port1, port2 = int(ports[0]), int(ports[1])
else:
port1, port2 = int(port), None
# Handle custom certificate
basename = os.path.splitext(sys.argv[0])[0]
certfile = basename + '.crt'
if len(sys.argv) > 2:
certfile = sys.argv[2]
# Handle custom key
keyfile = basename + '.key'
if len(sys.argv) > 3:
keyfile = sys.argv[3]
try:
ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
except AttributeError:
ssl_ctx = None
if HAS_TLS:
print('Python ssl library does not support SSLContext, hence starttls and TLS are not supported.')
import smtpd
if HAS_TLS and ssl_ctx is not None:
print('Using %s and %s' % (certfile, keyfile))
ssl_ctx.load_cert_chain(certfile=certfile, keyfile=keyfile)
print('Start SMTP server on port', port1)
smtp_server1 = smtpd_tls.DebuggingServer(('127.0.0.1', port1), None, ssl_ctx=ssl_ctx, starttls=True)
if port2:
print('Start TLS SMTP server on port', port2)
smtp_server2 = smtpd_tls.DebuggingServer(('127.0.0.1', port2), None, ssl_ctx=ssl_ctx, starttls=False)
else:
print('Start SMTP server on port', port1)
smtp_server1 = smtpd.DebuggingServer(('127.0.0.1', port1), None) # pylint: disable=used-before-assignment
if port2:
print('WARNING: TLS is NOT supported on this system, not listening on port %s.' % port2)
asyncore.loop()