Network – network utility module

In this module you’ll find all the network related implementation

New in version 0.6: The Network module.

class Network.UDPThreadBroadcastClient(host, port, target_port)

The Broadcast UDP client thread class.

This class is a thread to serve as Pyevolve client on the UDP datagrams, it is used to send data over network lan/wan.

Example:
>>> s = Network.UDPThreadClient('192.168.0.2', 1500, 666)
>>> s.setData("Test data")
>>> s.start()
>>> s.join()
Parameters:
  • host – the hostname to bind the socket on sender (this is NOT the target host)
  • port – the sender port (this is NOT the target port)
  • target_port – the destination port target
close()

Close the internal socket

daemon

A boolean value indicating whether this thread is a daemon thread (True) or not (False).

This must be set before start() is called, otherwise RuntimeError is raised. Its initial value is inherited from the creating thread; the main thread is not a daemon thread and therefore all threads created in the main thread default to daemon = False.

The entire Python program exits when no alive non-daemon threads are left.

getData()

Get the data to send

Return type:data to send
getSentBytes()

Returns the number of sent bytes. The use of this method makes sense when you already have sent the data

Return type:sent bytes
ident

Thread identifier of this thread or None if it has not been started.

This is a nonzero integer. See the thread.get_ident() function. Thread identifiers may be recycled when a thread exits and another thread is created. The identifier is available even after the thread has exited.

isAlive()

Return whether the thread is alive.

This method returns True just before the run() method starts until just after the run() method terminates. The module function enumerate() returns a list of all alive threads.

is_alive()

Return whether the thread is alive.

This method returns True just before the run() method starts until just after the run() method terminates. The module function enumerate() returns a list of all alive threads.

join(timeout=None)

Wait until the thread terminates.

This blocks the calling thread until the thread whose join() method is called terminates – either normally or through an unhandled exception or until the optional timeout occurs.

When the timeout argument is present and not None, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof). As join() always returns None, you must call isAlive() after join() to decide whether a timeout happened – if the thread is still alive, the join() call timed out.

When the timeout argument is not present or None, the operation will block until the thread terminates.

A thread can be join()ed many times.

join() raises a RuntimeError if an attempt is made to join the current thread as that would cause a deadlock. It is also an error to join() a thread before it has been started and attempts to do so raises the same exception.

name

A string used for identification purposes only.

It has no semantics. Multiple threads may be given the same name. The initial name is set by the constructor.

run()

Method called when you call .start() of the thread

send()

Broadcasts the data

setData(data)

Set the data to send

Parameters:data – the data to send
start()

Start the thread’s activity.

It must be called at most once per thread object. It arranges for the object’s run() method to be invoked in a separate thread of control.

This method will raise a RuntimeError if called more than once on the same thread object.

class Network.UDPThreadServer(host, port, poolSize=10, timeout=3)

The UDP server thread class.

This class is a thread to serve as Pyevolve server on the UDP datagrams, it is used to receive data from network lan/wan.

Example:
>>> s = UDPThreadServer("192.168.0.2", 666, 10)
>>> s.start()
>>> s.shutdown()
Parameters:
  • host – the host to bind the server
  • port – the server port to bind
  • poolSize – the size of the server pool
  • timeout – the socket timeout

Note

this thread implements a pool to keep the received data, the poolSize parameter specifies how much individuals we must keep on the pool until the popPool method is called; when the pool is full, the sever will discard the received individuals.

close()

Closes the internal socket

daemon

A boolean value indicating whether this thread is a daemon thread (True) or not (False).

This must be set before start() is called, otherwise RuntimeError is raised. Its initial value is inherited from the creating thread; the main thread is not a daemon thread and therefore all threads created in the main thread default to daemon = False.

The entire Python program exits when no alive non-daemon threads are left.

getBufferSize()

Gets the current receive buffer size

Return type:integer
getData()

Calls the socket recvfrom method and waits for the data, when the data is received, the method will return a tuple with the IP of the sender and the data received. When a timeout exception occurs, the method return None.

Return type:tuple (sender ip, data) or None when timeout exception
ident

Thread identifier of this thread or None if it has not been started.

This is a nonzero integer. See the thread.get_ident() function. Thread identifiers may be recycled when a thread exits and another thread is created. The identifier is available even after the thread has exited.

isAlive()

Return whether the thread is alive.

This method returns True just before the run() method starts until just after the run() method terminates. The module function enumerate() returns a list of all alive threads.

isReady()

Returns True when there is data on the pool or False when not

Return type:boolean
is_alive()

Return whether the thread is alive.

This method returns True just before the run() method starts until just after the run() method terminates. The module function enumerate() returns a list of all alive threads.

join(timeout=None)

Wait until the thread terminates.

This blocks the calling thread until the thread whose join() method is called terminates – either normally or through an unhandled exception or until the optional timeout occurs.

