Example Of A Simple HTTP Server#

The following code is an example of a simple HTTP server that can be used to test basic async transaction status callback operations. The code is not intended for actual use.

Note that the HTTP 200 ACK is sent asyncronously before internal processing of the callback.

#!/usr/bin/env python
from datetime import datetime
import SimpleHTTPServer
import SocketServer
import logging
import cgi
import json
from pprint import pprint
PORT = 8080

class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):

    def do_GET(self):
        SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

    def do_POST(self):
        self.send_response(200)
        self.wfile.write("ACK")

        # Insert internal processing here.
        # Below is an example of internal processing that simply prints out the
        # callback request.
        print "\nPOST - {}".format(datetime.now())
        print "Headers:"
        pprint(dict(self.headers))
        print "\nRaw Body:"
        body = self.rfile.read(int(self.headers['Content-Length'])).decode('utf-8')
        pprint(body)
        print "\nPretty Body:"
        pprint(json.loads(body))


Handler = ServerHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "Serving at port", PORT
httpd.serve_forever()