33 import xml.etree.ElementTree
as ET
34 import xml.parsers.expat
38 logging.basicConfig(level = logging.DEBUG,
39 format=
'%(asctime)s %(levelname)s %(message)s')
46 logging.info(
'[ConciergeHandler] %(command)s request: %(host)s:%(port)d --- %(path)s',
47 dict(command = self.command,
48 host = self.client_address[0],
49 port = self.client_address[1],
53 logging.info(
'[ConciergeHandler] %(command)s returned %(status)d',
54 dict(command = self.command,
61 self.send_response(200)
62 self.send_header(
'Content-type',
'text/html')
68 logging.debug(
'[ConciergeHandler] %s', xml.tag)
69 for attr
in xml.attrib:
70 logging.debug(
'[ConciergeHandler] %s [%s] %s', xml.tag, attr, xml.attrib[attr])
71 for kid
in xml.getchildren():
77 for hdr
in self.headers.headers:
78 logging.debug(
'[ConciergeHandler] POST: header: %s', hdr.rstrip())
80 length = int(self.headers.getheader(
'Content-Length'))
81 content = self.rfile.read(length)
84 logging.debug(
'[ConciergeHandler] POST: content: %s', content)
86 postXml = ET.fromstring(content)
88 except xml.parsers.expat.ExpatError, xmlError:
89 logging.error(
'[ConciergeHandler] POST illformed:%s', xmlError)
90 self.send_response(500)
94 self.send_response(200)
95 self.send_header(
'Content-Type',
'text/html')
96 self.send_header(
'Content-Length', len(
'<success/>'))
99 self.wfile.write(
'<success/>')
102 self.send_response(500)
103 self.send_header(
'Content-Type',
'text/html')
104 self.send_header(
'Content-Length', len(
'<error>gotcha!</error>'))
106 self.wfile.write(
'<error>gotcha!</error>')
114 if __name__ ==
'__main__':
116 logging.info(
'[ConciergeServer] Concierge Broker Test Server starting')
118 parser = optparse.OptionParser()
119 parser.add_option(
'-p',
'--port', dest =
'port', help =
'port to listen on', metavar =
'PORT')
120 parser.add_option(
'-f',
'--fail', dest =
'fail', action =
'store_true', help =
'always fail POST requests')
122 (options, args) = parser.parse_args()
124 httpServer = BaseHTTPServer.HTTPServer((
'', 8080), ConciergeHandler)
126 httpServer.serve_forever()
127 except KeyboardInterrupt:
128 logging.info(
'[ConciergeServer] terminating')
130 httpServer.server_close()