Module autobahn.util

autobahn.util.encode_truncate(text, limit, encoding='utf8', return_encoded=True)[source]

Given a string, return a truncated version of the string such that the UTF8 encoding of the string is smaller than the given limit.

This function correctly truncates even in the presence of Unicode code points that encode to multi-byte encodings which must not be truncated in the middle.

Parameters:
  • text (str) – The (Unicode) string to truncate.
  • limit (int) – The number of bytes to limit the UTF8 encoding to.
  • encoding (str) – Truncate the string in this encoding (default is utf-8).
  • return_encoded (bool) – If True, return the string encoded into bytes according to the specified encoding, else return the string as a string.
Returns:

The truncated string.

Return type:

str or bytes

autobahn.util.xor(d1, d2)[source]

XOR two binary strings of arbitrary (equal) length.

Parameters:
  • d1 (binary) – The first binary string.
  • d2 (binary) – The second binary string.
Returns:

XOR of the binary strings (XOR(d1, d2))

Return type:

bytes

autobahn.util.utcstr(ts=None)[source]

Format UTC timestamp in ISO 8601 format.

Note: to parse an ISO 8601 formatted string, use the iso8601 module instead (e.g. iso8601.parse_date("2014-05-23T13:03:44.123Z")).

Parameters:ts (instance of datetime.datetime or None) – The timestamp to format.
Returns:Timestamp formatted in ISO 8601 format.
Return type:str
autobahn.util.utcnow()[source]

Get current time in UTC as ISO 8601 string.

Returns:Current time as string in ISO 8601 format.
Return type:str
autobahn.util.generate_token(char_groups, chars_per_group, chars=None, sep=None, lower_case=False)[source]

Generate cryptographically strong tokens, which are strings like M6X5-YO5W-T5IK. These can be used e.g. for used-only-once activation tokens or the like.

The returned token has an entropy of math.log(len(chars), 2.) * chars_per_group * char_groups bits.

With the default charset and 4 characters per group, generate_token() produces strings with the following entropy:

character groups entropy (at least) recommended use
2 38 bits  
3 57 bits one-time activation or pairing code
4 76 bits secure user password
5 95 bits  
6 114 bits globally unique serial / product code
7 133 bits  

Here are some examples:

  • token(3): 9QXT-UXJW-7R4H
  • token(4): LPNN-JMET-KWEP-YK45
  • token(6): NXW9-74LU-6NUH-VLPV-X6AG-QUE3
Parameters:
  • char_groups (int) – Number of character groups (or characters if chars_per_group == 1).
  • chars_per_group (int) – Number of characters per character group (or 1 to return a token with no grouping).
  • chars (str or None) – Characters to choose from. Default is 27 character subset of the ISO basic Latin alphabet (see: DEFAULT_TOKEN_CHARS).
  • sep (str) – When separating groups in the token, the separater string.
  • lower_case (bool) – If True, generate token in lower-case.
Returns:

The generated token.

Return type:

str

autobahn.util.generate_activation_code()[source]

Generate a one-time activation code or token of the form u'W97F-96MJ-YGJL'. The generated value is cryptographically strong and has (at least) 57 bits of entropy.

Returns:The generated activation code.
Return type:str
autobahn.util.generate_user_password()[source]

Generate a secure, random user password of the form u'kgojzi61dn5dtb6d'. The generated value is cryptographically strong and has (at least) 76 bits of entropy.

Returns:The generated password.
Return type:str
autobahn.util.generate_serial_number()[source]

Generate a globally unique serial / product code of the form u'YRAC-EL4X-FQQE-AW4T-WNUV-VN6T'. The generated value is cryptographically strong and has (at least) 114 bits of entropy.

Returns:The generated serial number / product code.
Return type:str
autobahn.util.rtime()[source]

Precise, fast wallclock time.

Returns:The current wallclock in seconds. Returned values are only guaranteed to be meaningful relative to each other.
Return type:float