Ejemplo n.º 1
0
	def do_POST(self):
		self.send_response(200)
		self.send_header('Access-Control-Allow-Origin', '*')
		self.end_headers()

		parsed_path = urlparse.urlparse(self.path)
		params = urlparse.parse_qs(parsed_path.query)

		ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
		if ctype == 'multipart/form-data':
			postvars = cgi.parse_multipart(self.rfile, pdict)
		elif ctype == 'application/x-www-form-urlencoded':
			length = int(self.headers.getheader('content-length'))
			postvars = cgi.parse_qs(self.rfile.read(length), keep_blank_values=1)
		else:
			postvars = {}

		print parsed_path

		if parsed_path.path == '/heartbeat' :
			returnJson = {'status':  'ok'}
		elif parsed_path.path == '/completions':
			script = Script(postvars)
			script.display()
			returnJson = prophet.speak(script)
		else:
			returnJson = {'error': 'command ' + parsed_path.path + ' does not exist'}

		print "Returning", len(returnJson), "items"
		print
		self.wfile.write(json.dumps(returnJson, encoding = 'latin1'))