Python

Additional Python Content

Additional Content related to python are collected in this extra section .

 

Read From .env file

# pip install python-dotenv
import os
import dotenv
dotenv.load_dotenv()

API_KEY = os.getenv("API_KEY")

 

 

IP Address Using Python

# ip address using python 
import urllib.request as urllib2
import json
ip=input("Enter the ip address: ")
url="http://ip-api.com/json/"
response=urllib2.urlopen(url+ip)
data=response.read()
valuse=json.loads(data)
'''
import requests

ip = input("Enter the IP address: ")
url = f"https://ipinfo.io/{ip}/json"

response = requests.get(url)
data = response.json()

'''

 

Email Send Using Sendgrid


# pip install sendgrid
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def send(
    sendgrid_api_key:str,
    from_email:str,
    receiver: str,
    message: str,
    subject: str,
):
    '''
    from_email: Any | None = None,
    to_emails: Any | None = None,
    subject: Any | None = None,
    plain_text_content: Any | None = None,
    html_content: Any | None = None,
    amp_html_content: Any | None = None,
    global_substitutions: Any | None = None,
    is_multiple: bool = False

    Parameters
        from_email : From, tuple, optional
        The email address of the sender

        subject : Subject, optional
        The subject of the email

        to_emails : To, str, tuple, list(str), list(tuple), list(To), optional
        The email address of the recipient

        plain_text_content : string, optional
        The plain text body of the email

        html_content : string, optional
        The html body of the email

        amp_html_content : string, optional
        The amp-html body of the email
    
    '''
    mail = Mail(
        from_email=from_email,
        to_emails=receiver,
        subject=subject,
        plain_text_content=message,
    )

    try:
        sg = SendGridAPIClient(sendgrid_api_key)
        response = sg.send(mail)
        logger.info(response.status_code)
    except Exception as e:
        logger.exception(str(e))

Sendgrid also provide Dynamic Template with dynamic value render, which can be achieved by following code below.

def dynamic_email(
    sender_email: str,
    sendgrid_api_key: str,
    email: str,
    subject: str,
):
    message = Mail(
        from_email=sender_email,
        to_emails=email,
        subject=subject,
    )

    message.template_id = "template_id"
    message.dynamic_template_data = {
        "name": "User",
    }
    try:
        sg = SendGridAPIClient(sendgrid_api_key)
        response = sg.send(message)
        print(response.status_code)
    except Exception as e:
        print(str(e))

For more info: https://pypi.org/project/sendgrid/

 

 

 

update the pip

pip install --upgrade pip setuptools wheel

 


About author

author image

Amrit panta

Fullstack developer, content creator



Scroll to Top