HTTP Server with active TCP connections in python
<p>I am writing a pseudo-http application in python, the requirements for which are:</p>

<ol>
<li>It should handle HTTP requests.</li>
<li>The connections between the client and the server outlive the request-response, i.e. the underlying TCP connection remains alive after a response has been sent to a client.</li>
<li>The server needs to be able to send data to a particular client for which it already has an opened connection.</li>
</ol>

<p>I looked at twisted and python's TCPServer/BaseHTTPServer, but they don't quite fit the bill. The way I see it, I have two options:</p>

<ol>
<li>Start from a HTTP server implementation and override my way down to connection management.</li>
<li>Have a simple socket server that will manage the connections and pass data between the "http" server and the client.</li>
</ol>

<p>Has anyone tackled a similar issue? Any ideas on other approaches or which one will be a better option?</p>

<p>Thanks!</p>

<p><strong>EDIT 1</strong>
I cannot use HTTP 2 or web sockets; HTTP &lt;2 over TCP is a hard requirement.</p>

<p>Since you cannot use websockets or http/2, and you require the ability to push data from the server to the client, then long-polling is probably the best option remaining.</p>

<p>See Nevow, at <a href="https://github.com/twisted/nevow" rel="nofollow">https://github.com/twisted/nevow</a>, for one possible implementation of long-polling, via the athena module.</p>

python tcp twisted httpserver