I'm trying to use BaseHTTPServer for a Prrarf unit test, but I got long pauses while it tried to fetch. That was because my eyes slid over the example code and I didn't realize I had to call the SocketServer.TCPServer method serve_forever for it to.. you know.. do anything.
import BaseHTTPServer
class TestFeedServerHandler (BaseHTTPServer.BaseHTTPRequestHandler):
host = 'localhost'
port = 12003
path = '/testfeed.xml'
url = 'http://%s:%d%s' % (host, port, path)
def do_GET(self):
if self.path <> TestFeedServerHandler.path:
self.wfile.write('HTTP/1.0 404 File Not Found\r\n')
self.wfile.write('Content-type: text/plain\r\n\r\n')
self.wfile.write('The file %s was not found.' % (self.path,))
self.wfile.close()
return
self.wfile.write('HTTP/1.0 200 OK\r\n')
self.wfile.write('Content-type: text/xml\r\n\r\n')
self.wfile.write("""
an elided RSS document
""")
self.wfile.close()
import os
pid = os.fork()
if 0 == pid:
TestFeedServer = BaseHTTPServer.HTTPServer ((TestFeedServerHandler.host, TestFeedServerHandler.port), TestFeedServerHandler)
TestFeedServer.serve_forever() # the secret ingredient: a ServerSocket.TCPServer method
import atexit, signal
atexit.register (os.kill, (pid, signal.SIGTERM))
This has the problem that, afterward, the web server process isn't stopped and reaped. Perhaps there's something I can do with I can't run the tests on my Windows box, because Windows doesn't have atexit. It also has the distinct imperfection thatos.fork(). However the alternative, using an external web server, is just as well, as I don't have one of those on the Windows box either. At least this way it's self-contained.
P.S. OK, so my Beautifier code isn't so hot. I should have it split long strings at \bs if possible. Plus the RSS document got reescaped if I escaped it and not escaped if I didn't, so I left it out; not exactly material anyway.