bstinson / centos / t_functional

Forked from centos/t_functional 4 years ago
Clone

Blame tests/p_abrt-cli/_pyserve

Matej Habrnal e4efea
#!/usr/bin/env python
Matej Habrnal e4efea
# Single purpose HTTP server
Matej Habrnal e4efea
# - serves files specified as arguments in order of appearance
Matej Habrnal e4efea
Matej Habrnal e4efea
Matej Habrnal e4efea
import os
Matej Habrnal e4efea
import sys
Matej Habrnal e4efea
import BaseHTTPServer
Matej Habrnal e4efea
#from pprint import pprint
Matej Habrnal e4efea
Matej Habrnal e4efea
class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
Matej Habrnal e4efea
    def do_POST(self):
Matej Habrnal e4efea
Matej Habrnal e4efea
        print self.rfile.read(int(self.headers.getheader('content-length')))
Matej Habrnal e4efea
Matej Habrnal e4efea
        response = self.dummy_response
Matej Habrnal e4efea
        if not self.filelist:
Matej Habrnal e4efea
            print 'No more files to serve - sending dummy response'
Matej Habrnal e4efea
            sys.stdout.flush()
Matej Habrnal e4efea
        else:
Matej Habrnal e4efea
            response = self.filelist.pop()
Matej Habrnal e4efea
        self.reply(response)
Matej Habrnal e4efea
Matej Habrnal e4efea
    def reply(self, response):
Matej Habrnal e4efea
        try:
Matej Habrnal e4efea
            #redirect stdout to client
Matej Habrnal e4efea
            stdout = sys.stdout
Matej Habrnal e4efea
            sys.stdout = self.wfile
Matej Habrnal e4efea
            print response
Matej Habrnal e4efea
            stdout.flush()
Matej Habrnal e4efea
        finally:
Matej Habrnal e4efea
            sys.stdout = stdout # restore
Matej Habrnal e4efea
            pass
Matej Habrnal e4efea
Matej Habrnal e4efea
PORT = 12345
Matej Habrnal e4efea
print "Serving at port", PORT
Matej Habrnal e4efea
sys.stdout.flush()
Matej Habrnal e4efea
Matej Habrnal e4efea
filelist = []
Matej Habrnal e4efea
for file in sys.argv[1:]:
Matej Habrnal e4efea
    if os.path.isfile(file):
Matej Habrnal e4efea
        print "Adding file %s" % file
Matej Habrnal e4efea
        sys.stdout.flush()
Matej Habrnal e4efea
        with open(file) as f:
Matej Habrnal e4efea
            filelist.append(f.read())
Matej Habrnal e4efea
Matej Habrnal e4efea
filelist.reverse()
Matej Habrnal e4efea
Handler.filelist = filelist
Matej Habrnal e4efea
Handler.dummy_response = open("dummy", "r").read()
Matej Habrnal e4efea
httpd = BaseHTTPServer.HTTPServer(("", PORT), Handler)
Matej Habrnal e4efea
httpd.serve_forever()