Introduction to Python and NetDNA API
September 11, 2012 | Robert GibbUsing Python programming language to interact with the NetDNA REST API is made easy by using the NetDNA Python client software package. If you have Python already installed in your system, you can use
pipapplication to install the NetDNA python client by invoking the following command in your shell or command line:
$ sudo pip install netdnarwsPip Installs Python (Pip) is an package management system for Python to install and manage libraries and programs. It is synonymous to RubyGems of the the Ruby Programming language and many of the these programs and libraries can be downloaded from the Python Package Index (PyPi), the official third-party software repository for Python. The
pipapplication is the command line tool that will allow you to list installed packages, upgrade packages, and uninstall packages. It is also a replacement for EasyInstall since it doesn't have the capability to list or update locally installed packages. If
pipcommand is not recognized in your command-line or shell, it means that you don't have it installed in your system yet. To install
pip, you can follow the following commands:
$ curl -O http://pypi.python.org/packages/source/p/pip/pip-1.1.tar.gz $ tar xzf pip-1.1.tar.gz $ cd pip-1.1 $ sudo python setup.py installAfter executing the commands above, and no error messages were encountered, the
pipapplication should now be installed.
List Installed PackagesTo list locally installed packages in your system, you can use the following command:
$ pip freeze altgraph==0.9 bdist-mpkg==0.4.4 bonjour-py==0.3The
freezecommand will display all the packages installed on your system with their corresponding version number.
Installing NetDNA Python ClientBefore you can properly install the NetDNA Python client program, some packages need to be present in your system. Make sure that
certifipackages are already installed.
Requestsis an HTTP library written in Python while he
certifipackage is an SSL library. You can install these two packages by running the commands:
$ sudo pip install requests $ sudo pip install certifiOnce you have these two packages installed properly, you can now proceed installing the NetDNA Python client. Just run the following command:
$ sudo pip install netdnarwsAfter this command, you should should now see
netndnarwsin the list of packages after running the
$ pip freeze modulegraph==0.9.1 netdnarws==0.2.1 numpy==1.6.1 py2app==0.6.3
Now that you have the
netdnarwsPython package installed, you can start creating your client for the NetDNA REST API. Just register a new application under your account, and get the values for consumer key, consumer secret, and company alias. Use these values for the following Python code, and replace them with the actual values:
api = NetDNA("company_alias", "consumer_key", "consumer_secret")To test our own client, you can use the following sample Python code to display the JSON responses from different API calls.
from netdnarws import NetDNA api = NetDNA("alias", "90739dhkhf84bf8786388648gd8738v37974", "23edfbdjbdjg3vrvuf4fvrvufyu47") reportstats = api.get("/reports/stats.json") zonestats = api.get("/zones.json/summary") popularfiles = api.get("/reports/popularfiles.json?date_from=2012-07-01&date_to=2012-07-31") statuscodes = api.get("/reports/statuscodes.json/daily") print '[Report Stats]:n', reportstats print '[Zone Stats]:n', zonestats print '[Popular Files]:n', popularfiles print '[Status Codes]:n', statuscodesFor the example code above, we also used the
jsonpackage to display the JSON responses from the API. The
netdnarwspython package also has support for
DELETEOAuth API requests. You can verify the required HTTP request for each method by visiting the API Console. In addition, you may also add an additional debug parameter for every API request you send.
api.get("/account.json", debug=True) # Will output # Making GET request to http://rws.netdna.com/myalias/account.jsonIf your application requires user authentication, you may also initialize the API with additional
api = NetDNA("myalias", "consumer_key", "consumer_secret", token="user_token", token_secret="user_token_secret")You also have an option to send the optional parameter
header_auth, which takes a boolean value to indicate whether to send the OAuth header in the body or URLEncoded.