Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
development:python [2023/07/31 17:58] Zyzonixdevelopment:python [2024/02/27 07:32] (current) Zyzonix
Line 1: Line 1:
-===== Python =====+====== Python ======
  
 ===== Snippet list ===== ===== Snippet list =====
Line 52: Line 52:
         return mailText         return mailText
  
-    def sendMail(self, hosts, awakeList, failedList, statusUnknownList):+    def sendMail(self):
         if EMAILRECEIVER and EMAILSENDER and MAILSERVER and MAILSERVERPORT:         if EMAILRECEIVER and EMAILSENDER and MAILSERVER and MAILSERVERPORT:
             import smtplib             import smtplib
Line 65: Line 65:
             msgRoot = MIMEMultipart("alternative")             msgRoot = MIMEMultipart("alternative")
             msgRoot['Subject'] = Header(subject, "utf-8")             msgRoot['Subject'] = Header(subject, "utf-8")
-            msgRoot['From'] = "wolserver <" + EMAILSENDER + ">"+            msgRoot['From'] = "Python-Script <" + EMAILSENDER + ">"
             msgRoot['To'] = EMAILRECEIVER             msgRoot['To'] = EMAILRECEIVER
-            mailContent = MIMEText(wakeup.buildMail(self))+            mailContent = MIMEText(buildMail(self))
             mailText = MIMEText(mailContent, "plain", "utf-8")             mailText = MIMEText(mailContent, "plain", "utf-8")
             msgRoot.attach(mailText)             msgRoot.attach(mailText)
Line 81: Line 81:
 ++++ ++++
 </panel> </panel>
 +
 +<panel type="info" icon="glyphicon glyphicon-file" title="Send mails with python using AUTH">
 +++++ 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, MAILSERVERPORT)
 +            smtp.ehlo()
 +            smtp.starttls()
 +            smtp.ehlo()
 +            smtp.login(MAILUSER, MAILPASSWORD)
 +            # define subject here
 +     subject = ""
 +            msgRoot = MIMEMultipart("alternative")
 +            msgRoot['Subject'] = Header(subject, "utf-8")
 +            msgRoot['From'] = EMAILSENDER
 +            msgRoot['To'] = EMAILRECEIVER
 +            
 +            
 +            mailContent = buildMail(self)
 +            mailText = MIMEText(mailContent, "plain", "utf-8")
 +            
 +            msgRoot.attach(mailText)
 +            try:
 +                smtp.sendmail(MAILUSER, EMAILRECEIVER, msgRoot.as_string())
 +                
 +            except:
 +                print(self, "Failed to send mail to " + EMAILRECEIVER + " | Check your settings!")
 +                print(self, traceback.format_exc())
 +            
 +        else:
 +            print(self, "Mailing disabled or not configured properly.")
 +</code>
 +++++
 +</panel>
 +
 +==== Execute Bash command and capture output ====
 +//The output will be captured when the command exited.//
 +<panel type="info" icon="glyphicon glyphicon-file" title="Run subprocess">
 +++++ Show/Hide |
 +<code bash>
 +resultEncoded = subprocess.run("/command/to/execute", capture_output=True, shell=True)
 +result = resultEncoded.stdout.decode()[:-1]
 +resultErr = resultEncoded.stderr.decode()[:-1]
 +</code>
 +++++
 +</panel>
 +
 +//Sourced from [[https://stackoverflow.com/questions/39062015/sending-email-using-python-through-postfix|stackoverflow.com - Python Email]]//
 +
 +==== Get current Memory (RAM) information/statistics (Linux)====
 +//Using ''op.popen'' and Linux build-in binary ''free''.//
 +<panel type="info" icon="glyphicon glyphicon-file" title="Get memory information on Linux">
 +++++ Show/Hide |
 +<code bash>total_memory, used_memory, free_memory, shared_memory, cached_memory, available_memory = map(int, os.popen('free -t -m').readlines()[1].split()[1:])</code>
 +++++
 +</panel>
 +
 +==== FastAPI/Uvicorn ====
 +=== Change Log Format ===
 +Adding the following lines to your ''uvicorn'' initialization function will change the output of the webserver:
 +<code bash>
 +log_config = uvicorn.config.LOGGING_CONFIG
 +log_config["formatters"]["access"]["fmt"] = "%(asctime)s - %(levelname)s - %(message)s"
 +uvicorn.run(app, port=80, host=SERVERIP, log_config=log_config)
 +</code>
 +
 +//Sourced from [[https://github.com/tiangolo/fastapi/issues/1508|github.com - tiangolo/fastapi]]//
  • development/python.1690819129.txt.gz
  • Last modified: 2023/07/31 17:58
  • by Zyzonix