Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Aside from using an external tool to simulate the kind of network you're interested in, one good approach is to use a substitute implementation of socket.</p> <p>This involves making the socket construction a parameter to your function, instead of importing the socket module and using it directly. For normal operation, you will pass in the real socket type, but when you want to test various adverse network conditions, you can pass in an implementation that simulates those conditions. For example, you might create a socket type which parameterizes latency and bandwidth (untested code, beware):</p> <pre><code>import time, socket class ControllableSocket: def __init__(self, latency, bandwidth): self._latency = latency self._bandwidth = bandwidth self._bytesSent = 0 self._timeCreated = time.time() self._socket = socket.socket() def send(self, bytes): now = time.time() connectionDuration = now - self._timeCreated self._bytesSent += len(bytes) # How long should it have taken to send how many bytes we've sent with our # given bandwidth limitation? requiredDuration = self._bytesSent / self._bandwidth time.sleep(max(requiredDuration - connectionDuration, self._latency)) return self._socket.send(bytes) </code></pre> <p>If you implement the other socket methods, connect, recv, etc, you can substitute an instance of this class for an instance of the real socket type. This leaves all the rest of your program completely free of any knowledge of your simulation, simplifying it, while also letting you try out many different network configurations by just implementing a new socket type that simulates them.</p> <p>This idea is one of the reasons Twisted explicitly separates the idea of "protocols" - objects which know how to interpret bytes from the network and generate new bytes to send to the network - from "transports" - objects which know how to get bytes off the network and put bytes onto it. The separation eases testing and allows novel configurations like this one, where a simulation of some other network conditions (which may be difficult to produce for real) is provided by the transport.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload