예제 #1
0
def get(path, headers, body):
  global logger

  query = headers.get('QUERY')
  arguments = helpers.parse_query_string(query)

  try:
    priv = load_private()
    identity = decrypt(arguments['identity'], priv)
    if is_email(identity):
      password = decrypt(arguments['password'], priv)
      password = hashlib.new('md5', password).hexdigest()
      create_with_email(identity, password)
    elif is_phone_number(identity):
      password = decrypt(arguments['password'], priv)
      password = hashlib.new('md5', password).hexdigest()
      create_with_phone_number(identity, password)
    else:
      pass

    return 200, 'OK', message, {
      'Content-Type': 'text/plain'}
  except rsa.DecryptionError:
    logger.error(helpers.format_exception())
    return 500, 'Internal Server Error', 'Decryption failed', {}
예제 #2
0
def get(path, headers, body):
  global logger

  query = headers.get('QUERY')
  if None == query:
    return 400, 'Bad Request', 'Missing argument(s)', {}
  arguments = helpers.parse_query_string(query)
  if 'identity' not in arguments:
    return 400, 'Bad Request', 'Missing argument(s)', {}
  if 'password' not in arguments:
    return 400, 'Bad Request', 'Missing argument(s)', {}

  try:
    priv = load_private()
    
    identity_str = decrypt(arguments['identity'], priv)
    if None == identity_str:
      message = 'The login of "%s" does not exist.' % identity_str
      return 404, 'Not found', message, {}    
    password = decrypt(arguments['password'], priv)
    if None == password:
      return 401, 'Unauthorized', 'Incorrect password.', {}
    
    db = user.Database()
    logger.debug('identity = %s', identity_str)
    identity = user.Identity.load(db, identity_str)
    if None == identity: 
      message = 'The login of "%s" does not exist.' % identity_str
      return 404, 'Not found', message, {}
    if identity.validate(password):
      token = identity.create_token(db)
      return 200, 'OK', token, {'Content-Type': 'text/plain'}
    else:
      return 401, 'Unauthorized', 'Incorrect password.', {}
  except ValueError:
    logger.error(helpers.format_exception())
    return 400, 'Bad Request', 'Missing argument(s).', {}
  except rsa.DecryptionError:
    logger.error(helpers.format_exception())
    return 500, 'Internal Server Error', 'Decryption failed', {}
  except user.IdentityError:
    logger.error(helpers.format_exception())
    return 400, 'Bad Request', 'Unrecognized identity.', {}
예제 #3
0
파일: user.py 프로젝트: unasogno/bookstore
  def _exec(self, statement, handler = None):
    global _logger
    db = mysql.connect(
      config.DB_HOST, config.DB_USER, config.DB_PASSWORD, config.DB_INST)

    try:
      db.query(statement)
      if None <> handler:
        return handler(db)
      else:
        return db.affected_rows()
    except:
      _logger.error(helpers.format_exception())
      raise
    finally:
      db.close()
예제 #4
0
def run(send_spec, recv_spec, handlers):
  logger = helpers.init_logger('handler', config.LOG_PATH)
  sender_id = uuid4().hex

  conn = handler.Connection(
    sender_id, send_spec, recv_spec)

  while True:
    logger.debug('wait for request')
    req = conn.recv()

    if req.is_disconnect():
      logger.debug('request disconnected')
      continue
    else:
      method = req.headers.get('METHOD')
      logger.debug('incoming %s request' % method)

      code = 500
      status = 'Internal Server Error'
      response = 'Server Error'
      headers = None
      try:
        if method not in handlers:
          code = 405
          status = 'Method Not Allowed'
          response = 'The given method of %s is not supported' % method
        else:
          method_handler = handlers[method]
          code, status, response, headers = method_handler(
            req.path, req.headers, req.body)
          if 0 == code:
            logger.debug('Continue %s', response)
            continue
      except:
        logger.warn('An error occurs - %s', helpers.format_exception())
        logger.debug('Sending response - %s', response)
        conn.reply_http(req, response, code, status, headers)
        logger.debug('Request handled')
        
      logger.debug('Sending response - %s', response)
      conn.reply_http(req, response, code, status, headers)
      logger.debug('Request handled')
예제 #5
0
def run_program(prog_text, output_buf):
    def to_buf(val):
        output_buf.append(str(val))
    error_catcher = ErrorCatcher()
    config = dict(text = prog_text,
                  output = to_buf,
                  on_suspension = notify_suspend)

    old_err = sys.stderr
    try:
        sys.stderr = error_catcher
        executable = std_factory.create_program(**config)
        sys.stderr = old_err
    except:
        e = sys.exc_info()[1]
        exception, msg_list = format_exception(e, error_catcher.error_buf)
        return dict(exception = exception, msg_list = msg_list)
    else:
        return execute(executable, output_buf)
예제 #6
0
  q = xapian.Query(xapian.Query.OP_OR, l)

  enquire.set_query(q)
  matches = enquire.get_mset(0, 100)

  print '%i results found.' % matches.get_matches_estimated()
  print 'Result - %i:' % matches.size()

  r = []
  for m in matches:
    # print '%i: %i%% docid=%i [%s]' % (m.rank + 1, m.percent, m.docid,\
    # m.document.get_data())
    r.append(m.document.get_data())

  print json.dumps(r)

  return 200, 'OK', json.dumps(r), None 

handlers = { 'GET': get }
logger = helpers.init_logger('search', config.LOG_PATH)

if __name__ == '__main__':
  
  try:
    handler_config = config.HANDLER_CONFIG['search']
    handler.run(handler_config.send_spec, handler_config.recv_spec, handlers)
  except:
    logger.error(helpers.format_exception())
else:
  handler.handlers_registry[__name__] = handlers