tcex.tcex module

TcEx Framework

class tcex.tcex.TcEx[source]

Bases: object

Provides basic functionality for all types of TxEx Apps.


Retrieve Custom Indicator Associations types from the ThreatConnect API.


Send System and App data to logs.


Log the App data information.


Log the current Platform.


Log the current Python version.


Log the proxy settings.


Log the current TcEx version number.

_logger(fh=False, clear_handler=True)[source]

Create TcEx app logger instance.

The logger is accessible via the tc.log.<level> call.

Logging examples

tcex.log.debug('logging debug')'logging info')
tcex.log.warning('logging warning')
tcex.log.error('logging error')
Parameters:(bool, default (stream_only) – False): If True only the Stream handler will be enabled.
Returns:An instance of logging
Return type:logger

Add API logging handler.


Add File logging handler.


Add stream logging handler.


Initialize the resource module.

This method will make a request to the ThreatConnect API to dynamically build classes to support custom Indicators. All other resources are available via this class.


Resource Classes can be accessed using tcex.resources.<Class> or using tcex.resource(‘<resource name>’).

_signal_handler(signal_interupt, frame)[source]

Handle singal interrupt.


Argparser args Namespace.

batch(owner, action=None, attribute_write_type=None, halt_on_error=False, playbook_triggers_enabled=None)[source]

Return instance of Batch

cache(domain, data_type, ttl_minutes=None, mapping=None)[source]

Get instance of the Cache module.

  • domain (str) – The domain can be either “system”, “organization”, or “local”. When using “organization” the data store can be accessed by any Application in the entire org, while “local” access is restricted to the App writing the data. The “system” option should not be used in almost all cases.
  • data_type (str) – The data type descriptor (e.g., tc:whois:cache).
  • ttl_minutes (int) – The number of minutes the cache is valid.

An instance of the Cache Class.

Return type:



Return an instance of the Data Filter Class.

A simple helper module to filter results from ThreatConnect API or other data source. For example if results need to be filtered by an unsupported field the module allows you to pass the data array/list in and specify one or more filters to get just the results required.

Parameters:data (list) – The list of dictionary structure to filter.
Returns:An instance of DataFilter Class
Return type:(object)
datastore(domain, data_type, mapping=None)[source]

Get instance of the DataStore module.

  • domain (str) – The domain can be either “system”, “organization”, or “local”. When using “organization” the data store can be accessed by any Application in the entire org, while “local” access is restricted to the App writing the data. The “system” option should not be used in almost all cases.
  • data_type (str) – The data type descriptor (e.g., tc:whois:cache).

An instance of the DataStore Class.

Return type:



All args parsed before App args are added.


ThreatConnect error codes.

exit(code=None, msg=None)[source]

Application exit method with proper exit code

The method will run the Python standard sys.exit() with the exit code previously defined via exit_code() or provided during the call of this method.

  • code (Optional [integer]) – The exit code value for the app.
  • msg (Optional [string]) – A message to log and add to message tc output.

Return the current exit code.

static expand_indicators(indicator)[source]

Process indicators expanding file hashes/custom indicators into multiple entries.

Parameters:indicator (string) – ” : ” delimited string
Returns:a list of indicators split on ” : “.
Return type:(list)

Returns the object type as a string given a api entity.




Return all defined ThreatConnect Group types.

Returns:A list of ThreatConnect Group types.
Return type:(list)

All supported ThreatConnect Group types.

handle_error(code, message_values=None, raise_error=True)[source]

Raise RuntimeError

  • code (integer) – The error code from API or SDK.
  • message (string) – The error message from API or SDK.

Return ThreatConnect associations type data.

Retrieve the data from the API if it hasn’t already been retrieved.

Returns:A dictionary of ThreatConnect associations types.
Return type:(dictionary)

Return ThreatConnect Indicator types.

Retrieve the data from the API if it hasn’t already been retrieved.

Returns:A list of ThreatConnect Indicator types.
Return type:(list)

Return ThreatConnect indicator types data.

Retrieve the data from the API if it hasn’t already been retrieved.

Returns:A dictionary of ThreatConnect Indicator data.
Return type:(dict)

Return contents of install.json configuration file, loading from disk if required.


