title: Executing an asynchronous test in Python
body: <p>I need to do the following test:</p>
  
  <ul>
  <li>send a GET request to a server (http://remote/...)</li>
  <li>wait for the server to send a POST request in response (http://local/...)</li>
  <li>parse the POST data and do some assertions</li>
  </ul>
  
  <p>Selenium does not fit this case: it can't listen to connections, and I can send a GET without Selenium as well.</p>
  
  <p>so, I make a unit test:</p>
  
  <pre><code>class MobiMoneyTestCase(TestCase):
  def test_can_send_response(self):
      resp = requests.post('http://url/api/', data={'callback': 'http://localhost:8000'})
  
      class Handler(SimpleHTTPRequestHandler):
          def do_GET(self):
              assert self.path == '...'
  
      httpd = SocketServer.ThreadingTCPServer(('localhost', 8000),Handler)
  </code></pre>
  
  <p>The test has to wait 5 seconds for the POST request and then fail if nothing happened. How can I merge these items in the test? If I put <code>sleep(5)</code> in the <code>test_can...</code>, the <code>httpd</code> handler does not reply until the countdown ends.</p>