add checking the connection

This commit is contained in:
pavel.muhortov 2021-12-19 17:51:07 +03:00
parent 849a8c72d3
commit 062374fe7b

View File

@ -7,7 +7,8 @@ from email.mime.base import MIMEBase
from email import encoders
from os import path
from smtplib import SMTP
from socket import error
from socket import error, create_connection, close
from time import sleep
class Mail:
@ -18,7 +19,7 @@ class Mail:
smtp_host: str = 'smtp.gmail.com', smtp_port: str = '587', smtp_stls: bool = True,
mail_from: str = None, mail_subj: str = 'no subject',
mail_text: str = 'no text', mail_type: str = 'plain',
mail_file: str = None):
mail_file: str = None) -> None:
"""
Object constructor
:param smtp_user: smtp valid user
@ -65,7 +66,7 @@ class Mail:
content.add_header("Content-Disposition", f"attachment; filename= {path.basename(file)}")
self._mailData.attach(content)
def send(self):
def send(self) -> str:
"""
Sending the generated email to the destination list
:return: delivery report string
@ -88,7 +89,7 @@ class Mail:
return report
@classmethod
def _getText(cls, string: str):
def _getText(cls, string: str) -> str:
"""
If text is a file - read text from this file
:param string: string of text or file path
@ -100,7 +101,7 @@ class Mail:
return string
def str2bool(value: str):
def str2bool(value: str) -> bool:
"""
Converts a string value to boolean
:param value: string containing "true" or "false", "yes" or "no", "1" or "0"
@ -109,6 +110,23 @@ def str2bool(value: str):
return str(value).lower() in ("true", "yes", "1")
def isConnected(sock_host: str, sock_port: str) -> bool:
"""
Checking the connection with the host.
:param sock_host: hostname or ip address
:param sock_port: port number
:return: bool True or False
"""
try:
sock = create_connection((sock_host, int(sock_port)))
if sock is not None:
sock.close
return True
except OSError:
pass
return False
if __name__ == "__main__":
from argparse import ArgumentParser
@ -139,11 +157,20 @@ if __name__ == "__main__":
help='mail body type: plain, html (default: plain)')
args.add_argument('--file', type=str, default=None, required=False,
help='mail attachment files (default: None)')
args.add_argument('--time', type=str, default='3', required=False,
help='minutes of attempts to send (default: 3)')
args = vars(args.parse_args())
print(Mail(smtp_user=args['user'], smtp_pass=args['pass'], mail_dest=args['dest'],
smtp_host=args['smtp'], smtp_port=args['port'], smtp_stls=args['stls'],
mail_from=args['from'], mail_subj=args['subj'], mail_text=args['text'],
mail_type=args['type'], mail_file=args['file']
).send()
)
attempts_pass, attempts_wait = 0, 60
while attempts_pass <= int(args['time']):
if not isConnected(sock_host=args['smtp'], sock_port=args['port']):
attempts_pass = attempts_pass + 1
sleep(attempts_wait)
else:
print(Mail(smtp_user=args['user'], smtp_pass=args['pass'], mail_dest=args['dest'],
smtp_host=args['smtp'], smtp_port=args['port'], smtp_stls=args['stls'],
mail_from=args['from'], mail_subj=args['subj'], mail_text=args['text'],
mail_type=args['type'], mail_file=args['file']
).send()
)
break