14 """ Proxies a TCP connection between a single client-server pair.
16 This proxy is not suitable for production, but should work well for cases in
17 which a test needs to spy on the bytes put on the wire between a server and
21 from __future__
import absolute_import
22 from __future__
import division
23 from __future__
import print_function
32 _TCP_PROXY_BUFFER_SIZE = 1024
33 _TCP_PROXY_TIMEOUT = datetime.timedelta(milliseconds=500)
37 proxy_socket = socket.create_connection((gateway_address, gateway_port))
42 """Proxies a TCP connection between one client and one server."""
44 def __init__(self, bind_address, gateway_address, gateway_port):
77 for socket_to_read
in sockets_to_read:
79 client_socket, client_address = socket_to_read.accept()
82 data = socket_to_read.recv(_TCP_PROXY_BUFFER_SIZE)
87 data = socket_to_read.recv(_TCP_PROXY_BUFFER_SIZE)
95 raise RuntimeError(
'Unidentified socket appeared in read set.')
98 for socket_to_write
in sockets_to_write:
112 expected_writes = expected_reads
113 sockets_to_read, sockets_to_write, _ = select.select(
114 expected_reads, expected_writes, (),
115 _TCP_PROXY_TIMEOUT.total_seconds())
119 client_socket.close()