def do_PATCH(self): print("Just received a PATCH request") form = cgi.FieldStorage(fp=self.rfile, headers=self.headers, environ={ 'REQUEST_METHOD': 'PATCH', 'CONTENT_TYPE': self.headers['Content-Type'], }) args = {} for i in form: args[i] = form.getvalue(i) sp = ServicesProvider() contents = sp.get('router').do_PATCH(self.path, args) if (contents != False): self.send_response(200) self.end_headers() self.wfile.write(contents) else: self.send_response(404) self.end_headers()
def do_DELETE(self): print("Just received a DELETE request") try: sp = ServicesProvider() contents = sp.get('back.router').do_DELETE(self.path) self.send_response(200) self.end_headers() self.wfile.write(contents) except RestfulServerException as e: print 'HTTP error : {}'.format(e.get_message()) self.send_response(e.get_code()) self.end_headers() self.wfile.write('HTTP error : {}'.format(e.get_message()))
def do_GET(self): path = urlparse(self.path).path print("GET " + path) try: sp = ServicesProvider() contents = sp.get('back.router').do_GET(path) self.send_response(200) self.end_headers() self.wfile.write(contents) except RestfulServerException as e: print 'HTTP error : {}'.format(e.get_message()) self.send_response(e.get_code()) self.end_headers() self.wfile.write('HTTP error : {}'.format(e.get_message()))
def do_PUT(self): print("Just received a PUT request") args = cgi.FieldStorage(fp = self.rfile, headers = self.headers, environ = {'REQUEST_METHOD' : 'PUT'}) try: sp = ServicesProvider() contents = sp.get('router').do_PUT(self.path, args) self.send_response(200) self.end_headers() self.wfile.write(contents) except RestfulServerException as e: print 'HTTP error : {}'.format(e.get_message()) self.send_response(e.get_code()) self.end_headers() self.wfile.write('HTTP error : {}'.format(e.get_message()))
class SentencesServer(HTTPServer, object): def __init__(self, port, update_callback = None): super(SentencesServer, self).__init__(('0.0.0.0', port), SentencesHTTPHandler) self.sp = ServicesProvider(self) self.update_callback = update_callback def start(self): thread = threading.Thread(target = self.serve_forever) thread.daemon = True thread.start() def stop(self): self.shutdown() def on_update(self): if callable(self.update_callback): self.update_callback(self.sp.get('sentences_list').serialize()) def get_sentences(self): return self.sp.get('sentences_list').serialize()
def do_GET(self): path = urlparse(self.path).path print("GET " + path) query = urlparse(self.path).query args = dict() if len(query) > 0: for qc in query.split("&"): args[qc.split("=")[0]] = qc.split("=")[1] sp = ServicesProvider() contents = sp.get('http_server_router').do_GET(path, args) if (contents != False): self.send_response(200) self.end_headers() self.wfile.write(contents) else: self.send_response(404) self.end_headers()
def __init__(self, port, update_callback = None): super(HC1_Server, self).__init__(('0.0.0.0', port), HC1_HTTPHandler) self.sp = ServicesProvider(self) self.update_callback = update_callback
def __init__(self, port): super(RestfulServer, self).__init__(('0.0.0.0', port), HTTPHandler) self.sp = ServicesProvider(self)