"A HTTP Push CGI module for Python"

import sys
import cgi
import re

class HTTPPush:
    def __init__(self):
        parms = cgi.FieldStorage()
        self.type = parms.getvalue("pushtype")

        if self.type == "event":
            print "Content-Type: application/x-dom-event-stream\n"
        elif self.type == "multipart":
            print "Content-Type: multipart/x-mixed-replace;",
            print "boundary=\"bndry\"\n"
            # Dump a kb of data to flush browsers
            print "--bndry"
        elif self.type == "iframe":
            print "Content-Type: text/html\n\n"
            # Dump a kb of data to flush browsers
#            print "<html><head>"
#            print "<title>Inner frame for push</title>"
#            print "</head><body onload=\"alert('eep');\">"
            print "\n"*1024
        sys.stdout.flush()


    def push(self, data):
        if self.type == "event":
            print "Event: push"
            for i in data.split("\n"):
                print "data: "+str(i)
            print "";
        elif self.type == "multipart":
            print "Content-Type: text/plain\n"
            print data
            print "--bndry\n"
        elif self.type == "iframe":
            data = re.sub("'", "\\'", data);
            out = "<script type='text/javascript'>"
            out+= "parent.crappypush('"+data+"');"
            out+= "</script>\n"

            # Pad so IE plays nice
            if len(out) < 257:
                out += "\n" * (257 - len(out))

            print out
        sys.stdout.flush()

        
