Working with Python Subprocess & Platform Module

subprocess module allows you to start new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several older modules and functions like os.system, os.spawn, os.popen

With subprocess module, one can execute system commands , capture the output and redirect the output.

#Using ls utility 

> subprocess.check_output(["ls", "-l", "/dev/null"])
 'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'

#To run above command on shell :

> subprocess.check_output("ls -l /dev/null",shell=True)
 'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'

 

check_output(*popenargs, **kwargs) – Run command with arguments and return its output as a byte string. If the exit code was non-zero it raises a CalledProcessError.

To start the new process, subprocess module has method called popen.

import subprocess

process = subprocess.Popen(['ls', '-l', '/dev/null'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
print stdout

#output
crw-rw-rw- 1 root root 1, 3 Nov 1 08:15 /dev/null

 

Other useful subprocess methods & terms :

>>> dir(subprocess)

['CREATE_NEW_CONSOLE', 'CREATE_NEW_PROCESS_GROUP', 'CalledProcessError', 'MAXFD', 'PIPE', 'Popen', 'STARTF_USESHOWWINDOW', 'STARTF_USESTDHANDLES', 'STARTUPINFO', 'STDOUT', 'STD_ERROR_HANDLE', 'STD_INPUT_HANDLE', 'STD_OUTPUT_HANDLE', 'SW_HIDE', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_active', '_args_from_interpreter_flags', '_cleanup', '_demo_posix', '_demo_windows', '_eintr_retry_call', '_subprocess', 'call', 'check_call', 'check_output', 'errno', 'gc', 'list2cmdline', 'msvcrt', 'mswindows', 'os', 'pywintypes', 'signal', 'sys', 'threading', 'traceback', 'types']

 

platform module in Python is used to access the underlying platform’s data,
such as, hardware, operating system, and interpreter version information. Some useful methods are listed below :

import platform

>>> platform.system()
'Windows'
>>> platform.processor()
'Intel64 Family 6 Model 58 Stepping 9, GenuineIntel'
>>> platform.machine()
'AMD64'
>>> platform.architecture()
('32bit', 'WindowsPE')
>>> platform.win32_ver()
('8', '6.2.9200', 'SP0', u'Multiprocessor Free')
>>> platform.uname()
('Windows', 'user', '8', '6.2.9200', 'AMD64', 'Intel64 Family 6 Model 58 Stepping 9, GenuineIntel')
>>> platform.version()
'6.2.9200'
>>> platform.python_build()
('v2.7.13:a06454b1afa1', 'Dec 17 2016 20:42:59')
>>> platform.python_compiler()
'MSC v.1500 32 bit (Intel)'
>>> platform.python_version()
'2.7.13'
>>> platform.python_implementation()
'CPython'
>>> platform.linux_distribution()
('Ubuntu', '14.04', 'trusty')
>>> platform.version()
'6.2.9200'

 

Example with code snippet is given below to explain, how platform & subprocess modules can be combined and used. Following script runs on both, Linux & Windows Environment . Script displays all current TCP/IP network configuration values.

 #!/usr/bin/python

import subprocess
import platform

def call_windows():
  print "Hello Windows"
  output = subprocess.check_output("ipconfig",shell=True)
  print output

def call_linux():
  print "Hello Linux"
  output = subprocess.check_output("ifconfig",shell=True)
  print output

if platform.system() == 'Windows':
  call_windows()
elif platform.system() == 'Linux':
  call_linux()
else:
  pass

Output on Windows System :

Hello Windows

Windows IP Configuration

Wireless LAN adapter Lenovo Easyplus Hotspot
:

Media State . . . . . . . . . . . : Media disconnected
 Connection-specific DNS Suffix . :

Wireless LAN adapter Wi-Fi:

Connection-specific DNS Suffix . : domain.name
 Link-local IPv6 Address . . . . . : fe80::9145:2c6:57de:7f97%15
 IPv4 Address. . . . . . . . . . . : 192.168.126.15
 Subnet Mask . . . . . . . . . . . : 255.255.255.0
 Default Gateway . . . . . . . . . : fe80::217:7cff:fe70:4a9a%15
 192.168.126.1

Ethernet adapter Ethernet:

Media State . . . . . . . . . . . : Media disconnected
 Connection-specific DNS Suffix . :

Tunnel adapter isatap.domain.name:

Media State . . . . . . . . . . . : Media disconnected
 Connection-specific DNS Suffix . : domain.name

Tunnel adapter Local Area Connection* 13:

Media State . . . . . . . . . . . : Media disconnected
 Connection-specific DNS Suffix . :

Tunnel adapter Teredo Tunneling Pseudo-Interface:

Media State . . . . . . . . . . . : Media disconnected
 Connection-specific DNS Suffix . :

Output on Linux System :

Hello Linux

lo       Link encap:Local Loopback
         inet addr:127.0.0.1 Mask:255.0.0.0
         UP LOOPBACK RUNNING MTU:65536 Metric:1
         RX packets:0 errors:0 dropped:0 overruns:0 frame:0
         TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:0
         RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

venet0   Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
         inet addr:127.0.0.2 P-t-P:127.0.0.2 Bcast:0.0.0.0 Mask:255.255.255.255
         UP BROADCAST POINTOPOINT RUNNING NOARP MTU:1500 Metric:1
         RX packets:2624 errors:0 dropped:0 overruns:0 frame:0
         TX packets:1803 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:0
         RX bytes:218487 (218.4 KB) TX bytes:161904 (161.9 KB)

venet0:0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
         inet addr:172.35.16.221 P-t-P:172.35.16.221 Bcast:172.35.16.221 Mask:255.255.255.255
         UP BROADCAST POINTOPOINT RUNNING NOARP MTU:1500 Metric:1

 

I hope this example will be useful to understand platform & subprocess module in python. To learn more about platform & subprocessplease refer following links :

https://docs.python.org/2/library/subprocess.html

https://docs.python.org/2/library/platform.html

 

 

SSH Connection using Python paramiko

Secure Shell (SSH) is a cryptographic network protocol for operating network services securely over an unsecured network. The best known example application is for remote login to computer systems by users.

Python allows us SSH connection using library paramiko. It is a Python implementation of the SSHv2 protocol, providing both client and server functionality.

 

To install paramiko library,  run the following command in cmd. paramiko needs cryptography as dependency module. So run both commands in cmd :

 

C:\Python27\Scripts >> pip install paramiko
C:\Python27\Scripts >> pip install cryptography

 

After installation is done, now we will connect to remote SSH server using paramiko library.  Code snippet for the same is given below :

 

import paramiko

#Create object of SSHClient and connecting to SSH
ssh = paramiko.SSHClient()
ssh.connect('1.1.1.2', port=22, username='UserName', password='PassWord', timeout = 3)

#To add new host key to the local HostKeys object if missing
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

#Execute command on SSH terminal using exec_command
stdin, stdout, stderr = ssh.exec_command('show ip interface brief')

for name in stdout:
   print name

 

Taking this as base, one can automate the stuff of login to remote SSH server, executing commands and capturing the results, just using one python script. I hope this example will be useful to understand about paramiko.

To know more about paramiko, please refer following link :

http://docs.paramiko.org/en/2.3/index.html

Working with Database – Python SQLite3

SQLite, as the name suggests, is a lite version of an SQL database. SQLite3 comes as a part of the Python 3 standard library. One can create database locally and store the data using SQLite.

In the following example, I have created one database called “tutorial.db”. Within this database, one table is created with name called “mytable” and data is inserted inside the table. To run any SQL command , pass it as argument to function execute(sql_command_to_execute). 

import sqlite3

# To connecting with database
conn = sqlite3.connect('tutorial.db')
c = conn.cursor()

# To create the table within the database
def create_table():
   c.execute("CREATE TABLE IF NOT EXISTS mytable ( roll_no INT, name VARCHAR(20) )")

# To insert data inside the table
def insert_data(roll_no, name):
   command = "INSERT INTO mytable VALUES ({}, \"{}\")".format(roll_no,name)
   c.execute(command)

# To save data of the table
def save_data():
   c.close()
   conn.commit()
   conn.close()

# To delete the table
def delete_table():
   c.execute("DROP TABLE mytable")

# To fetch the data from database
def fetch_data(table_name):
   conn = sqlite3.connect('tutorial.db')
   c = conn.cursor()
   c.execute("SELECT * FROM {}".format(table_name))
   ans = c.fetchall()
   for i in ans:
   print i

########################################################3

create_table()
insert_data(1,"Suresh")
insert_data(2,"Ramesh")
insert_data(3,"Mahesh")
save_data()
fetch_data()
delete_table()

 

Output of the following can be printed on the command line like shown below :

(1, u'Suresh')
(2, u'Ramesh')
(3, u'Mahesh')

 

One can write commands to search the data from the table and delete the data from the table. I hope it will be useful. Write your doubts and suggestions in comments. Thank you 🙂

 

Reference :

http://www.geeksforgeeks.org/sql-using-python/

https://pythonprogramming.net/sql-database-python-part-1-inserting-database/

Updating Excel Workbook with Python ( OpenPyXl and Tkinter )

We all have worked with MS Excel and have entered data manually in sheets. But when one has to enter the data of thousands of users, Python comes to the rescue. Openpyxl library allows you to work with Excel sheets. One can automate boring stuff of entering data manually to sheet using Openpyxl library.

 

Stuff to automate : Take Username and Password from user and store it in a Excel Sheet. Here for this example, I have used one more Python library called Tkinter, for graphical user interface. So there are two files :

  1. excelprogram.py : for storing user data in Excel Sheet.
  2. excelgui.py : for taking user input through GUI Window.

 

Code Snippet for file excelprogram.py :

import openpyxl

filename = 'userdata.xlsx'

if os.path.isfile(filename):
   wb = openpyxl.Workbook(filename)
 if 'Data' in wb.get_sheet_names():
   pass
 else:
   wb.create_sheet(index=0, title='Data')
else:
   wb = openpyxl.Workbook()
   wb.create_sheet(index=0, title='Data')
   wb.save(filename)

wb = openpyxl.load_workbook(filename)
sheet = wb.get_sheet_by_name('Data')

def add_username(name,password):
   ws = wb.active
   first_column = ws['A']
   second_column = ws['B']
   col_len1 = str(len(first_column)+1)
   col_len2 = str(len(second_column)+1)
   sheet['A' + col_len1] = name
   sheet['B' + col_len2] = password
   wb.save(filename)

if (sheet['A1'].value == 'Username') and (sheet['B1'].value == 'Password'):
   pass 
else:
   sheet['A1'] = 'Username'
   sheet['B1'] = 'Password'

 

Code Snippet for file excelgui.py :

from Tkinter import *
from excelprogram import *

root = Tk()

def saveData():
 global entry1
 global entry2
 username = entry1.get()
 password = entry2.get()
 add_username(username,password)
 print username, password

root.minsize(600,300)
root.title("Login Details")
root.configure(background='light grey')

root.grid_columnconfigure(0, minsize=20)
root.grid_columnconfigure(1, minsize=30)
root.grid_columnconfigure(3, minsize=75)
root.grid_rowconfigure(0, minsize=50)
root.grid_rowconfigure(2, minsize=50)
root.grid_rowconfigure(4, minsize=50)

label_1 = Label(root,text = "Enter the Username :", font = (None, 15))
label_1.grid(row=1,column=2,sticky=W)

entry1 = Entry(root, width = 30)
entry1.grid(row=1, column=4, sticky=W)

label_2 = Label(root,text = "Enter the Password :", font = (None, 15))
label_2.grid(row=3,column=2,sticky=W)

entry2 = Entry(root, show="*", width = 30)
entry2.grid(row=3, column=4, sticky=W)

button = Button(root,text='Enter',command=saveData, height = 1, width = 10,font = (None, 15))
button.grid(row=5, column=3, sticky=W)

root.mainloop()

 

Note : Here I have imported excelprogram functions to excelgui.py . So just run the excelgui.py file which internally will call to excelprogram functions for storing data inside Excel Sheet. One can use files separately for different purpose, with little modification.

Output : 

 

I hope it will be useful. Write your doubts and suggestions in comments. Thank you 🙂

 

Reference :

https://automatetheboringstuff.com/chapter12/

Send Email without opening Gmail with Python

Have you ever tried sending email without opening an email account? Python allows you to send email without even opening a browser. Python provides smtplib library for that. Code snippet is given below :

 

import smtplib
import getpass  # to hide the password

smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
smtpObj.ehlo()

smtpObj.starttls()

username = raw_input("Enter your email address = ")
password = getpass.getpass('Password =')
receivername = raw_input("Enter receiver's email address = ")
smtpObj.login(username, password)

smtpObj.sendmail(username, receivername, 'Subject: Automated Mail\nDear Bhavesh, This is automated mail.\n\nSincerely, Bhavesh Tanwani')

smtpObj.quit()

 

Note : Above is written specifically for Gmail. SMTP Server domain name and port value will change in method smtplib.SMTP() for different emails.

Run above code in command prompt. One may get following error :

raise SMTPAuthenticationError(code, resp)
SMTPAuthenticationError: (534, '5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbtw\n5.7.14 Qh5zRfnA-9UWR2K374HBUhol1Byhou8DA0STuEcVs2A2eEv8kTCt4tyhr8fO80PWRhZQo7\n5.7.14 jj5mIvWQGo6t_s1rSpbYIK8WUNqGaqLjZ8df95D8lMZA6IXfklMXa7NsSvAflWZVOgJCTu\n5.7.14 oRDnCyxPeVLO7-4Qv06STxxQspf4YghLF43a7ShlMlkQblP_qEl-diAlbH2jx_LHHej9yj\n5.7.14 dYbVA4L9I48O73H4yBbKKl-J_AcVY> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 z1sm10960334pge.45 - gsmtp')

 

Note : Google blocks sign-in attempts from apps which do not use modern security standards (mentioned on their support page). You can however, turn on/off this safety feature by going to the link below:

Go to this link and select Turn On
https://www.google.com/settings/security/lesssecureapps

This should solve your problem and you will be able to send the email. One can automate, sending of emails using this.

 

Refer below link to learn more :

https://automatetheboringstuff.com/chapter16/

Automating Login for website using Python (Selenium)

To automate any website login, we can use selenium library for python. Not only login but one can automate, testing of whole website using selenium python library.

To install selenium library for Python, run the following command in cmd :

C:\Python27\Scripts >> pip install selenium

 

I have tried automating Facebook login using selenium library. Code snippet is given below:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox(executable_path=r'C:\geckodriver\geckodriver.exe')
driver.get("https://www.facebook.com/")

driver.find_element_by_id("email").send_keys("abc_xyz@gmail.com")
driver.find_element_by_id("pass").send_keys("password")
driver.find_element_by_id("u_0_r").click()

 

Users can automate Gmail login using same code but one has to change the values in method find_element_by_id() . Method find_element_by_id() is called Element Locator in Selenium to locate particular element. There are multiple Element Locators :

  • find_element_by_id
  • find_element_by_name
  • find_element_by_xpath
  • find_element_by_link_text
  • find_element_by_partial_link_text
  • find_element_by_tag_name
  • find_element_by_class_name
  • find_element_by_css_selector

Any of the above Element Locators can be used to find the value of the element. To learn more about selenium Python library follow the below link :

http://selenium-python.readthedocs.io/

Automate Windows Applications using Python

Here I have used two Python libraries ( pyautogui , pywinauto ), which can be used to automate any windows application.

To install the above libraries in Python, run the following commands from cmd :

C:\Python27\Scripts >> pip install pyautogui
C:\Python27\Scripts >> pip install pywinauto

 

I have automated opening of Notepad, writing text in a file and saving the file using these libraries.

 

Using  pywinauto :

from pywinauto import application
import time

app = application.Application()
app.start("Notepad.exe")

app.Notepad.edit.TypeKeys("I love Python....!!!!", with_spaces = True)
app.Notepad.MenuSelect("File -> Save")
app.SaveAs.edit.SetText("pywinautodemo.txt")
time.sleep(5)

app.SaveAs.Save.Click()

 

Using pyautogui :

import pyautogui
import os
import time

def press_fun(key):
    pyautogui.press(key)

def type_fun(val):
    pyautogui.typewrite(val)

os.startfile("Notepad.exe")
time.sleep(5)

type_fun("I love Python....!!!!")
press_fun("alt")
press_fun("f")
press_fun("s")
time.sleep(5)

type_fun(os.getcwd() + "\\" + "pyautoguidemo.txt")
time.sleep(5)

press_fun("enter")

 

To learn more about these libraries, refer following links :

http://pyautogui.readthedocs.io/en/latest/
http://pywinauto.readthedocs.io/en/latest/
https://automatetheboringstuff.com/chapter18/

 

Difference between Python 2.x & Python 3.x

Following are the main differences :

Difference

Python 2.x

Python 3.x

print function – Round brackets are mandatory in Python 3.x
print "Hello World...!!"
print ("Hello World....!!!")
Division OperatorPython 2.x gives floor division where Python 3.x gives true division.
print 6/5 ===> 1
print -6/5 ===> -2
print (6/5) ====> 1.2
print (-6/5) ====> -1.2
 range() function Python 2.x stores the range in the form of list in memory Python 3.x, range() behaves as xrange() in Python 2.x. It iterates over range and stores one element at time in the memory.
str type str type is ASCII. str type is Unicode.
Error Handling –  In Python 3.x, ‘as’ keyword is required.
try:
    check_error
except NameError, err:
    print err, "Error Ocurred"
try:
    check_error
except NameError as err:
   print (err, "Error Ocurred")

 

To use Python 3.x functionalities in Python 2.x, one can use __future__ module.

Example :

from __future__ import print_function
from __future__ import division

print ("Hello World....!!!")
print (5/7)

Output :

Hello World....!!!
0.714285714286