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
-
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
-
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
-
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
-
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
-
isReady
()¶ Returns True when there is data on the pool or False when not
Return type: boolean
-
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
-
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
-
isReady
()¶ Returns True when there is data on the pool or False when not
Return type: boolean
-
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
-
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