Created
October 4, 2016 05:54
-
-
Save braoru/525f41d2a89fb8d3a4a6baee61c8ec89 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
#!/usr/bin/python -i | |
from concurrent import futures | |
import logging | |
import time | |
from os import getpid | |
from circuits import Component, Event, Debugger, handler | |
from circuits.web import Server, Logger, JSONController | |
import json | |
_ONE_DAY_IN_SECONDS = 60 * 60 * 24 | |
# Logging | |
logging.basicConfig( | |
format='%(asctime)s %(name)s %(levelname)s %(process)d %(funcName)s() %(message)s', | |
datefmt='%m/%d/%Y %I:%M:%S %p' | |
) | |
logger = logging.getLogger("test") | |
logger.setLevel(logging.DEBUG) | |
class work(Event): | |
""" | |
""" | |
class App(Component): | |
def init(self): | |
Server(('0.0.0.0', 11111)).register(self) | |
Logger().register(self) | |
Debugger().register(self) | |
DeployableJSONController().register(self) | |
VirtualHostJSONController().register(self) | |
#self.server.start(process=True) | |
class DeployableJSONController(JSONController): | |
channel = "/deployable" | |
@handler("work") | |
def work(self, data): | |
json_data = json.loads(data) | |
logger.info(data) | |
logger.info("########################### start processing {n}".format(n=getpid())) | |
time.sleep(10) | |
logger.info( | |
json.dumps( | |
json_data, | |
sort_keys=True, | |
indent=4, | |
separators=(',', ': ') | |
) | |
) | |
def GET(self, *args, **kwargs): | |
return { | |
"data": "GET({0:s}, {1:s})".format(repr(args), repr(kwargs)) | |
} | |
def POST(self, *args, **kwargs): | |
data = str(self.request.body.read().decode()) | |
self.fire(work(data), "/deployable") | |
return { | |
"yoda": "To the hand you talk, because listening i'm not.", | |
"pid": getpid() | |
} | |
def PUT(self, *args, **kwargs): | |
return { | |
"data": "PUT({0:s}, {1:s})".format(repr(args), repr(kwargs)) | |
} | |
def DELETE(self, *args, **kwargs): | |
return { | |
"data": "DELETE({0:s}, {1:s})".format(repr(args), repr(kwargs)) | |
} | |
def HEAD(self, *args, **kwargs): | |
return { | |
"data": "HEAD({0:s}, {1:s})".format(repr(args), repr(kwargs)) | |
} | |
class VirtualHostJSONController(JSONController): | |
channel = "/virtualhost" | |
def GET(self, *args, **kwargs): | |
return "GET({0:s}, {1:s})".format(repr(args), repr(kwargs)) | |
def POST(self, *args, **kwargs): | |
return "POST({0:s}, {1:s})".format(repr(args), repr(kwargs)) | |
def PUT(self, *args, **kwargs): | |
return "PUT({0:s}, {1:s})".format(repr(args), repr(kwargs)) | |
def DELETE(self, *args, **kwargs): | |
return "DELETE({0:s}, {1:s})".format(repr(args), repr(kwargs)) | |
def HEAD(self, *args, **kwargs): | |
return "HEAD({0:s}, {1:s})".format(repr(args), repr(kwargs)) | |
if __name__ == "__main__": | |
app = App() | |
app.start(process=True) | |
try: | |
while True: | |
time.sleep(_ONE_DAY_IN_SECONDS) | |
except KeyboardInterrupt: | |
app.stop(0) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment