title: Listening for HTTP responses
body: <p>I am trying to write a python application that will listen for HTTP responses on a socket. I am using <a href="https://github.com/benoitc/http-parser" rel="nofollow">http-parser</a> for this. Here is my code:</p>
  
  <pre><code>#!/usr/bin/env python
  import socket
  
  from http_parser.http import HttpStream
  from http_parser.reader import SocketReader
  
  from http_parser.util import b
  
  def main():
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.bind((socket.gethostname(), 7000))
      s.listen(5)
      try:
          while True:
              p = HttpStream(SocketReader(s))
      finally:
          s.close()
  
  if __name__ == "__main__":
      main()
  </code></pre>
  
  <p>I have two questions:</p>
  
  <ol>
  <li>Is this the best way to do this? Note that I do not want to send a request and then listen for a response. I want to have a redirection mechanism that redirects all responses to the box running this script.</li>
  <li>How should I test this? Is there a tool that can mock HTTP responses?</li>
  </ol>
  
  <p><strong>EDIT</strong>
  What I am trying to do is this: I have three boxes, once runs Apache, one runs this script and one is the client. When the client connects to Apache and it sends back a response, I am diverting the response to this box. So in this script, I am trying to listen for HTTP responses. </p>
  
  <p><strong>Topology</strong>
  Here is my topology:
  Server &lt;----> Switch one &lt;-----> Switch two &lt;-----> Box one and two
  Initially, box one connects to the server and sends a request. When the second switch receives responses from the server, it forks it to both box one and box two.</p>