3.4. steelscript.common.connection

3.4.1. Connection Objects

class steelscript.common.connection.Connection(hostname, auth=None, port=None, verify=True, reauthenticate_handler=None)

Handle authentication and communication to remote machines.

__init__(hostname, auth=None, port=None, verify=True, reauthenticate_handler=None)

Initialize new connection and setup authentication

hostname - include protocol, e.g. “https://host.comauth - authentication object, see below port - optional port to use for connection verify - require SSL certificate validation.

Authentication: For simple basic auth, passing a tuple of (user, pass) is sufficient as a shortcut to an instance of HTTPBasicAuth. This auth method will trigger a check to ensure the protocol is using SSL to connect (though cert verification may still be turned off to avoid errors with self-signed certs).

OAuth2 will require the requests-oauthlib package and an instance of the OAuth2Session object.

netrc config files will be checked if auth is left as None. If no authentication is provided for the hostname in the netrc file, or no file exists, an error will be raised when trying to connect.

class JsonEncoder(skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, encoding='utf-8', default=None)

Handle more object types if first encoding doesn’t work.

Connection.download(url, path=None, overwrite=False, method='GET', extra_headers=None, params=None)

Download a file from a remote URI and save it to a local path.

url is the url of the file to download.

path is an optional path on the local filesystem to save the downloaded file. It can be:

  • a complete path
  • a directory

In the first case the file will have the specified name and extension. In the second case the filename will be retrieved by the ‘Content-Disposition’ HTTP header. If a path cannot be determined, a ValueError is raised.

overwrite if True will save the downloaded file to path no matter
if the file already exists.

method is the HTTP method used for the request.

extra_headers is a dictionary of headers to use for the request.

params is a dictionary of parameters for the request.

Connection.get_url(path)

Returns a fully qualified URL given a path.

Connection.json_request(method, path, body=None, params=None, extra_headers=None, raw_response=False)

Send a JSON request and receive JSON response.

Connection.upload(path, data, method='POST', params=None, extra_headers=None)

Upload raw data to the given URL path with the given content type.

data may be either a string or a python file object.

extra_headers is a dictionary of additional HTTP headers to send
with the request (e.g. Content-Type, Content-Disposition)
params is a dictionary of URL parameters to attach to the request.
The keys and values will be urlencoded.
method defaults to “POST”, but can be overridden if the API requires
another method such as “PUT” to be used instead.

Returns location information if resource has been created, otherwise the response body (if any).

Connection.upload_file(path, files, body=None, params=None, extra_headers=None, file_headers=None, field_name='file', raw_response=False)

Executes a POST to upload a file or files.

Parameters:
  • path – The full or relative URL of the file upload API
  • files – Can be a string that is the full path to a file to be uploaded OR it can be a tuple/list of strings that are each the full path to a file to be uploaded.
  • body – Optional body. If present must be a dictionary.
  • params – optional URL params
  • extra_headers – Optional headers
  • file_headers – Optional headers to include with the multipart file data. Default is {‘Expires’: ‘0’}. Pass in an empty dict object if you would not like to include any file_headers in the multipart data.
  • field_name – The name of the form field on the destination that will receive the posted multipart data. Default is ‘file’
  • raw_response – False (defualt) results in the function returning only the decoded JSON response present in the response body. If set to True then the funciton will return a tuple of the decoded JSON body and the full response object. Set to True if you want to inspect the result code or response headers.
Returns:

See ‘raw_response’ for details on the returned data.

Connection.urlencoded_request(method, path, body=None, params=None, extra_headers=None, raw_response=False)

Send a request with url encoded parameters in body

Connection.xml_request(method, path, body=None, params=None, extra_headers=None, raw_response=False)

Send an XML request to the host.

The Content-Type and Accept headers are set to text/xml. In addition, any response will be XML-decoded as an xml.etree.ElementTree. The body is assumed to be an XML encoded text string and is inserted into the HTTP payload as-is.