2 # Copyright 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 # Simple client/server script for generating an unlimited TCP stream.
7 # see shadow.sh for how it's intended to be used.
20 tmp
= socket
.recv(4096)
29 tmp
= socket
.send(data
)
37 last_report
= time
.time()
44 received_now
= received
45 delta
= now
- last_report
46 sent_mbps
= ((sent_now
- last_sent
) * 8.0 / 1000000) / delta
47 received_mbps
= ((received_now
- last_received
) * 8.0 / 1000000) / delta
48 print "Sent: %5.2f mbps Received: %5.2f mbps" % (sent_mbps
, received_mbps
)
51 last_received
= received_now
53 def Serve(socket
, upload
=True, download
=True):
55 (s
, addr
) = socket
.accept()
57 thread
.start_new_thread(Spew
, (s
,))
59 thread
.start_new_thread(Sink
, (s
,))
61 def Receiver(port
, upload
=True, download
=True):
62 s
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
63 s
.setsockopt(socket
.SOL_SOCKET
, socket
.SO_REUSEADDR
, 1)
66 thread
.start_new_thread(Serve
, (s
, upload
, download
))
69 def Connect(to_hostport
, upload
=True, download
=False):
70 s
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
71 s
.setsockopt(socket
.SOL_SOCKET
, socket
.SO_REUSEADDR
, 1)
72 s
.connect(to_hostport
)
74 thread
.start_new_thread(Spew
, (s
,))
76 thread
.start_new_thread(Sink
, (s
,))
81 print "%s listen <port>" % sys
.arv
[0]
82 print "%s upload <host> <port>" % sys
.arv
[0]
83 print "%s download <host> <port>" % sys
.arv
[0]
84 print "%s updown <host> <port>" % sys
.arv
[0]
89 if sys
.argv
[1] == "listen":
90 Receiver(int(sys
.argv
[2]))
91 elif sys
.argv
[1] == "download":
92 Connect( (sys
.argv
[2], int(sys
.argv
[3])), upload
=False, download
=True)
93 elif sys
.argv
[1] == "upload":
94 Connect( (sys
.argv
[2], int(sys
.argv
[3])), upload
=True, download
=False)
95 elif sys
.argv
[1] == "updown":
96 Connect( (sys
.argv
[2], int(sys
.argv
[3])), upload
=True, download
=True)