This tutorial continues to show some basic Python scripting concepts. We cover pulling our code into a script, functions, classes, and the sys module.
Python Skeleton Script:
Below is a basic shell that can be used to start a Python script. We start out telling the OS which interpreter to use with the file by “#!/usr/bin/env python”. Then we declare a main function with “def main():” and the last 2 lines of code are to have main() run first. You can define other functions within your script to make the code easier to understand and modify:
#!/usr/bin/python import <module1>, <module2> def myFunction(): def main(): myFunction() if __name__=="__main__": main()
One common way to leverage a function is to have a snippet of code that performs some action and returns the output. Below is some basic pseudocode demonstrating this concept:
# Declare function/setup logic def MyFunction: ...do work... return output #Call the function from main: def main(): output = MyFunction(input)
Python classes can be confusing at first because it is a different way to design your code. If you have the grasped the concept of definitions then you can think of the Class as a logical grouping of data and definitions. So a class will have certain attributes and methods associated with it. When you define a class you can later create an object of that class that will inherit the attributes and methods associated with it, this is referred to as object-oriented programming.
I suggest not getting to caught up with Classes if the concept is confusing to you. You actually don’t need to leverage classes, but it can make your coding less redundant. Below we will define a new class “Domain” using the “class” keyword. The various methods within the class code are available when you instantiate an object of type Domain.
>>> import os >>> class Domain: ... def __init__(self, domain, port, protocol): # Stores the variabled passed inside two variables ... self.domain=domain ... self.port=port ... self.protocol=protocol # Defines a method to build a URL ... def URL(self): ... if self.protocol == 'https': ... URL = 'https://'+self.domain+':'+self.port+'/' ... if self.protocol == 'http': ... URL = 'http://'+self.domain+':'+self.port+'/' ... return URL # Sets up a method to lookup resolve domain to IP using host command via os.system ... def lookup(self): ... os.system("host "+self.domain) ... >>> >>> domain=Domain('google.com', '443', 'https') >>> >>> dir(domain) ['URL', '__doc__', '__init__', '__module__', 'ip', 'lookup', 'port', 'protocol'] >>> domain.URL() 'https://184.108.40.206:443/' >>> domain.ip '220.127.116.11' >>> domain.port '443' >>> domain.protocol 'https' >>> domain.lookup() google.com has address 18.104.22.168 google.com has address 22.214.171.124 google.com has address 126.96.36.199
As you can see after instantiating an instance of the Domain class you can run the methods within that class. Again, this concept can be confusing at first, especially when you are just grasping Python and programming in general. Try to implement a new class in a Python script you’ve already written, I find this can be a useful way to start to grasp the concept.
Handling CLI Arguments with “sys”:
One final module to touch on for this introduction is the sys module. This allows you to read arguments given at the CLI and pull them into variable in your script. The syntax is pretty simple, sys.agrv
import sys script = sys.argv ip = sys.argv port = sys.argv print "[+] The script name is: "+script print "[+] The IP is: "+ip+" and the port is: "+port
When this quick script is invoked at the command line with a few arguments it produces the following output:
~$ python sys.py 188.8.131.52 53 [+] The script name is: sys.py [+] The IP is: 184.108.40.206 and the port is: 53
Continue to explore additional Python modules and built-in functions because they’ll allow you to solve problems a lot easier as you begin to write more complex code. The next tutorial will introduce the concept of making network connections with Python by building a basic port scanner.