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