Changes

Jump to: navigation, search

OPS435 Python Lab 3

520 bytes removed, 23:57, 27 January 2018
PART 2 - Running System Commands with Subprocess
== PART 2 - Running System Commands with Subprocess ==
:The remainder of this investigation will allow you to run operating system commands via your Python script. Although there are different ways in which to issue operating system commands, you will learn how to issue two of them within a Python script to run in a secure manner regardless of the type of operating system platform (eg Linux, Windows, MacOSX).
'''Perform the Following Steps:'''
:#Start the ipython3 shellCreate a new python file for testing.:<source>ipython3</source> #You can issue operating system commands by using the '''system()''' function.<br><br>:#Issue the following in ipythonTry it:<source lang="python">os.system('ls')os.system('whoami')os.system('ifconfig')</source> You should notice Notice that the output from the Linux commands that you called programs is not printed to the console automatically, it's printed by ipython onlyin your script. Make sure Consider that may not always be what you understand the differencewant.<br><br>:#Issue the following in ipythonTry this also:<source lang="python">os.system('ipconfig')</source>You should notice an error message: ''''ipconfig : command not found''''. That error occurs since that command was an MS Windows command, and our current platform is Linux.<br><br>It is not always a good idea to run system commands in Python, this makes your Python code less portable and makes it require a specific operating system or a system that has those commands available. Also, allowing python to execute commands on the operating system can be a '''security problem'''. For these reasons You should think about that when you decide whether you should only or should not use '''sub-process''' and '''a system commands''' as a last resort and command to accomplish some task or stick to pure Python code only.<br><br>As you may recall from lab2, you issued '''import sys''' to import special variables from the system. You can import a subprocess in order to run common non OS specific commands securely.<br><br>:#Issue Import the following subprocess module in ipython:<source lang="your python">import subprocess</source>file.:#To view the available modules and attributes to obtain non OS specific command-like information, issue the following:<source>dir(subprocess)</source>There are many available modules and attributes features available as part of the subprocessmodule, we are interested in "'''Popen'''". This method subprocess.Popen() can be used to run system commands as a child process to the Python script. This The code below output will create a new child process, in Python we can control this through the new Python object we just created, "'''p'''". "'''p'''" now has a collection of methods(functions that are apart of a object) available, view them with '''dir()'''.<br><br>
:#To demonstrate, issue the following:<source lang="python">
p = subprocess.Popen(['date'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
:#This next step is going to communicate with the process and get the retrieve it's output (stdout).<source>
output = p.communicate()
print(output)print(output[0])
# The above stdout is stored in bytes
# Convert stdout to a string and strip off the newline characters
stdout = output[0].decode('utf-8').strip()
print(stdout)
</source>
:# Sometimes you will be able to use purely python code to get your job done, but often you will need to call existing system commands. It's important to learn how to call them and how to interact with those external processes.
'''Practice Running System Commands From Python'''
:'''Perform the Following Instructions:'''
:#<ol><li>Create the "'''~/ops435/lab3/lab3d.py'''" script. The purpose of this script is to create a Python function that can return the linux system's root directory free space.:::*The script should '''import the correct module''':::*The script should use the linux command: '''<nowiki>df -h | grep '/$' | awk '{print $4}'</nowiki>''' :::*The script should contain the function called: '''free_space()''':::*The function '''free_space()''' should return a string which is in '''utf-8''' and has '''newline characters strip''':::*'''Note:''' your output may be completely different, the free/available disk space on every computers root directory may be different.:::'''Sample Run 1:''' <source>run ./lab3d.py
9.6G
</source>
:::'''Sample Run 2 (with using importfrom another Python file):'''<source>
import lab3d
lab3d.free_space()
'9.6G'
</source></li>:::3. Exit the ipython3 shell, download <li>Download the checking script and check your work. Enter the following commands from the bash shell.<source lang="bash">
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
ls CheckLab3.py || wget https://raw.githubusercontent.com/Seneca-CDOT/ops435/master/LabCheckScripts/CheckLab3.py
python3 ./CheckLab3.py -f -v lab3d
</source></li>:::4. <li>Before proceeding, make certain that you identify any and all errors in lab3d.py. When the checking script tells you everything is OK before proceeding - proceed to the next step.</li></ol>
= INVESTIGATION 3: USING LISTS =

Navigation menu