Parse params from install.json into a dict by name.

message_tc(message, max_length=255)[source]

Write data to message_tc file in TcEX specified directory.

This method is used to set and exit message in the ThreatConnect Platform. ThreatConnect only supports files of max_message_length. Any data exceeding this limit will be truncated by this method.

Parameters:message (string) – The message to add to message_tc file
metric(name, description, data_type, interval, keyed=False)[source]

Get instance of the Metrics module.

  • name (string) – The name for the metric.
  • description (string) – The description of the metric.
  • data_type (string) – The type of metric: Sum, Count, Min, Max, First, Last, and Average.
  • interval (string) – The metric interval: Hourly, Daily, Weekly, Monthly, and Yearly.
  • keyed (boolean) – Indicates whether the data will have a keyed value.

An instance of the Metrics Class.

Return type:



Get instance of the Notification module.

Returns:An instance of the Notification Class.
Return type:(object)

Instance tcex args parser.


Include the Playbook Module.


Playbook methods can be accessed using tcex.playbook.<method>.


Formats proxy configuration into required format for Python Requests module.

Generates a dictionary for use with the Python Requests module format when proxy is required for remote connections.

Example Response

{"http": "http://user:pass@"}
Returns:Dictionary of proxy settings
Return type:(dictionary)

Argparser args Namespace with Playbook args automatically resolved (resolved args).


Return an instance of the Request Class.

A wrapper on the Python Requests module that provides a different interface for creating requests. The session property of this instance has built-in logging, session level retries, and preconfigured proxy configuration.

Returns:An instance of Request Class
Return type:(object)

Get instance of Resource Class with dynamic type.

Parameters:resource_type – The resource type name (e.g Adversary, User Agent, etc).
Returns:Instance of Resource Object child class.
Return type:(object)
results_tc(key, value)[source]

Write data to results_tc file in TcEX specified directory.

The TcEx platform support persistent values between executions of the App. This method will store the values for TC to read and put into the Database.

  • key (string) – The data key to be stored.
  • value (string) – The data value to be stored.
s(data, errors='strict')[source]

Decode value using correct Python 2/3 method.

This method is intended to replace the to_string() method with better logic to handle poorly encoded unicode data in Python2 and still work in Python3.

  • data (any) – Data to ve validated and (de)encoded
  • errors (string) – What method to use when dealing with errors.

Return decoded data

Return type:


static safe_group_name(group_name, group_max_length=100, ellipsis=True)[source]

Truncate group name to match limit breaking on space and optionally add an ellipsis.


Currently the ThreatConnect group name limit is 100 characters.

  • group_name (string) – The raw group name to be truncated.
  • group_max_length (int) – The max length of the group name.
  • ellipsis (boolean) – If true the truncated name will have ‘…’ appended.

The truncated group name with optional ellipsis.

Return type:


safe_indicator(indicator, errors='strict')[source]

Indicator encode value for safe HTTP request.

  • indicator (string) – Indicator to URL Encode
  • errors (string) – The error handler type.

The urlencoded string

Return type:


static safe_rt(resource_type, lower=False)[source]

Format the Resource Type.

Takes Custom Indicator types with a space character and return a safe string.

(e.g. User Agent is converted to User_Agent or user_agent.)

  • resource_type (string) – The resource type to format.
  • lower (boolean) – Return type in all lower case

The formatted resource type.

Return type:


safe_tag(tag, errors='strict')[source]

URL Encode and truncate tag to match limit (128 characters) of ThreatConnect API.

Parameters:tag (string) – The tag to be truncated
Returns:The truncated tag
Return type:(string)
safe_url(url, errors='strict')[source]

URL encode value for safe HTTP request.

Parameters:url (string) – The string to URL Encode.
Returns:The urlencoded string.
Return type:(string)
safetag(tag, errors='strict')[source]

Wrapper method for safe_tag.

safeurl(url, errors='strict')[source]

Wrapper method for safe_url.


Return an instance of Requests Session configured for the ThreatConnect API.


Include the Threat Intel Module.


Threat Intell methods can be accessed using tcex.ti.<method>.


Include the Utils module.


Utils methods can be accessed using tcex.utils.<method>.