cancel
Showing results for 
Search instead for 
Did you mean: 

Tech Tip: Using the RESTful Control API with Python - addextrafile

The following code uses Stingray's RESTful API to a file to the extra directory.   The code is written in Python.  This program adds the file 'validserialnumbers' to the extra directory and if the file already exists it will be overwrite it.  If this is not the desired behavior, code can be added to to check for the existence of the file as was done in the addpool example.

 

addextrafile.py

 

#! /usr/bin/env python
import requests
import json
import sys

fileName = 'validserialnumbers'
url = 'https://stingray.example.com:9070/api/tm/1.0/config/active/extra/' + fileName
contentType = {'content-type': 'application/octet-stream'}

validSerialNumbers = """\ 123456 234567 345678""" client = requests.Session() client.auth = ('admin', 'admin') client.verify = False try: response = client.put(url, data = validSerialNumbers, headers = contentType) except requests.exceptions.ConnectionError: print "Error: Unable to connect to " + url sys.exit(1) # If the file already exists, it will be replaced with this version and 204 will be returned # otherwise 201 will be returned. if response.status_code == 201 or response.status_code == 204: print "File %s added" %(fileName) else: error = json.loads(response.content) print "Error adding file %s: url=%s Status=%d Id=%s: %s" %(fileName, url, response.status_code, error['error_id'], error['error_text'])

 

 

Running the example

 

This code was tested with Python 2.7.3 and version 1.1.0 of the requests library.

 

Run the Python script as follows:

 

$ addextrafile.py

File added

 

Notes

 

Since this is a file and not a configuration resource, JSON will not be used and the MIME type will be "application/octet-stream".  Another difference when dealing with files is how Stingray handles adding a file that already exists.  If the file already exists, Stingray will overwrite the it and return a HTTP status code of 204.  If the file doesn't already exist, the HTTP status code will be a 201.

 

Read More

 

 

Version history
Revision #:
3 of 3
Last update:
‎01-06-2021 04:40:PM
Updated by:
 
Labels (1)
Contributors