Hands-On Enterprise Automation with Python.
上QQ阅读APP看书,第一时间看更新

Why use Python for network automation?

Python is a pretty well-structured and easy programming language available today and targets many areas in technology, web and internet development, data mining and visualization, desktop GUI, analysis, game building, and automation testing; that's why it's called a general purpose language.

So, there are three reasons to choose Python:

  • Readability and ease of use: When you develop using Python, you actually find yourself writing in English. Many keywords and program flows inside Python are structured to have readable statements. Also, Python doesn't require ; or curly braces to start and end blocks, which gives Python a shallow learning curve. Finally, Python has some optional rules, called PEP 8, that tell Python developers how to format their program to have readable code.

You can configure PyCharm to take care of these rules and check whether your code violates them or not by going to Settings | Inspections | PEP 8 coding style violation:

  • Libraries: This is the real power of Python: libraries and packages. Python has a wide range of libraries in many areas. Any Python developer can easily develop a Python library and upload it online to make it available to other developers. Libraries are uploaded to a website called PyPI (https://pypi.Python.org/pypi) and linked to a GitHub repository. When you want to download the library to your PC, then you use a tool called pip to connect to PyPI and download it locally. Network vendors such as Cisco, Juniper, and Arista developed libraries to facilitate access to their platforms. Most vendors are pushing to make their libraries easy to use and require minimum installation and configuration steps to retrieve useful information from devices.
  • Powerful: Python tries to minimize the number of steps required to reach the end result. For example, to print hello world using Java, you will need this block of code:

However, in Python, the whole block is written in one line to print it, as shown in the following screenshot:

Combining all these reasons together leads to making Python the de facto standard for automation and the first choice for vendors when it comes to automating network devices.