コード例 #1
0
def app(environ, start_response):
    get_data = urlparse.parse_qs(environ.get('QUERY_STRING', ''))

    try:
        request_body_size = int(environ.get('CONTENT_LENGTH', 0))
    except (ValueError):
        request_body_size = 0

    request_body = environ['wsgi.input'].read(request_body_size)
    post_data = urlparse.parse_qs(request_body)

    if post_data:
        print 'POST: {0}'.format(post_data)

    if get_data:
        print 'GET: {0}'.format(get_data)

    form = '''
        <form action="/" method="POSt">
        <input name="text" type="test">
        <input type="submit">
        </form>
    '''
    start_response('200 OK', [('Content-Type', 'text/html')])
    return 'GET: {0}<br> POST: {1}<br>{2}'.format(get_data, post_data, form)
コード例 #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)
コード例 #3
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) 
コード例 #4
0
ファイル: tyr_simple_server.py プロジェクト: gijs/tyr
  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 
コード例 #5
0
 def handle_request(self):
   #turn the request into json for some echo output
   split = self.path.split('?')
   query = {}
   if len(split) == 2:
     self.path = split[0]
     query = urlparse.parse_qs(split[1])
   d = {'method': self.command, 'path': self.path, 'version': self.request_version, 'headers': self.headers.dict, 'query': query}
   return json.dumps(d, separators=(',',':'))
コード例 #6
0
ファイル: query.py プロジェクト: f-hashemi/api
 def parse_url(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')
         params = urlparse.parse_qs(body)
         return params
     #handle GET
     else:
         params = urlparse.parse_qs(split.query)
         return params
コード例 #7
0
 def handle_request(self):
     #turn the request into json for some echo output
     split = self.path.split('?')
     query = {}
     if len(split) == 2:
         self.path = split[0]
         query = urlparse.parse_qs(split[1])
     d = {
         'method': self.command,
         'path': self.path,
         'version': self.request_version,
         'headers': self.headers.dict,
         'query': query
     }
     return json.dumps(d, separators=(',', ':'))
コード例 #8
0
    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
コード例 #9
0
ファイル: dashboard.py プロジェクト: johndam/ploneintranet
 def tile_options(self, tile):
     ''' Return the name of the tile from the tile path
     '''
     qs = tile.partition('?')[-1]
     return urlparse.parse_qs(qs)