libtaxii.clients Module

Version: 1.1.119


Classes

class libtaxii.clients.HttpClient(auth_type=0, auth_credentials=None, use_https=False)[source]
call_taxii_service2(host, path, message_binding, post_data, port=None, get_params_dict=None, content_type=None, headers=None, user_agent=None, timeout=None)[source]

Call a TAXII service.

Note: this uses urllib2 instead of httplib, and therefore returns a different kind of object than call_taxii_service().

Returns:urllib2.Response
set_auth_credentials(auth_credentials_dict)[source]

Set the authentication credentials used later when making a request.

Note that it is possible to pass in one dict containing credentials for different authentication types and swap between them later.

Parameters:dict (auth_credentials_dict) – The dictionary containing authentication credentials. e.g.: - {‘key_file’: ‘/path/to/key.key’, ‘cert_file’: ‘/path/to/cert.crt’} - {‘username’: ‘abc’, ‘password’: ‘xyz’} - Or both, if both username/password and certificate based auth are used
set_auth_type(auth_type)[source]

Set the authentication type for this client.

Parameters:auth_type (string) – Must be one of AUTH_NONE, AUTH_BASIC, or AUTH_CERT
set_proxy(proxy_string=None)[source]

Set the proxy settings to use when making a connection.

Parameters:proxy_string (string) – Proxy address formatted like http://proxy.example.com:80. Set to SYSTEM_PROXY to use the system proxy; set to NO_PROXY to use no proxy.
set_use_https(bool_)[source]

Indicate whether the HttpClient should use HTTP or HTTPs. The default is HTTP.

Parameters:bool (bool) – The new use_https value.
set_verify_server(verify_server=False, ca_file=None)[source]

Tell libtaxii whether to verify the server’s ssl certificate using the provided ca_file.

Parameters:verify_server (bool) – Flag indicating whether or not libtaxii should verify the server.

Examples

TAXII clients have three types of authentication credentials: None, HTTP Basic, and TLS Certificate. This section demonstrates usage of all three auth types.

All examples assume the following imports:

import libtaxii as t
import libtaxii.messages_11 as tm11
import libtaxii.clients as tc
from libtaxii.common import generate_message_id
from libtaxii.constants import *
from dateutil.tz import tzutc

Using No Credentials

client = tc.HttpClient()
client.set_auth_type(tc.HttpClient.AUTH_NONE)
client.set_use_https(False)

discovery_request = tm11.DiscoveryRequest(generate_message_id())
discovery_xml = discovery_request.to_xml(pretty_print=True)

http_resp = client.call_taxii_service2('hailataxii.com', '/taxii-discovery-service', VID_TAXII_XML_11, discovery_xml)
taxii_message = t.get_message_from_http_response(http_resp, discovery_request.message_id)
print taxii_message.to_xml(pretty_print=True)

Using Basic HTTP Auth

client = tc.HttpClient()
client.set_auth_type(tc.HttpClient.AUTH_BASIC)
client.set_auth_credentials({'username': 'guest', 'password': 'guest'})

discovery_request = tm11.DiscoveryRequest(generate_message_id())
discovery_xml = discovery_request.to_xml(pretty_print=True)

http_resp = client.call_taxii_service2('hailataxii.com', '/taxii-discovery-service', VID_TAXII_XML_11, discovery_xml)
taxii_message = t.get_message_from_http_response(http_resp, discovery_request.message_id)
print taxii_message.to_xml(pretty_print=True)

Using TLS Certificate Auth

Note: The following code is provided as an example of this authentication method, but will not work as-is, because Hail A Taxii does not support TLS.

client = tc.HttpClient()
client.set_use_https(True)
client.set_auth_type(tc.HttpClient.AUTH_CERT)
client.set_auth_credentials({'key_file': '../PATH_TO_KEY_FILE.key', 'cert_file': '../PATH_TO_CERT_FILE.crt'})

discovery_request = tm11.DiscoveryRequest(generate_message_id())
discovery_xml = discovery_request.to_xml(pretty_print=True)

http_resp = client.call_taxii_service2('hailataxii.com', '/taxii-discovery-service/', VID_TAXII_XML_11, discovery_xml)
taxii_message = t.get_message_from_http_response(http_resp, discovery_request.message_id)
print taxii_message.to_xml(pretty_print=True)