Building Network Web Services with Python

In the previous chapters, we were a consumer of the APIs provided by various tools. In chapter 3, API and Intent-Driven Networking, we saw we can use a HTTP POST method to NX-API at the http://<your router ip>/ins URL with the CLI command embedded in the body to execute commands remotely on the Cisco Nexus device; the device then returns the command execution output in return. In chapter 8, Network Monitoring with Python - Part 2, we used the GET method for our sFlow-RT at http://<your host ip>:8008/version with an empty body to retrieve the version of the sFlow-RT software. These exchanges are examples of RESTful web services.

According to Wikipedia (https://en.wikipedia.org/wiki/Representational_state_transfer), Representational state transfer (REST) or RESTful web services are one way of providing interoperability between computer systems on the Internet. REST-compliant web services allow requesting systems to access and manipulate textual representation of web resources using a uniform and predefined set of stateless operations. As noted, REST web services using the HTTP protocol offer the only method of information exchange on the web; other forms of web services just exist. But it is the most commonly used web service today, with the associated verbs GET, POST, PUT, and DELETE as a predefined way of information exchange.

One of the advantages of using RESTful services is the ability it provides for you to hide your internal operations from the user while still providing them with the service. In the case of the sFlow-RT example, if we were to log in to the device where our software is installed, we wouldn't really know where to check for the software version. By providing the resources in the form of a URL, the software abstracts the version-checking operations from the requester. The abstraction also provides a layer of security, as it can now open up the endpoints as needed.

As the master of the network universe, RESTful web services provide many notable benefits that we can enjoy, such as:

  • You can abstract the requester from learning about the internals of the network operations. For example, we can provide a web service to query the switch version without the requester having to know the exact command.
  • We can consolidate and customize operations that uniquely fit our network, such as a resource to upgrade all our top rack switches.
  • We can provide better security by only exposing the operations as needed. For example, we can provide read-only URLs (GET) to core network devices and read-write URLs (GET / POST / PUT / DELETE) to access switches.

In this chapter, we will use one of the most popular Python web frameworks, namely Flask, to create our own REST web service for our network. In this chapter, we will learn about the following:

  • Comparing Python web frameworks
  • Introduction to Flask
  • Operations involving static network contents
  • Operations involving dynamic network operations

Let's get started by looking at the available Python web frameworks and why we choose Flask.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.162.49