CyberKeeda In Social Media
Showing posts with label Python Library. Show all posts
Showing posts with label Python Library. Show all posts

Python Encode and Decode string using BASE64 module


base64 is a python library that can be used to encrypt and decrypt strings and characters, that can have a multiple use case.

One common use case is instead of directly pasting a plain text credential parameters into a file or as a parameter and that can be later decrypted using the decode statements within the program.

Within this blog post, we will cover.
  • How can we encrypt strings using base64
  • How can be decrypt the above base64 encrypted string using bas64 decoder.

Please note the two important points before we use this module.

  • base64 encode and decode functions both require a bytes-like object. In order to get our string into bytes, we must encode it first using Python's built in encode function. Most commonly, the UTF-8 encoding is used.
  • Encryption of same string using Linux command line interface and python shell differs, please use the same environment for both the encryption and decryption.

Here in this example, we will encrypt our string "cyberkeeda@123" and later we will decrypt it.


# Encryption Block 

import base64
base64.b64encode(bytes("cyberkeeda@123", "utf-8"))
Output for the above.


Below code can be use to decrypt the above, please use only the string content enclosed within string to decrypt, so for the above example output consider string leaving the b ( byte ) identifier.
# Decryption Block 

import base64

Output for the above.

Hope this small piece of snippet will help you in some context.

Read more ...

Python : Selenium and Chromedriver to take webpage screenshot and send it via email.

Browser Automation using Python

Within this post, we will cover.

  • Use of selenium and chromedriver to take screenshot of most recent webpage status.
  • Will leverage the basic preinstalled libraries to send email.
  • Will know, how we can send an image as a part of email body not as an attachment.
  • Will use the headless mode option of chromdriver, that's is the entire operation will run in background no GUI chrome events will be observed.

We will start with the use case:

  • if you have a requirement to check the current status of a webpage multiple times within a day/hour.
  • If you have multiple of webpages to look into periodically without opening any of it, check status of directly from your inbox only.
We will use a python script here and perform the below operations sequentially.
  1. Take screenshot of webpage using Python selenium library and google chrome webdriver and save it in our folder by name screenshot.png
  2. Use Python base library email and use it's multipart function to draft image as an email body.
  3. Use Python base library to send email using SMTP server.
  • Download mandatory requirements.
    • Install selenium library from PIP
C:\Users\cyberkeeda> pip install selenium
First confirm which chrome browser you have on your system, for this open Chrome Browser --> Click on 3 dots at the top right corner of chrome --> Help
--> About Chrome Chrome --> You will get google chrome version.
  • Create an new directory and place your above chrome webdriver into it.
  • Note your organizations SMTP server URL/IP.
  • Create a new Python file and paste the below script lines within it.
  • Paste and save your python script and replace the below lines with yours.
    • driverPath  : Replace it with the path you have kept your downloaded chromedriver 
    • chrome_options.add_argument('window-size=1920,1080')  : Replace it with resolution what you want for the screenshot.
      • 720p = 1280 x 720 - is usually known as HD or "HD Ready" resolution
      • 1080p = 1920 x 1080 - is usually known as FHD or "Full HD" resolution
      • 1440p = 2560 x 1440 - is commonly known as QHD or Quad HD resolution
    • driver.get(''): Replace it your URL.
    • sendFrom : Replace it your sender's email address.
    • sentTo: Replace it your recipient email address. ( You can use same email for sendFrom and sendTo)
    • smtp = smtplib.SMTP('') : Replace it your own smtp server or IP.

import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from import Options
from selenium import webdriver
from time import sleep

def grab_screenshot():

    # Path for your downloaded chromedriver.
    driverPath = '\\Users\\cyberkeeda\\BrowserAutomation\\chromedriver'

    # To activate GUI Mode Uncomment te below two lines
    # And comment the below four lines staring from chrome_options to driver

    #driver = webdriver.Chrome(executable_path=driverPath)
    #driver = webdriver.Chrome(driverPath)
    chrome_options = Options()
    driver = webdriver.Chrome(options=chrome_options, executable_path=driverPath)


def emailScreenShot():


    html_string = """ 
                    '<b>Covid-19 Cases </b> <br> 
                    Do and Don't Link : <br>
                    <img src="cid:image1"><br>Regards <br> Automation

    sendFrom = 'Covid Update <>'
    sendTo = ''
    # Create the root message and fill in the from, to, and subject headers
    msgRoot = MIMEMultipart('related')
    msgRoot['Subject'] = 'Covid-19 Cases Status'
    msgRoot['From'] = strFrom
    msgRoot['To'] = strTo

    # Encapsulate the plain and HTML versions of the message body in an
    # 'alternative' part, so message agents can decide which they want to display.
    msgAlternative = MIMEMultipart('alternative')

    msgText = MIMEText('This is the alternative plain text message.')

    # We reference the image in the IMG SRC attribute by the ID we give it below
    msgText = MIMEText(html_string, 'html')

    # This example assumes the image is in the current directory
    fp = open('screenshot.png''rb')
    msgImage = MIMEImage(

    # Define the image's ID as referenced above

    # Send the email (this example assumes SMTP authentication is required)
        smtp = smtplib.SMTP('')
        smtp.sendmail(sendFrom, sendTo, msgRoot.as_string())
        print("SMTP Error")


Hope this blog post helps you to meet your requirement, in case of any support using above script do comment.

Read more ...

Python : How to extract desired cell values from a table created by module PrettyTable


It is simple Python library designed to make it quick and easy to represent tabular data in visually appealing ASCII tables. It was inspired by the ASCII tables used in the PostgreSQL shell psql. PrettyTable allows for selection of which columns are to be printed, independent alignment of columns (left or right justified or centred) and printing of “sub-tables” by specifying a row range.

Official Document : Link

There could be many uses cases while using it, here in this blog post we will cover how we can extract our desired cell values from preetyTable.

So we will start by 
  • Importing module
  • Creating object
  • Add Feilds
  • Add Data
from prettytable import PrettyTable           # importing module

tabular_table = PrettyTable()                 # creating object
tabular_fields = ["Name""Age""Gender"]    # Define Field List

tabular_table.field_names = tabular_fields    # Add Fields to table

tabular_table.add_row(["Tokyo""33""M"])   # Add Data


|  Name  | Age | Gender |
| Tokyo  |  33 |   M    |
| Narobi |  74 |   M    |
| Denver |  21 |   F    |

Use Case 1.

  • Extract cell values for a specific field only from prettyTable
Below For loop will help to do so.

for row in tabular_table:
     print (row.get_string(fields=["Name"]))

|  Name |
| Tokyo |
|  Name  |
| Narobi |
|  Name  |
| Denver |

Use Case 2.

  • Extract cell values without header or border from prettyTable

for row in tabular_table:
     row.border = False
     row.header = False
     print (row.get_string(fields=["Name"]).strip())

Read more ...
Designed By Jackuna