Python for ethical hackers
Mohammad reza Kamalifard
kamalifard@datasec.ir
Python language essentials
Module 1:
Introduction to Python and Setting up an Environment for Programing
Part 5 :
Functions
Functions
• Functions are a construct to structure programs.
• Functions are used to utilize code in more than one place

in a program.
• The only way without functions to reuse code consists in
copying the code.
Functions
• Functions allow sections of code to be grouped better as

per functionality
Functions
A function in Python is defined by a def statement. The
general syntax looks like this:
def function-name(Parameter list):
statements, i.e. the function body
• The parameter list consists of none or more parameters.
• The function body gets executed every time the function is

called. 
Functions
def function(arg1,arg2=default,..):
do_something
return value
>>> def print5times(input):
...
for count in range(0,5):
...
print input
...
>>> print5times('Hello')
Hello
Hello
Hello
Hello
Hello
>>> print5times('Javad')
Javad
Javad
Javad
Javad
Javad
>>> print5times('Hello, World!')
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
>>>
return statement
• Function bodies can contain a return statement.
•  Can be anywhere in the function body.
• Ends the execution of the function call and "returns" the

result, i.e. the value of the expression following the return
keyword, to the caller.
• If there is no return statement in the function code, the

function ends, when the control flow reaches the end of the
function body. 
def add(x, y):
"""Return x plus y"""
return x + y
>>> add(4, 5)
9
>>> add(8, 3)
11
>>>
def add(x, y = 5):
"""Return x plus y"""
return x + y
>>> add(4)
9
>>> add(8, 3)
11
>>>
Arbitrary Number of Parameters
def arbitrary(x, y, *more):
print "x=", x, ", x=", y
print "arbitrary: ", more
>>>arbitrary(3, 4)
x = 3 , x = 4
arbitrary: ()
>>>arbitrary(3, 4, "Hello World", 5 ,6)
x = 3 , x = 4
arbitrary: ('Hello World', 5, 6)
$ vim testargv.py
#!/usr/bin/env python
import sys
print sys.argv
~
~
$ chmod a+x testargv.py
$ ./testargv.py
['./testargv.py']
$
$ ./testargv.py salam
['./testargv.py', 'salam']
$
$
$./testargv.py -h 199 --app firefox
['./testargv.py', '-h', '199', '--app', 'firefox']
$
$ ./function.py salam
salam
salam
salam
salam
salam
$
$ ./function.py salam hamid
salam
salam
salam
salam
salam
$
$ ./function.py 'salam hamid'
salam hamid
salam hamid
salam hamid
salam hamid
salam hamid
$ vim function.py
#!/usr/bin/env python
import sys
def print5times(content_to_print):
for count in range(0,5):
print content_to_print
print5times(sys.argv[1])
~
~
$ ./function.py salam
salam
salam
salam
salam
salam
$
$ ./function.py salam hamid
salam
salam
salam
salam
salam
$
$ ./function.py 'salam hamid'
salam hamid
salam hamid
salam hamid
salam hamid
salam hamid
References
SPSE securitytube training by Vivek Ramachandran
SANS Python for Pentesters (SEC573)
Violent python
Security Power Tools
python-course.eu
----------------------------http://www.python-course.eu/functions.php
http://www.tutorialspoint.com/python/python_functions.htm
This work is licensed under the Creative Commons Attribution-NoDerivs 3.0 Unported License.
To view a copy of this license, visit
http://creativecommons.org/licenses/by-nd/3.0/
Copyright 2013 Mohammad Reza Kamalifard
All rights reserved.
Go to Kamalifard.ir/pysec101 to Download Slides and Course martials .

جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱

  • 1.
    Python for ethicalhackers Mohammad reza Kamalifard kamalifard@datasec.ir
  • 2.
    Python language essentials Module1: Introduction to Python and Setting up an Environment for Programing Part 5 : Functions
  • 3.
    Functions • Functions area construct to structure programs. • Functions are used to utilize code in more than one place in a program. • The only way without functions to reuse code consists in copying the code.
  • 4.
    Functions • Functions allowsections of code to be grouped better as per functionality
  • 5.
    Functions A function inPython is defined by a def statement. The general syntax looks like this: def function-name(Parameter list): statements, i.e. the function body • The parameter list consists of none or more parameters. • The function body gets executed every time the function is called. 
  • 6.
    Functions def function(arg1,arg2=default,..): do_something return value >>>def print5times(input): ... for count in range(0,5): ... print input ... >>> print5times('Hello') Hello Hello Hello Hello Hello
  • 7.
    >>> print5times('Javad') Javad Javad Javad Javad Javad >>> print5times('Hello,World!') Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! >>>
  • 8.
    return statement • Functionbodies can contain a return statement. •  Can be anywhere in the function body. • Ends the execution of the function call and "returns" the result, i.e. the value of the expression following the return keyword, to the caller. • If there is no return statement in the function code, the function ends, when the control flow reaches the end of the function body. 
  • 9.
    def add(x, y): """Returnx plus y""" return x + y >>> add(4, 5) 9 >>> add(8, 3) 11 >>>
  • 10.
    def add(x, y= 5): """Return x plus y""" return x + y >>> add(4) 9 >>> add(8, 3) 11 >>>
  • 11.
    Arbitrary Number ofParameters def arbitrary(x, y, *more): print "x=", x, ", x=", y print "arbitrary: ", more >>>arbitrary(3, 4) x = 3 , x = 4 arbitrary: () >>>arbitrary(3, 4, "Hello World", 5 ,6) x = 3 , x = 4 arbitrary: ('Hello World', 5, 6)
  • 12.
    $ vim testargv.py #!/usr/bin/envpython import sys print sys.argv ~ ~
  • 13.
    $ chmod a+xtestargv.py $ ./testargv.py ['./testargv.py'] $ $ ./testargv.py salam ['./testargv.py', 'salam'] $ $ $./testargv.py -h 199 --app firefox ['./testargv.py', '-h', '199', '--app', 'firefox'] $
  • 14.
    $ ./function.py salam salam salam salam salam salam $ $./function.py salam hamid salam salam salam salam salam $ $ ./function.py 'salam hamid' salam hamid salam hamid salam hamid salam hamid salam hamid
  • 15.
    $ vim function.py #!/usr/bin/envpython import sys def print5times(content_to_print): for count in range(0,5): print content_to_print print5times(sys.argv[1]) ~ ~
  • 16.
    $ ./function.py salam salam salam salam salam salam $ $./function.py salam hamid salam salam salam salam salam $ $ ./function.py 'salam hamid' salam hamid salam hamid salam hamid salam hamid salam hamid
  • 17.
    References SPSE securitytube trainingby Vivek Ramachandran SANS Python for Pentesters (SEC573) Violent python Security Power Tools python-course.eu ----------------------------http://www.python-course.eu/functions.php http://www.tutorialspoint.com/python/python_functions.htm
  • 18.
    This work islicensed under the Creative Commons Attribution-NoDerivs 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nd/3.0/ Copyright 2013 Mohammad Reza Kamalifard All rights reserved. Go to Kamalifard.ir/pysec101 to Download Slides and Course martials .