Beispiel #1
0
    def handle_request(self):
        #split the query from the path
        try:
            split = urlparse.urlsplit(self.path)
        except:
            raise Exception(
                'Try a url with 2 components: is_prime?possible_prime=SOME_NUMBER'
            )

        if split.path != '/is_prime':
            raise Exception('Try a valid path: is_prime')

        #get a dict and unexplode non-list entries
        try:
            params = urlparse.parse_qs(split.query)
            if len(params['possible_prime']) > 1:
                raise Exception('Only one possible_prime at a time')
            prime = int(params['possible_prime'][0])
        except Exception as e:
            raise e

        #prime computation
        divisor = 2
        high = prime
        while divisor < high:
            if prime % divisor == 0:
                break
            high = prime / divisor
            divisor += 1
        if divisor < high:
            prime = 2

        #hand it back
        return str(prime)
Beispiel #2
0
  def handle_request(self):
    #split the query from the path
    try:
      split = urlparse.urlsplit(self.path)
    except:
      raise Exception('Try a url with 2 components: is_prime?possible_prime=SOME_NUMBER')

    if split.path != '/is_prime':
      raise Exception('Try a valid path: is_prime')

    #get a dict and unexplode non-list entries
    try:
      params = urlparse.parse_qs(split.query)
      if len(params['possible_prime']) > 1:
        raise Exception('Only one possible_prime at a time')
      prime = int(params['possible_prime'][0])
    except Exception as e:
      raise e

    #prime computation
    divisor = 2;
    high = prime;
    while divisor < high:
      if prime % divisor == 0:
        break
      high = prime / divisor
      divisor += 1
    if divisor < high:
      prime = 2

    #hand it back
    return str(prime) 
Beispiel #3
0
  def handle_request(self):
    if len(self.path) > 1024:
      raise Exception('DOS someone else jerkface')

    #split the query from the path
    try:
      split = urlparse.urlsplit(self.path)
    except:
      raise Exception('Try a url with 2 components: action?querystring')
    #path has the costing method and action in it
    try:
      action = actions[split.path.split('/')[-1]]
    except:
      raise Exception('Try a valid action: ' + str([k for k in actions]))
    #get a dict and unexplode non-list entries
    params = urlparse.parse_qs(split.query)
    for k,v in params.iteritems():
      if len(v) == 1:
        params[k] = v[0]
    #save jsonp or not
    jsonp = params.get('jsonp', None)
    if params.has_key('json'):
      params = json.loads(params['json'])
      if jsonp is not None:
        params['jsonp'] = jsonp
    #do the action
    #just send the json over to c++ and parse it there
    result = action(json.dumps(params, separators=(',', ':'))).Action()
    #hand it back
    return result, jsonp is not None 
    def parse_segments(self, post):
        #split the query from the path
        try:
            split = urlparse.urlsplit(self.path)
        except:
            raise Exception('Try a url that looks like /action?query_string')
        #path has the action in it
        try:
            if split.path.split('/')[-1] not in actions:
                raise
        except:
            raise Exception('Try a valid action: ' + str([k for k in actions]))

        #handle GET
        json_req = None
        params = urlparse.parse_qs(split.query)
        if 'json' in params:
            json_req = json.loads(params['json'][0])
            del params['json']

        #handle POST
        if post:
            body = self.rfile.read(int(
                self.headers['Content-Length'])).decode('utf-8')
            json_req = json.loads(body)

        #do we have something
        if json_req is None:
            raise Exception('No json provided')

        #mix in the query parameters
        for k, v in params.iteritems():
            if k in json_req:
                continue
            if len(v) == 1:
                json_req[k] = v[0]
            elif len(v) > 1:
                json_req[k] = v

        return json_req
 def parse_trace(self, post):
   #split the query from the path
   try:
     split = urlparse.urlsplit(self.path)
   except:
     raise Exception('Try a url that looks like /action?query_string')
   #path has the action in it
   try:
     if split.path.split('/')[-1] not in actions:
       raise
   except:
     raise Exception('Try a valid action: ' + str([k for k in actions]))
   #handle POST
   if post:
     body = self.rfile.read(int(self.headers['Content-Length'])).decode('utf-8')
     return json.loads(body)
   #handle GET
   else:
     params = urlparse.parse_qs(split.query)
     if 'json' in params:
       return json.loads(params['json'][0])
   raise Exception('No json provided')