When the timeout argument is present and not None, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof). As join() always returns None, you must call isAlive() after join() to decide whether a timeout happened – if the thread is still alive, the join() call timed out.

When the timeout argument is not present or None, the operation will block until the thread terminates.

A thread can be join()ed many times.

join() raises a RuntimeError if an attempt is made to join the current thread as that would cause a deadlock. It is also an error to join() a thread before it has been started and attempts to do so raises the same exception.

name

A string used for identification purposes only.

It has no semantics. Multiple threads may be given the same name. The initial name is set by the constructor.

poolLength()

Returns the size of the pool

Return type:integer
popPool()

Return the last data received on the pool

Return type:object
run()

Called when the thread is started by the user. This method is the main of the thread, when called, it will enter in loop to wait data or shutdown when needed.

setBufferSize(size)

Sets the receive buffer size

Parameters:size – integer
shutdown()

Shutdown the server thread, when called, this method will stop the thread on the next socket timeout

start()

Start the thread’s activity.

It must be called at most once per thread object. It arranges for the object’s run() method to be invoked in a separate thread of control.

This method will raise a RuntimeError if called more than once on the same thread object.

class Network.UDPThreadUnicastClient(host, port, pool_size=10, timeout=0.5)

The Unicast UDP client thread class.

This class is a thread to serve as Pyevolve client on the UDP datagrams, it is used to send data over network lan/wan.

Example:
>>> s = Network.UDPThreadClient('192.168.0.2', 1500)
>>> s.setData("Test data")
>>> s.setTargetHost('192.168.0.50', 666)
>>> s.start()
>>> s.join()
Parameters:
  • host – the hostname to bind the socket on sender (this is not the target host)
  • port – the sender port (this is not the target port)
  • pool_size – the size of send pool
  • timeout – the time interval to check if the client have data to send
addData(data)

Set the data to send

Parameters:data – the data to send
close()

Close the internal socket

daemon

A boolean value indicating whether this thread is a daemon thread (True) or not (False).

This must be set before start() is called, otherwise RuntimeError is raised. Its initial value is inherited from the creating thread; the main thread is not a daemon thread and therefore all threads created in the main thread default to daemon = False.

The entire Python program exits when no alive non-daemon threads are left.

ident

Thread identifier of this thread or None if it has not been started.

This is a nonzero integer. See the thread.get_ident() function. Thread identifiers may be recycled when a thread exits and another thread is created. The identifier is available even after the thread has exited.

isAlive()

Return whether the thread is alive.

This method returns True just before the run() method starts until just after the run() method terminates. The module function enumerate() returns a list of all alive threads.

isReady()

Returns True when there is data on the pool or False when not

Return type:boolean
is_alive()

Return whether the thread is alive.

This method returns True just before the run() method starts until just after the run() method terminates. The module function enumerate() returns a list of all alive threads.

join(timeout=None)

Wait until the thread terminates.

This blocks the calling thread until the thread whose join() method is called terminates – either normally or through an unhandled exception or until the optional timeout occurs.

When the timeout argument is present and not None, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof). As join() always returns None, you must call isAlive() after join() to decide whether a timeout happened – if the thread is still alive, the join() call timed out.

When the timeout argument is not present or None, the operation will block until the thread terminates.

A thread can be join()ed many times.

join() raises a RuntimeError if an attempt is made to join the current thread as that would cause a deadlock. It is also an error to join() a thread before it has been started and attempts to do so raises the same exception.

name

A string used for identification purposes only.

It has no semantics. Multiple threads may be given the same name. The initial name is set by the constructor.

poolLength()

Returns the size of the pool

Return type:integer
popPool()

Return the last data received on the pool

Return type:object
run()

Method called when you call .start() of the thread

send(data)

Send the data

Parameters:data – the data to send
Return type:bytes sent to each destination
setMultipleTargetHost(address_list)

Sets multiple host/port targets, the destinations

Parameters:address_list – a list with tuples (ip, port)
setTargetHost(host, port)

Set the host/port of the target, the destination

Parameters:
  • host – the target host
  • port – the target port

Note

the host will be ignored when using broadcast mode

shutdown()

Shutdown the server thread, when called, this method will stop the thread on the next socket timeout

start()

Start the thread’s activity.

It must be called at most once per thread object. It arranges for the object’s run() method to be invoked in a separate thread of control.

This method will raise a RuntimeError if called more than once on the same thread object.

Network.getMachineIP()

Return all the IPs from current machine.

Example:
>>> Util.getMachineIP()
['200.12.124.181', '192.168.0.1']      
Return type:a python list with the string IPs
Network.pickleAndCompress(obj, level=9)

Pickles the object and compress the dumped string with zlib

Parameters:
  • obj – the object to be pickled
  • level – the compression level, 9 is the best and -1 is to not compress
Network.unpickleAndDecompress(obj_dump, decompress=True)

Decompress a zlib compressed string and unpickle the data

Parameters:obj – the object to be decompressend and unpickled