443
443
self.init_proxy_auth()
446
class RecordingServer(object):
447
"""A fake HTTP server.
449
It records the bytes sent to it, and replies with a 200.
452
def __init__(self, expect_body_tail=None):
455
:type expect_body_tail: str
456
:param expect_body_tail: a reply won't be sent until this string is
459
self._expect_body_tail = expect_body_tail
462
self.received_bytes = ''
465
self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
466
self._sock.bind(('127.0.0.1', 0))
467
self.host, self.port = self._sock.getsockname()
468
self._ready = threading.Event()
469
self._thread = threading.Thread(target=self._accept_read_and_reply)
470
self._thread.setDaemon(True)
474
def _accept_read_and_reply(self):
477
self._sock.settimeout(5)
479
conn, address = self._sock.accept()
480
# On win32, the accepted connection will be non-blocking to start
481
# with because we're using settimeout.
482
conn.setblocking(True)
483
while not self.received_bytes.endswith(self._expect_body_tail):
484
self.received_bytes += conn.recv(4096)
485
conn.sendall('HTTP/1.1 200 OK\r\n')
486
except socket.timeout:
487
# Make sure the client isn't stuck waiting for us to e.g. accept.
490
# The client may have already closed the socket.
497
# We might have already closed it. We don't care.