Skip to content

Instantly share code, notes, and snippets.

@natevw
Created September 23, 2011 19:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natevw/1238217 to your computer and use it in GitHub Desktop.
Save natevw/1238217 to your computer and use it in GitHub Desktop.
import sys, json
class External(object):
def run(self):
line = sys.stdin.readline()
while line:
try:
response = self.process(json.loads(line.decode('utf_8')))
except Exception, e:
response = {'code':500, 'json':{'error':True, 'reason':"Internal error processing request (%s)" % e}}
sys.stdout.write("%s\n" % json.dumps(response))
sys.stdout.flush()
line = sys.stdin.readline()
def process(self, req):
res = {}
# TODO: set res['body'] or res['json'] based on req['method']/req['body']/req['path']/etc.
return res
@steve-ross
Copy link

class External(object):
    def run(self, part='body'):
        line = sys.stdin.readline()
        while line:
            try:
                return self.process(json.loads(line.decode('utf_8')), part)
            except Exception, e:
                logger.exception(e)
                response = {'code':500, 'json':{'error':True, 'reason':"Internal error processing request (%s)" % e}}
                sys.stdout.write("%s\n" % json.dumps(response))
                sys.stdout.flush()
                line = sys.stdin.readline()

    def process(self, req, part):
        res = req[part]
        # TODO: set res['body'] or res['json'] based on req['method']/req['body']/req['path']/etc.
        return res

    def respond(self, code=200, data={}, headers={}):
            sys.stdout.write("%s\n" % json.dumps({"code": code, "json": data, "headers": headers}))
            sys.stdout.flush()

Created another class to extend it:

import sys
import os
import json
import Greenlight

import logging
logger = logging.getLogger('myapp')
hdlr = logging.FileHandler('/tmp/password.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr) 
logger.setLevel(logging.INFO)

class password_checker(Greenlight.External):

    def main(self):
        # for req in requests():
        #   respond(data={"response": req['query']})

        logger.info('running')
        data = self.run(part='query')

        logger.info('logging response:')
        logger.info(data)
        logger.info(data['fieldValue'])
        pwl = Greenlight.Passwordlist()
        pw = pwl.valid(data['fieldValue'])
        if data['fieldValue'] == pw:
            self.respond(data=['password', True, 'This password is good!'])
        else:
            self.respond(data=['password', False, 'This password is too common'])

if __name__ == "__main__":
    try:
        logger.info('running')
        pw = password_checker()
        pw.main()
    except Exception, e:
        logger.info('Error')
        logger.exception(e)

The password_checker is my external... and it only runs on 1,3,5 etc attempts... no log output whatsoever on 2,4,6,etc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment