4_PHP
4_PHP
Parameter
Command- The command that will be escaped.
Return Value
The function/method returns the string after escaping the described chraters
as backtick operators
Example
<?php Output
$command = substr("\\%!** Hello World",0); ^\^%^!^*^* Hello World
$escaped_command = escapeshellcmd($command);
echo ($escaped_command);
?>
shell_exec ()
The shell_exec() function is an inbuilt function in PHP which is used to execute the
commands via shell and return the complete output as a string. The shell_exec is an
alias for the backtick operator, for those used to unix. If the command fails return
NULL and the values are not reliable for error checking.
Syntax:
string shell_exec( $cmd )
Parameters:
This function accepts single parameter $cmd which is used to hold the
command that will be executed.
Return Value:
This function returns the executed command or NULL if an error occurred.
Note:
This function is disabled when PHP is running in safe mode.
Example:
<?php
// shell_exec test
if (shell_exec('mkdir test'))
Explanation
test.py: in this file the first line mentions from where the python executable file can
locate so that the python code can get execute there after. Whereas the second line is
just a print statement for displaying a string.
test.php: in php file the first line is using the escapeshellcmd function accepts the
command for executing the python file test.py. The second line extract the generated
output of first command in a PHP variable. And third is just used to show the resulted
variable on the browser by just passing the URL of PHP file to execute it.
4.2 Executing PHP Script using Python
4.2.1 Sub-process Module in Python:
The subprocess module present in Python(both 2.x and 3.x) is used to run new
applications or programs through Python code by creating new processes. It also
helps to obtain the input/output/error pipes as well as the exit codes of various
commands.
A running program is called a process. Each process has its own system state,
which includes memory, lists of open files, a program counter that keeps track of the
instruction being executed, and a call stack used to hold the local variables of
functions.
A program can create new processes using library functions such as those
found in the os or subprocess modules such as os.fork(), subprocess.Popen(), etc.
However, these processes, known as subprocesses, run as completely independent
entities-each with their own private system state and main thread of execution.
Subprocess Module
The subprocess module allows us to:
spawn new processes
connect to their input/output/error pipes
obtain their return codes
4.2.1.1Methods: Check-call(), check-output(), declode(), Popen(),
Communicate(), split()
Popen() function
The Underlying process creation and management in sub-process module is
handled by the popen class. It offers a lot of flexibility so that developers are able to
handle the less common cases not covered by the convenience functions.The P in the
name of the Popen() function stands for process.
If you have multiple instances of an application open, each of those instances is
a separate process of the same program.
For example, if you open multiple windows of your web browser at the same
time, each of those windows is a different process of the web browser program
Syntax:
class subprocess.Popen(args, bufsize=-
1, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=True, shell=F
alse, cwd=None, env=None, universal_newlines=None, startupinfo=None, creationflags=0, restore_signa
In this syntax we are storing the command output (stdout) and command error
(stderr) in the same variable
Example1:
import subprocess
proc = subprocess.Popen("php demo1.php", shell=True, stdout=subprocess.PIPE)
script_response = proc.stdout.read()
print(script_response)
Let’s assume that demo1.php has 1 line code <?php echo “hello world”; ?>. it will
return hello world in it’s output.
Example:
#!/usr/bin/env python3
import subprocess
# Define command as string
cmd = 'ls -ltr'
# Use shell to execute the command and store it in sp variable
sp = subprocess.Popen(cmd,shell=True)
# Store the return code in rc variable
rc=sp.wait()
# Print the content of sp variable
print(sp)
call() function
Subprocess in Python has a call() method that is used to initiate a program.
The subprocess module provides another function that makes process
spawning a safer operation than using Popen().
The subprocess.call() function waits for the called command/program to finish
reading the output.
Syntax:
subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None,
**other_popen_kwargs)
Example:
import subprocess
subprocess.call(["ls", "-l"])
check_call() function
This is another function which is part of subprocess module which can run
command with arguments. check_call will wait for command execution to complete.
check_call() will wait for command execution to complete.
If the execution is successful then the function will return zero then return,
otherwise raise CalledProcessError.
The CalledProcessError object will have the return code in
the returncode attribute.
Syntax:
subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
Parameters
args: It is the command that you want to execute. You can pass multiple commands by
separating them with a semicolon (;)
stdin: This refers to the standard input stream’s value passed as (os.pipe())
stdout: It is the standard output stream’s obtained value
stderr: This handles any errors that occurred from the standard error stream
shell: It is the boolean parameter that executes the program in a new shell if kept true
Example:
import subprocess
s = subprocess.check_call("gcc HelloWorld.c -o out1;./out1", shell = True)
print(", return code", s)
Note:Do not use stdout=PIPE or stderr=PIPE with call and check_call function as that
can deadlock based on the child process output volume. Use Popen with the
communicate() method when you need pipes.
check_output() function
The save process output or stdout allows you to store the output of a code
directly in a string with the help of the check_output function.
The subprocess.check_output() is similar to subprocess.run(check=True)
By default, this function will return the data as encoded bytes so you can
use text=True or universal_newlines=True to get string value as output
This function will run command with arguments and return its output.
If the return code was non-zero it raises a CalledProcessError.
The CalledProcessError object will have the return code in
the returncode attribute and any output in the output attribute.
Syntax:
subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False
OR
subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, cwd=None, encoding=None, err
ors=None, universal_newlines=None, timeout=None, text=None, **other_popen_kwargs)
Parameters:
args The command to be executed. Several commands can be passed as a string by
separated by “;”.
stdinValue of standard input stream to be passed as pipe(os.pipe()).
stdoutValue of output obtained from standard output stream.
stderrValue of error obtained(if any) from standard error stream.
shellboolean parameter.If True the commands get executed through a new shell
environment.
universal_newlinesBoolean parameter.If true files containing stdout and stderr are
opened in universal newline mode.
Example:
import subprocess
s = subprocess.check_output(["echo", "Hello World!"])
print(s)
Output:
b’Hello World’!
communicate() function
In subprocess,Popen() can interact with the three channels and redirect each
stream to an external file, or to a special value called PIPE. An additional method,
called communicate(), is used to read from the stdout and write on the stdin.
The spawned processes can communicate with the operating system in three
channels:
Output:
b‘‘
… program finished with exit code 0
Press enter to exit console
In the above code, the process.communicate() is the primary call that reads all
the process’s inputs and outputs. The “stdout” handles the process output, and the
“stderr” is for handling any sort of error and is written only if any such error is
thrown.
split() function
Sometime, for example when you use subprocess you might want to run an
external program avoiding the invocation of the shell. For that you need to have the
external command in pieces, not in a single long string.
You could use the regular split method of strings with space as the delimiter,
but that would not give you the right results if some of the pieces contain spaces.
The solution is to use the split method of the shlex module.In this example we
use both. The str.split() and the shlex.split()
Example:
import shlex
cmd = './bin/application --source /some/directory --target /other/dir --verbose -d --name "Foo Bar"'
print(cmd.split(' '))
print(shlex.split(cmd))
Output:
['./bin/application', '--source', '/some/directory', '--target', '/other/dir', '--verbose', '-d', '--name',
'"Foo', 'Bar"']
['./bin/application', '--source', '/some/directory', '--target', '/other/dir', '--verbose', '-d', '--name', 'Foo
Bar']
decode() function
There are a couple of methods you can use to convert the byte into string
format for subprocess.Popen output:
decode("utf-8") (It Return a string decoded from the given bytes. Default
encoding is 'utf-8')
universal_newlines
In decode, errors may be given to set a different error-handling scheme. The
default for errors is 'strict', meaning that encoding errors raise a UnicodeError. Other
possible values are 'ignore', 'replace' and any other name registered via
codes.register.error().
Syntax:
bytes.decode(encoding='utf-8', errors='strict')
bytearray.decode(encoding='utf-8', errors='strict')
Example:
s = subprocess.check_call("gcc HelloWorld.c -o out1;./out1", shell = True)
print(", return code", s)
print(s.decode("utf-8"))
here, it decode “s” to a normal string
Output:
Number of bytes written: 51
Read()
os.read() method in Python is used to read at most n bytes from the file
associated with the given file descriptor.
If the end of the file has been reached while reading bytes from the given file
descriptor, os.read() method will return an empty bytes object for all bytes left to be
read.
Note: os.read() method is intended for low-level operation and should be applied to a
file descriptor as returned by os.open() or os.pipe() method.
Syntax:
os.read(fd, n)
Parameter:
fd: A file descriptor representing the file to be read.
n: An integer value denoting the number of bytes to be read from the file associated
with the given file descriptor fd.
Return Type:
This method returns a bytestring which represents the bytes read from the file
associated with the file descriptor fd.
Consider the below text as the content of the file named mywork.txt.
Example:
import os
path = "/home / phppython / myworks.txt"
fd = os.open(path, os.O_RDONLY)
# Number of bytes to be read
n = 51
readBytes = os.read(fd, n)
print(readBytes)
os.close(fd)
Output:
myworks: A Computer science portal for works’
close()
os.close() method in Python is used to close the given file descriptor, so that it
no longer refers to any file or other resource and may be reused.
Syntax:
os.close(fd)
Parameter:
fd: A file descriptor, which is to be closed.
Return Type:
This method does not return any value
Example:
import os
path = "/ home / phppython / myworks.txt"
fd = os.open(path, os.O_WRONLY)
s = "myworks: A computer science portal for works"
# Convert string to bytes object
line = str.encode(s)
os.write(fd, line)
os.close(fd)
print("File descriptor closed successfully")
Output:
File descriptor closed successfully
Output:
This is awesome
os.close()
This function closes the associated file with descriptor fr.
Example
import os
fr = "Python1.txt"
file = open(fr, 'r')
text = file.read()
print(text)
os.close(file)
Output:
Traceback (most recent call last):
Directory Handling
There are different methods available in the OS module for creating a directory.
These are –
os.mkdir() and os.makedirs()
os.mkdir()
os.mkdir() method in Python is used to create a directory named path with the
specified numeric mode. This method raises FileExistsError if the directory to be
created already exists.
Example:
import os
os.mkdir("d:\\newdir")
It will create the new directory to the path in the string argument of the
function in the D drive.
os.makedirs()
os.makedirs() method in Python is used to create a directory recursively. That
means while making leaf directory if any intermediate-level directory is missing,
os.makedirs() method will create them all.
Example:
#import the os module
from os import listdir, makedirs
#print the list of files and directories in current directory
print("Before adding directory")
print(listdir())
#create a directory
makedirs("./Educative/Leaf", 0o755)
print("After adding directory")
#print the list of files and directories in current directory
print(listdir())
print("Display inner directory")
print(listdir("./Educative"))
output:
Before adding directory
['__ed_script.sh', '__ed_stderr.txt', 'output', '__ed_mem.txt', 'main.py', '__ed_stdout.txt']
output:
False
True
Output:
.idea is a directory
main.py is a file
renamed_file.py is a file
os.path.join() :
This method joins various path components with exactly one directory
separator (“/”) following each non
non-empty
empty part except for the last path component. If
the last path component is empty then a directory separator (“/”) is put at the end.
This method returns a string with the concatenated path.
Example1:
Example2:
import os
good_vals = os.environ.get('homedrive')
joined = os.path.join(good_vals, '/index.html')
print(joined)
4.2.2.2isdir(),
(), listdir(), walk(), chdir()
isdir()
OS module in Python provides functions for interacting with the operating
system. OS comes under Python’s standard utility modules. This module provides a
portable way of using operating system dependent functionality. os.path module is
sub module of OS module in Python used for common path name manipulation.
os.path.isdir() method in Python is used to check whether the specified path is
an existing directory or not. This method follows ssymbolic
ymbolic link, that means if the
specified path is a symbolic link pointing to a directory then the method will return
True.
Syntax:
os.path.isdir(path)
Parameter:
path: A path-like
like object representing a file system path.
Return Type: This method returns a Boolean value of class bool. This method
returns True if specified path is an existing directory, otherwise returns False.
Example:
# Python program to explain os.path.isdir() method
# importing os.path module
import os.path
# Path
504 – Web Services and Framework (TYBCA Sem-V)
path = '/home/User/Documents/file.txt'
# Check whether the
# specified path is an
# existing directory or not
isdir = os.path.isdir(path)
print(isdir)
# Path
path = '/home/User/Documents/'
# Check whether the
# specified path is an
# existing directory or not
isdir = os.path.isdir(path)
print(isdir)
Output:
False
True
listdir()
This method in Python is used to get the list of all files and directories in the
specified directory. If we don’t specify any directory, then the list of files and
directories in the current working directory will be returned.
Example:
# Python program to explain os.listdir() method
# importing os module
import os
# Get the list of all files and directories
# in the root directory
path = "/"
dir_list = os.listdir(path)
print("Files and directories in '", path, "' :")
# print the list
print(dir_list)
Output:
Files and directories in ' / ' :
['sys', 'run', 'tmp', 'boot', 'mnt', 'dev', 'proc', 'var', 'bin', 'lib64', 'usr',
'lib', 'srv', 'home', 'etc', 'opt', 'sbin', 'media']
walk()
Suppose we have given below file structure in our system and we want to
traverse all its branches completely from top to bottom?
Example2:
import os
for root, dirs, files in os.walk('c:\python\osmodule'):
for name in files:
print('file: ' + os.path.join(root, name))
for name in dirs:
print('dir: ' + os.path.join(root, name))
output
file: C:\python\osmodule\main.py
file: C:\python\osmodule\renamed_file.py
dir: C:\python\osmodule\.idea
file: C:\python\osmodule\.idea\.gitignore
file: C:\python\osmodule\.idea\misc.xml
file: C:\python\osmodule\.idea\modules.xml
file: C:\python\osmodule\.idea\osmodule.iml
file: C:\python\osmodule\.idea\workspace.xml
504 – Web Services and Framework (TYBCA Sem-V)
dir: C:\python\osmodule\.idea\inspectionProfiles
file: C:\python\osmodule\.idea\inspectionProfiles\profiles_settings.xml
file: C:\python\osmodule\.idea\inspectionProfiles\Project_Default.xml
chdir()
The os module provides the chdir() function to change the current working
directory.
Example:
import os
os.chdir("d:\\")
Output:
d:\\
Path:
The os.path module is a very extensively used module that is handy when
processing files from different places in the system. It is used for different purposes
such as for merging, normalizing and retrieving path names in python . All of these
functions accept either only bytes or only string objects as their parameters. Its results
are specific to the OS on which it is being run.
import os.path vs import os
os.path works strangely actually. It looks like os packaged with a
submodule path but actually, os is a normal module which operate with sys.module to
support os.path. Let us list what happens behind the scenes:
When Python starts, it loads many modules into sys.module.
os module is also loaded when Python starts. It assigns its path to
the os specific module attribute.
It injects sys.modules['os.path'] = path so that you’re able to do import
os.path as though it was a submodule.
os.path module contains some useful functions on pathnames. The path
parameters are either strings or bytes . The result is an object of the same type, if a
path or file name is returned. As there are different versions of operating system so
there are several versions of this module in the standard library.
Following are some functions of OS Path module.
1. os.path.basename(path): This function gives us the last part of the path which
may be a folder or a file name. The difference in how the path is mentioned in
Windows and Linux in terms of the backslash and the forward slash.
Example1:
import os
# In windows
fldr = os.path.basename("C:\Users\xyz\Documents\My Web Sites")
print(fldr)
Output
My Web Sites
intro.html
MyWebSites
Output
C:\Users\xyz\Documents
/Documents
Output:
True
Example
# isdir function
import os
out = os.path.isdir("C:\\Users")
print(out)
Output:
True
5. os.path.isfile(path) : Sometimes we may need to check if the complete path given,
represents a folder or a file. If the file does not exist then it will give False as the
output. If the file exists then the output is True.
Example
print(IS_FILE)
IS_FILE = os.path.isfile("C:\Users\xyz\Documents\My Web Sites\intro.html")
print(IS_FILE)
# In nix*
IS_FILE = os.path.isfile("/Documents/MyWebSites")
print(IS_FILE)
IS_FILE = os.path.isfile("/Documents/MyWebSites/music.txt")
print(IS_FILE)
Output:
'\\baz'
depending on which OS it is. Which means so that A//B, A/B/, A/./B and A/foo/../B all
become A/B. On Windows, it converts forward slashes to backward slashes.
Example
import os
# Windows path
NORM_PATH = os.path.normpath("C:/Users/Pradeep/Documents/My Web Sites")
print(NORM_PATH)
# Unix Path
NORM_PATH = os.path.normpath("/home/ubuuser//Documents/")
print(NORM_PATH)
Example2:
# normpath function in Unix
import os
out = os.path.normpath("foo/./bar")
print(out)
Output:
'foo/bar'
8. os.path.getsize(): In this method, python will give us the size of the file in bytes. To
use this method we need to pass the name of the file as a parameter.
Example
import os #importing os module
size = os.path.getsize("filename")
print("Size of the file is", size," bytes.")
Output:
Size of the file is 192 bytes.
Extra functions:
split(p)
Split a pathname. Return tuple (head, tail) where tail is everything after the
final slash. Either part may be empty.
import os
split = os.path.split('path/to/file.html')
print(split)
output:
('path/to', 'file.html')
getcwd()
It returns the current working directory(CWD) of the file.
import os
print(os.getcwd())
rmdir()
The rmdir() function removes the specified directory with an absolute or
related path. First, we have to change the current working directory and remove the
folder.
Example
import os
# It will throw a Permission error; that's why we have to change the current working directory.
os.rmdir("d:\\newdir")
os.chdir("..")
os.rmdir("newdir")
rename()
A file or directory can be renamed by using the function os.rename(). A user
can rename the file if it has privilege to change the file.
Example
import os
fd = "python.txt"
os.rename(fd,'Python1.txt')
os.rename(fd,'Python1.txt')
Output:
Traceback (most recent call last):
File "main.py", line 3, in
os.rename(fd,'Python1.txt')
FileNotFoundError: [Errno 2] No such file or directory: 'python.txt' -> 'Python1.txt'
error()
The os.error() function defines the OS level errors. It raises OSError in case of
invalid or inaccessible file names and path etc.
Example
import os
try:
# If file does not exist,
# then it throw an IOError
filename = 'Python.txt'
f = open(filename, 'rU')
text = f.read()
f.close()
# The Control jumps directly to here if
# any lines throws IOError.
except IOError:
# print(os.error) will <class 'OSError'>
print('Problem reading: ' + filename)
Output:
Problem reading: Python.txt
All functions in the python os module raise the OSError (or subclasses thereof)
when invalid or inaccessible file names and paths, or other arguments that have the
correct type, but are not accepted by the operating system are encountered.