Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| development:python [2023/06/19 07:11] – created Zyzonix | development:python [2024/02/27 07:32] (current) – Zyzonix | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ===== Python ===== | + | ====== Python |
| ===== Snippet list ===== | ===== Snippet list ===== | ||
| Line 33: | Line 33: | ||
| ++++ | ++++ | ||
| </ | </ | ||
| + | |||
| + | ==== Send mail ==== | ||
| + | |||
| + | The following functions can be used to emails from python. | ||
| + | |||
| + | <panel type=" | ||
| + | ++++ Show/Hide | | ||
| + | <code bash> | ||
| + | EMAILRECEIVER = "" | ||
| + | EMAILSENDER = "" | ||
| + | MAILSERVER = "" | ||
| + | MAILSERVERPORT = "" | ||
| + | EMAILSENDER = "" | ||
| + | |||
| + | def buildMail(self): | ||
| + | mailText = "" | ||
| + | # build your mail here string by string (mailText += "" | ||
| + | return mailText | ||
| + | |||
| + | def sendMail(self): | ||
| + | if EMAILRECEIVER and EMAILSENDER and MAILSERVER and MAILSERVERPORT: | ||
| + | import smtplib | ||
| + | from email.mime.multipart import MIMEMultipart | ||
| + | from email.mime.text import MIMEText | ||
| + | from email.header import Header | ||
| + | |||
| + | smtp = smtplib.SMTP() | ||
| + | smtp.connect(MAILSERVER, | ||
| + | # define subject here | ||
| + | subject = "" | ||
| + | msgRoot = MIMEMultipart(" | ||
| + | msgRoot[' | ||
| + | msgRoot[' | ||
| + | msgRoot[' | ||
| + | mailContent = MIMEText(buildMail(self)) | ||
| + | mailText = MIMEText(mailContent, | ||
| + | msgRoot.attach(mailText) | ||
| + | try: | ||
| + | smtp.sendmail(EMAILSENDER, | ||
| + | except: | ||
| + | print(self, " | ||
| + | print(traceback.format_exc()) | ||
| + | | ||
| + | else: | ||
| + | print(self, " | ||
| + | </ | ||
| + | ++++ | ||
| + | </ | ||
| + | |||
| + | <panel type=" | ||
| + | ++++ Show/Hide | | ||
| + | <code bash> | ||
| + | # MAILCONFIG | ||
| + | MAILSERVER = "" | ||
| + | MAILSERVERPORT = 587 | ||
| + | MAILUSER = "" | ||
| + | MAILPASSWORD = "" | ||
| + | EMAILRECEIVER = "" | ||
| + | EMAILSENDER = "" | ||
| + | |||
| + | def buildMail(self): | ||
| + | mailText = "" | ||
| + | # build your mail here string by string (mailText += "" | ||
| + | return mailText | ||
| + | |||
| + | def sendMail(self): | ||
| + | if EMAILRECEIVER and MAILUSER and MAILSERVER and MAILSERVERPORT: | ||
| + | import smtplib | ||
| + | from email.mime.multipart import MIMEMultipart | ||
| + | from email.mime.text import MIMEText | ||
| + | from email.header import Header | ||
| + | |||
| + | smtp = smtplib.SMTP(MAILSERVER) | ||
| + | smtp.connect(MAILSERVER, | ||
| + | smtp.ehlo() | ||
| + | smtp.starttls() | ||
| + | smtp.ehlo() | ||
| + | smtp.login(MAILUSER, | ||
| + | # define subject here | ||
| + | subject = "" | ||
| + | msgRoot = MIMEMultipart(" | ||
| + | msgRoot[' | ||
| + | msgRoot[' | ||
| + | msgRoot[' | ||
| + | | ||
| + | | ||
| + | mailContent = buildMail(self) | ||
| + | mailText = MIMEText(mailContent, | ||
| + | | ||
| + | msgRoot.attach(mailText) | ||
| + | try: | ||
| + | smtp.sendmail(MAILUSER, | ||
| + | | ||
| + | except: | ||
| + | print(self, " | ||
| + | print(self, traceback.format_exc()) | ||
| + | | ||
| + | else: | ||
| + | print(self, " | ||
| + | </ | ||
| + | ++++ | ||
| + | </ | ||
| + | |||
| + | ==== Execute Bash command and capture output ==== | ||
| + | //The output will be captured when the command exited.// | ||
| + | <panel type=" | ||
| + | ++++ Show/Hide | | ||
| + | <code bash> | ||
| + | resultEncoded = subprocess.run("/ | ||
| + | result = resultEncoded.stdout.decode()[: | ||
| + | resultErr = resultEncoded.stderr.decode()[: | ||
| + | </ | ||
| + | ++++ | ||
| + | </ | ||
| + | |||
| + | //Sourced from [[https:// | ||
| + | |||
| + | ==== Get current Memory (RAM) information/ | ||
| + | //Using '' | ||
| + | <panel type=" | ||
| + | ++++ Show/Hide | | ||
| + | <code bash> | ||
| + | ++++ | ||
| + | </ | ||
| + | |||
| + | ==== FastAPI/ | ||
| + | === Change Log Format === | ||
| + | Adding the following lines to your '' | ||
| + | <code bash> | ||
| + | log_config = uvicorn.config.LOGGING_CONFIG | ||
| + | log_config[" | ||
| + | uvicorn.run(app, | ||
| + | </ | ||
| + | |||
| + | //Sourced from [[https:// | ||