943
class SocketDelay(object):
944
"""A socket decorator to make TCP appear slower.
946
This changes recv, send, and sendall to add a fixed latency to each
947
python call. It will therefore behave differently to the real underlying
948
TCP stack which may only pay the latency price on each actual roundtrip
949
rather than each packet initiated. We can come closer to the real
950
behaviour by only charging latency for each roundtrip we detect - that is
951
when recv is called, toggle a flag, and insert latency again only when
952
send has been called in the middle. For now though, showing more latency
953
than we have is probably acceptable.
955
Not all methods are implemented, this is deliberate as this class is not
956
a replacement for the builtin sockets layer. fileno is not implemented to
957
prevent the proxy being bypassed.
960
def __init__(self, sock, latency):
962
self.latency = latency
965
return self.sock.close()
968
return SocketDelay(self.sock.dup(), self.latency)
970
def getpeername(self, *args):
971
return self.sock.getpeername(*args)
973
def getsockname(self, *args):
974
return self.sock.getsockname(*args)
976
def getsockopt(self, *args):
977
return self.sock.getsockopt(*args)
979
def gettimeout(self, *args):
980
return self.sock.gettimeout(*args)
982
def recv(self, *args):
983
data = self.sock.recv(*args)
985
time.sleep(self.latency)
988
def sendall(self, *args):
989
time.sleep(self.latency)
990
return self.sock.sendall(*args)
992
def send(self, *args):
993
time.sleep(self.latency)
994
return self.sock.send(*args)
996
def setblocking(self, *args):
997
return self.sock.setblocking(*args)
999
def setsockopt(self, *args):
1000
return self.sock.setsockopt(*args)
1002
def settimeout(self, *args):
1003
return self.sock.settimeout(*args)
1005
def shutdown(self, *args):
1006
return self.sock.shutdown(*args)
943
1009
class SFTPServer(Server):
944
1010
"""Common code for SFTP server facilities."""
961
1028
"""StubServer uses this to log when a new server is created."""
962
1029
self.logs.append(message)
1031
def _run_server_entry(self, sock):
1032
"""Entry point for all implementations of _run_server.
1034
If self.add_latency is > 0.000001 then sock is given a latency adding
1037
if self.add_latency > 0.000001:
1038
sock = SocketDelay(sock, self.add_latency)
1039
return self._run_server(sock)
964
1041
def _run_server(self, s):
965
1042
ssh_server = paramiko.Transport(s)
966
1043
key_file = pathjoin(self._homedir, 'test_rsa.key')