Learning Lab: Hypertext Transfer Protocol (HTTP)

Introduction

One of the objectives of this Learning Lab is to demonstrate how to communicate directly with the Controller using HTTP. In each lesson of this Learning Lab, the sample code invokes functions in the Python library. The library functions communicate with the Controller by sending and receiving HTTP messages. Thus, the library is communicating with the Controller directly and the sample code is communicating with the library.

The structure is thus:

COSC Learning Lab -> lesson -> sample script in Python -> library layer in Python -> HTTP layer -> Controller

The data flow through the HTTP layer is excluded from the lessons but links are provided to pages that contain HTTP Reference Tables. These examples of actual HTTP requests and responses can be used to fulfil the stated objective. Here is an example of a HTTP Reference Table.

The following demonstrations are provided of direct communication with the Controller by HTTP:

  1. using plain Python independently from this Learning Lab.
  2. using curl and wget from the command line

Table of Contents

Prologue

If the Learning Lab team have provided you with either:

  • a Python Interactive Shell, or
  • an IPython Notebook

then please proceed. Otherwise, please follow the instructions on web page: How to set up your computer

Import the Python module named learning_lab.


In [1]:
import learning_lab
from basics.odl_http import http_history, http_history_clear
http_history_clear()
from basics.http import http_history_to_html
from IPython.core.display import HTML

HTTP Reference Table

Produce a reference table describing a HTTP request and response. This table is typical of the HTTP usage throughout this Learning Lab. This particular message was chosen because it is not too lengthy.

See also:

  • Reference table of Large HTTP response
  • Reference table of HTTP POST
  • Reference table of HTTP DELETE

Python code is used to produce the table. An explanation of this code is not relevant. The goal is just to produce the table.


In [2]:
run 01_inventory_mounted


Python Library Documentation: function inventory_mounted in module basics.inventory

inventory_mounted()
    List the names of network devices mounted on the Controller.

['xrvr-1', 'xrvr-2', 'xrvr-999']

In [3]:
HTML(http_history_to_html(http_history()))


Out[3]:
Request
Method GET
URL http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes
Headers
Authorization Basic YWRtaW46YWRtaW4=
Accept-Encoding gzip, deflate, compress
Accept application/xml
User-Agent python-requests/2.2.1 CPython/2.7.6 Linux/3.13.0-44-generic
Content
Response
Status Code 200
Headers
transfer-encoding chunked
content-type application/xml
server Jetty(8.1.14.v20131031)
Content
<?xml version='1.0' encoding='ASCII'?>
<nodes xmlns="urn:opendaylight:inventory">
  <node>
    <id>xrvr-1</id>
  </node>
  <node>
    <id>xrvr-2</id>
  </node>
  <node>
    <id>controller-config</id>
  </node>
  <node>
    <id>xrvr-999</id>
  </node>
</nodes>

In the HTML above there are two tables. The first table is titled 'Request' and the second table is titled 'Response'. The column at the left contains the names of HTTP fields. To the right of each HTTP field name is the field value.

Reproduce HTTP request and response

Demonstrate how to use the information displayed in a reference table to reproduce a HTTP request and response.

HTTP Client

The Controller is a HTTP server. To communicate with a HTTP server requires a HTTP Client. Some HTTP Client tools are:

  • Web Browser - every URL you enter is a HTTP GET request.
  • Web Browser Plugin - for example: Postman
  • Programming Language - for example: Python, Java, etc.
  • Command Line - for example: cURL

A demonstration will be given of the last two tools in the list.

Prepare HTTP request using Python

The field values of the HTTP request are assigned to variables.


In [4]:
from settings import config
address = config['odl_server']['address']
port = config['odl_server']['port']
url = 'http://%s:%s/restconf/config/opendaylight-inventory:nodes' % (address, port)
method = 'GET'
headers = {'Accept' : 'application/xml'}
content = None

Send HTTP request using Python

A HTTP request is delivered, using the variables from the previous step.


In [5]:
from requests import request

response = request(method, url, headers=headers, data=content, auth=('admin', 'admin'))

Receive HTTP response using Python

The response has been received and can be examined. The value of each HTTP field matches the 'Response' table (above).


In [6]:
response.status_code


Out[6]:
200

In [7]:
response.headers


Out[7]:
CaseInsensitiveDict({'transfer-encoding': 'chunked', 'content-type': 'application/xml', 'server': 'Jetty(8.1.14.v20131031)'})

In [8]:
response.content


Out[8]:
'<nodes xmlns="urn:opendaylight:inventory"><node><id>xrvr-1</id></node><node><id>xrvr-2</id></node><node><id>controller-config</id></node><node><id>xrvr-999</id></node></nodes>'

HTTP request and response using CLI

A command line alternative to Python using curl.


In [9]:
!curl -i {url}






{"nodes":{"node":[{"id":"xrvr-1"},{"id":"xrvr-2"},{"id":"controller-config"},{"id":"xrvr-999"}]}}

Notice that the HTTP response content (above) is JSON, not XML, because the 'Accept' header was not specified.

The same URL is requested using wget. This time an 'Accept' header requests a XML response.


In [10]:
!wget -O - --header='Accept: application/xml' --progress=dot {url}


--2015-03-20 12:59:56--  http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes
Connecting to 127.0.0.1:8181... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/xml]
Saving to: ‘STDOUT’
<nodes xmlns="urn:opendaylight:inventory"><node><id>xrvr-1</id></node><node><id>xrvr-2</id></node><node><id>controller-config</id></node><node><id>xrvr-999</id></node></nodes>
     0K                                                        15.3M=0s

2015-03-20 12:59:56 (15.3 MB/s) - written to stdout [175]

Conclusion

Achievements:

  • Display reference table of one HTTP request and response.
  • Explain the reference table layout and contents.
  • Use Python to reproduce the HTTP request and response.
  • Use curl and wget to reproduce the HTTP request and reponse.