Pyro4.util — Utilities

Miscellaneous utilities.

class Pyro4.util.JsonSerializer

(de)serializer that wraps the json serialization protocol.

class Pyro4.util.MarshalSerializer

(de)serializer that wraps the marshal serialization protocol.

class Pyro4.util.PickleSerializer

A (de)serializer that wraps the Pickle serialization protocol. It can optionally compress the serialized data, and is thread safe.

class Pyro4.util.SerializerBase

Base class for (de)serializer implementations (which must be thread safe)

classmethod class_to_dict(obj)

Convert a non-serializable object to a dict. Mostly borrowed from serpent.

deserializeCall(data, compressed=False)

Deserializes the given call data back to (object, method, vargs, kwargs) tuple. Set compressed to True to decompress the data first.

deserializeData(data, compressed=False)

Deserializes the given data (bytes). Set compressed to True to decompress the data first.

static dict_to_class(data)

Recreate an object out of a dict containing the class name and the attributes. Only a fixed set of classes are recognized.

serializeCall(obj, method, vargs, kwargs, compress=False)

Serialize the given method call parameters, try to compress if told so. Returns a tuple of the serialized data and a bool indicating if it is compressed or not.

serializeData(data, compress=False)

Serialize the given data object, try to compress if told so. Returns a tuple of the serialized data (bytes) and a bool indicating if it is compressed or not.

class Pyro4.util.SerpentSerializer

(de)serializer that wraps the serpent serialization protocol.

Pyro4.util.excepthook(ex_type, ex_value, ex_tb)

An exception hook you can use for sys.excepthook, to automatically print remote Pyro tracebacks

Pyro4.util.fixIronPythonExceptionForPickle(exceptionObject, addAttributes)

Function to hack around a bug in IronPython where it doesn’t pickle exception attributes. We piggyback them into the exception’s args. Bug report is at http://ironpython.codeplex.com/workitem/30805

Pyro4.util.formatTraceback(ex_type=None, ex_value=None, ex_tb=None, detailed=False)

Formats an exception traceback. If you ask for detailed formatting, the result will contain info on the variables in each stack frame. You don’t have to provide the exception info objects, if you omit them, this function will obtain them itself using sys.exc_info().

Pyro4.util.getPyroTraceback(ex_type=None, ex_value=None, ex_tb=None)

Returns a list of strings that form the traceback information of a Pyro exception. Any remote Pyro exception information is included. Traceback information is automatically obtained via sys.exc_info() if you do not supply the objects yourself.

Pyro4.util.resolveDottedAttribute(obj, attr, allowDotted)

Resolves a dotted attribute name to an object. Raises an AttributeError if any attribute in the chain starts with a ‘_‘. If the optional allowDotted argument is false, dots are not supported and this function operates similar to getattr(obj, attr).

Pyro4.socketutil — Socket related utilities

Low level socket utilities.

class Pyro4.socketutil.SocketConnection(sock, objectId=None)

A wrapper class for plain sockets, containing various methods such as send() and recv()

Pyro4.socketutil.bindOnUnusedPort(sock, host='localhost')

Bind the socket to a free port and return the port number. This code is based on the code in the stdlib’s test.test_support module.

Pyro4.socketutil.createBroadcastSocket(bind=None, reuseaddr=False, timeout=<object object at 0x557e28b0>, ipv6=False)

Create a udp broadcast socket. Set ipv6=True to create an IPv6 socket rather than IPv4. Set ipv6=None to use the PREFER_IP_VERSION config setting.

Pyro4.socketutil.createSocket(bind=None, connect=None, reuseaddr=False, keepalive=True, timeout=<object object at 0x557e28b0>, noinherit=False, ipv6=False, nodelay=False)

Create a socket. Default socket options are keepalive and IPv4 family. If ‘bind’ or ‘connect’ is a string, it is assumed a Unix domain socket is requested. Otherwise, a normal tcp/ip socket is used. Set ipv6=True to create an IPv6 socket rather than IPv4. Set ipv6=None to use the PREFER_IP_VERSION config setting.

Pyro4.socketutil.findProbablyUnusedPort(family=2, socktype=1)

Returns an unused port that should be suitable for binding (likely, but not guaranteed). This code is copied from the stdlib’s test.test_support module.

Pyro4.socketutil.getInterfaceAddress(ip_address)

tries to find the ip address of the interface that connects to the given host’s address

Pyro4.socketutil.getIpAddress(hostname, workaround127=False, ipVersion=None)

Returns the IP address for the given host. If you enable the workaround, it will use a little hack if the ip address is found to be the loopback address. The hack tries to discover an externally visible ip address instead (this only works for ipv4 addresses). Set ipVersion=6 to return ipv6 addresses, 4 to return ipv4, 0 to let OS choose the best one or None to use Pyro4.config.PREFER_IP_VERSION.

Pyro4.socketutil.getIpVersion(hostnameOrAddress)

Determine what the IP version is of the given hostname or ip address (4 or 6). First, it resolves the hostname or address to get an IP address. Then, if the resolved IP contains a ‘:’ it is considered to be an ipv6 address, and if it contains a ‘.’, it is ipv4.

Pyro4.socketutil.hasSelect = True

is poll() available?

Pyro4.socketutil.receiveData(sock, size)

Retrieve a given number of bytes from a socket. It is expected the socket is able to supply that number of bytes. If it isn’t, an exception is raised (you will not get a zero length result or a result that is smaller than what you asked for). The partial data that has been received however is stored in the ‘partialData’ attribute of the exception object.

Pyro4.socketutil.sendData(sock, data)

Send some data over a socket. Some systems have problems with sendall() when the socket is in non-blocking mode. For instance, Mac OS X seems to be happy to throw EAGAIN errors too often. This function falls back to using a regular send loop if needed.

Pyro4.socketutil.setKeepalive(sock)

sets the SO_KEEPALIVE option on the socket, if possible.

Pyro4.socketutil.setNoDelay(sock)

sets the TCP_NODELAY option on the socket (to disable Nagle’s algorithm), if possible.

Pyro4.socketutil.setNoInherit(sock)

Mark the given socket fd as non-inheritable to child processes

Pyro4.socketutil.setReuseAddr(sock)

sets the SO_REUSEADDR option on the socket, if possible.

Pyro4.socketutil.triggerSocket(sock)

send a small data packet over the socket, to trigger it

Pyro4.threadutil — wrapper module for threading

Threading abstraction which allows for threading2 use with a transparent fallback to threading when it is not available. Pyro doesn’t use threading directly: it imports all thread related items via this module instead. Code using Pyro can do the same (but it is not required).