Created
May 28, 2012 12:35
-
-
Save i-am-the-slime/2818935 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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