Python Network Programming Techniques
eBook - ePub

Python Network Programming Techniques

  1. 340 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Python Network Programming Techniques

About this book

Become well-versed with network programmability by solving the most commonly encountered problems using Python 3 and open-source packagesKey Features• Explore different Python packages to automate your infrastructure• Leverage AWS APIs and the Python library Boto3 to administer your public cloud network efficiently• Get started with infrastructure automation by enhancing your network programming knowledgeBook DescriptionNetwork automation offers a powerful new way of changing your infrastructure network. Gone are the days of manually logging on to different devices to type the same configuration commands over and over again. With this book, you'll find out how you can automate your network infrastructure using Python.You'll get started on your network automation journey with a hands-on introduction to the network programming basics to complement your infrastructure knowledge. You'll learn how to tackle different aspects of network automation using Python programming and a variety of open source libraries. In the book, you'll learn everything from templating, testing, and deploying your configuration on a device-by-device basis to using high-level REST APIs to manage your cloud-based infrastructure. Finally, you'll see how to automate network security with Cisco's Firepower APIs.By the end of this Python network programming book, you'll have not only gained a holistic overview of the different methods to automate the configuration and maintenance of network devices, but also learned how to automate simple to complex networking tasks and overcome common network programming challenges.What you will learn• Programmatically connect to network devices using SSH (secure shell) to execute commands• Create complex configuration templates using Python• Manage multi-vendor or multi-device environments using network controller APIs or unified interfaces• Use model-driven programmability to retrieve and change device configurations• Discover how to automate post modification network infrastructure tests• Automate your network security using Python and Firepower APIsWho this book is forThis book is for network engineers who want to make the most of Python to automate their infrastructure. A basic understanding of Python programming and common networking principles is necessary.

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access Python Network Programming Techniques by Marcel Neidinger in PDF and/or ePUB format, as well as other popular books in Computer Science & Computer Networking. We have over one million books available in our catalogue for you to explore.

Information

Chapter 1: A Primer on Python 3

Since its development in the early 1990s, Python has become one of the most widely used programming languages in the world. Its easy-to-use yet powerful syntax and cross-platform availability, combined with a rich ecosystem of pre-built functionality, have turned it into one of the most widely used languages in the realm of infrastructure automation. From simple automation scripts to complex web applications, Python can handle a wide variety of tasks.
In this chapter, we are going to review some basic concepts of the Python programming language before using these skills in the following chapters to automate our infrastructure.
While not being a full introduction to the Python language, this chapter should serve as a refresher for the concepts we are going to use in the automation script in later chapters. If you are already proficient with Python, you can skip ahead to the next chapter.
Specifically, you will learn about the following concepts in this chapter:
  • Assigning variables in Python
  • Converting between data types in Python
  • Looping over lists in Python
  • Controlling the flow of a Python program using if statements
  • Executing code until a condition is met using while loops
  • Writing reusable code with functions
  • Storing and accessing key-value pairs using dictionaries
  • Importing modules from the standard library
  • Installing modules from the Python Package Index (PyPI)

Technical requirements

For this section and for the remainder of the book, you'll need an installation of Python—specifically, you'll need a Python interpreter of version 3.6.1 or higher. This book makes use of language constructs of Python 3 and thus is incompatible with Python 2.x.
You will also require a code editor. Popular choices include Microsoft Visual Studio Code (VS Code) or Notepad++.
While the code for this book was written on a Mac with macOS X version 10.15 installed, we don't use any packages that behave differently on Windows or Linux, and all code samples should behave the same.
You can view this chapter's code in action here: https://bit.ly/3fW3EsK

Assigning variables in Python

Variables are named placeholders we can use to store and manipulate data. Variables point to a portion of the memory in which the data is stored. The kind of data that is stored within the memory portion that a variable is pointing to is known as the data type of that variable. These data types can be, for example, a string for text or an integer for a whole number. While some programming languages (such as C++ or Java) require you as the developer to explicitly define what kind of data you want to put into a variable, Python is dynamically typed. This means that the programming language will take care of assigning the correct data type to your variable, based on what you are putting in.
In this recipe, you will see how to assign variables in Python using the = operator and see the basic data types Python offers.

Getting ready

Open your code editor and start by creating a file called variables.py. Next, navigate your terminal to the same directory in which you just created the variables.py file.

How to do it…

Let's start by assigning some variables containing text (also referred to as a string), whole and floating-point numbers (in the form of integers and floats), truth values (known as Booleans), and lists that hold a list of variables:
  1. Define a variable called my_hostname and assign it the value of your name using the = operator:
    my_hostname = "router01"
  2. We can also use single quotes to assign a string. Note that you can't mix the different types of quotes:
    my_domain = 'example.com'
  3. Furthermore, we can use triple quotes to define a string that spans multiple lines:
    my_motd = """
    This is a string that will
    contain linebreaks and could be the motd of a
    router.
    """
  4. If we want to see the value of a variable, we can use Python's built-in print() function to display the value stored on the command line:
    print(my_hostname)
    print(my_domain)
    print(my_motd)
  5. Similar to strings, we can also assign numeric values. We can distinguish two types of variables: floats for floating-point numbers and integers for whole numbers. Define a variable called my_port to hold the default port number of a Secure Shell (SSH) server (as an integer/whole number) and a variable called my_throughput to hold your server's average number of requests per minute (as a float/floating-point number):
    my_port = 22
    my_throughput = 1.75
  6. We can print out the value of a variable of the type int or float, the same way we did with the string:
    print(my_port)
    print(my_throughput)
  7. Truth values in Python are represented by the two True and False Boolean values. We can define a variable called knows_python, assign it to either be true or false, and then print the value of said variable:
    knows_python = True
    knows_python = False
    print(knows_python)
  8. Sometimes, we want to store lists (also referre...

Table of contents

  1. Python Network Programming Techniques
  2. Contributors
  3. Preface
  4. Chapter 1: A Primer on Python 3
  5. Chapter 2: Connecting to Network Devices via SSH Using Paramiko
  6. Chapter 3: Building Configuration Templates Using Jinja2
  7. Chapter 4: Configuring Network Devices Using Netmiko
  8. Chapter 5: Model-Driven Programmability with NETCONF and ncclient
  9. Chapter 6: Automating Complex Multi-Vendor Networks with NAPALM
  10. Chapter 7: Automating Your Network Tests and Deployments with pyATS and Genie
  11. Chapter 8: Configuring Devices Using RESTCONF and requests
  12. Chapter 9: Consuming Controllers and High-Level Networking APIs with requests
  13. Chapter 10: Incorporating your Python Scripts into an Existing Workflow by Writing Custom Ansible Modules
  14. Chapter 11: Automating AWS Cloud Networking Infrastructure Using the AWS Python SDK
  15. Chapter 12: Automating your Network Security Using Python and the Firepower APIs
  16. Other Books You May Enjoy