util

Utility package with various useful functions

steam.util.ip_from_int(ip)

Convert IP to int

Parameters:ip (str) – IP in dot-decimal notation
Return type:int
steam.util.ip_to_int(ip)

Convert int to IP

Parameters:ip (int) – int representing an IP
Returns:IP in dot-decimal notation
Return type:str
steam.util.is_proto(emsg)
Parameters:emsg (int) – emsg number
Returns:True or False
Return type:bool
steam.util.set_proto_bit(emsg)
Parameters:emsg (int) – emsg number
Returns:emsg with proto bit set
Return type:int
steam.util.clear_proto_bit(emsg)
Parameters:emsg (int) – emsg number
Returns:emsg with proto bit removed
Return type:int
steam.util.proto_to_dict(message)

Converts protobuf message instance to dict

Parameters:message – protobuf message instance
Returns:parameters and their values
Return type:dict
Raises:TypeError if message is not a proto message
steam.util.proto_fill_from_dict(message, data, clear=True)

Fills protobuf message parameters inplace from a dict

Parameters:
  • message – protobuf message instance
  • data (dict) – parameters and values
  • clear (bool) – whether clear exisiting values
Returns:

value of message paramater

Raises:

incorrect types or values will raise

steam.util.chunks(arr, size)

Splits a list into chunks

Parameters:
  • arr (list) – list to split
  • size (int) – number of elements in each chunk
Returns:

generator object

Return type:

generator

class steam.util.WeakRefKeyDict

Bases: object

Pretends to be a dictionary. Use any object (even unhashable) as key and store a value. Once the object is garbage collected, the entry is destroyed automatically.

class steam.util.WeakRefCallback(refs, key)

Bases: object

util.binary

class steam.util.binary.StructReader(data)

Bases: object

Simplifies parsing of struct data from bytes

Parameters:data (bytes) – data bytes
rlen()

Number of remaining bytes that can be read

Returns:number of remaining bytes
Return type:int
read(n=1)

Return n bytes

Parameters:n (int) – number of bytes to return
Returns:bytes
Return type:bytes
read_cstring(terminator='\x00')

Reads a single null termianted string

Returns:string without bytes
Return type:bytes
unpack(format_text)

Unpack bytes using struct modules format

Parameters:format_text (str) – struct’s module format
Return data:result from struct.unpack_from()
Return type:tuple
skip(n)

Skips the next n bytes

Parameters:n (int) – number of bytes to skip

util.throttle

class steam.util.throttle.ConstantRateLimit(times, seconds, exit_wait=False, use_gevent=False)

Bases: object

Context manager for enforcing constant rate on code inside the block .

rate = seconds / times

Parameters:
  • times (int) – times to execute per...
  • seconds (int) – ...seconds
  • exit_wait (bool) – whether to automatically call wait() before exiting the block
  • use_gevent (bool) – whether to use gevent.sleep() instead of time.sleep()

Example:

with RateLimiter(1, 5) as r:
    # code taking 1s
    r.wait()  # blocks for 4s
    # code taking 7s
    r.wait()  # doesn't block
    # code taking 1s
    r.wait()  # blocks for 4s
wait()

Blocks until the rate is met

util.web

steam.util.web.make_requests_session()
Returns:requests session
Return type:requests.Session
steam.util.web.generate_session_id()
Returns:session id
Return type:str