Skip to content

Instantly share code, notes, and snippets.

@i-am-the-slime
Created May 28, 2012 12:35
Show Gist options
  • Save i-am-the-slime/2818935 to your computer and use it in GitHub Desktop.
Save i-am-the-slime/2818935 to your computer and use it in GitHub Desktop.
from BaseHTTPServer import BaseHTTPRequestHandler
import cgi
import urlparse
import json
from WaIDObjects import WaIdObjectFactory, WaIdMessage
__author__ = 'mark'
class WaIdHTTPHandler(BaseHTTPRequestHandler):
"""
Handler for HTTP requests.
"""
def __init__(self, request, client_address, server):
BaseHTTPRequestHandler.__init__(self, request, client_address, server)
self.eventList = []
def get_event_list(self):
return self.eventList
def as_waId_message(self, dic):
return WaIdMessage(dic['time'], dic['data'], dic['type'])
def do_POST(self):
"""
Handle messages containing POST variables. All other kinds of messages
are being ignored.
"""
contentType, pDict = cgi.parse_header(self.headers.getheader('content-type'))
if contentType=='application/x-www-form-urlencoded':
length = int(self.headers.getheader('content-length'))
post = urlparse.parse_qs(self.rfile.read(length), keep_blank_values=1)
jsonString = post.get('event')[0]
msg = json.loads(jsonString, object_hook=self.as_waId_message)
dings = WaIdObjectFactory.makeFromMessage(msg)
dings.addToEventList(self.eventList)
self.send_response(200, "") #Send back an OK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment