コード例 #1
0
def getdata(url, params, headers=None, post=None, verbose=False):
    """
    Invoke URL call and retrieve data from data-service based
    on provided URL and set of parameters. Use post=True to
    invoke POST request.
    """
    encoded_data = urllib.urlencode(params)
    if  not post:
        url = url + '?' + encoded_data
    if  not headers:
        headers = {}
    if  verbose:
        print '+++ getdata, url=%s, headers=%s' % (url, headers)
    req = urllib2.Request(url)
    for key, val in headers.iteritems():
        req.add_header(key, val)
    if  verbose > 1:
        handler = urllib2.HTTPHandler(debuglevel=1)
        opener  = urllib2.build_opener(handler)
        urllib2.install_opener(opener)
    ckey, cert = get_key_cert()
    handler = HTTPSClientAuthHandler(ckey, cert, verbose)
    opener  = urllib2.build_opener(handler)
    urllib2.install_opener(opener)
    try:
        if  post:
            data = urllib2.urlopen(req, encoded_data)
        else:
            data = urllib2.urlopen(req)
        info = data.info()
        code = data.getcode()
        if  verbose > 1:
            print "+++ response code:", code
            print "+++ response info\n", info
        data = json.load(data)
    except urllib2.HTTPError as httperror:
        msg  = 'HTTPError, url=%s, args=%s, headers=%s' \
                    % (url, params, headers)
        data = {'error': 'Unable to contact %s' % url , 'reason': msg}
        try:
            data.update({'httperror':extract_http_error(httperror.read())})
        except Exception as exp:
            data.update({'httperror': None})
        data = json.dumps(data)
    except Exception as exp:
        msg  = 'HTTPError, url=%s, args=%s, headers=%s, error=%s' \
                    % (url, params, headers, str(exp))
        data = {'error': 'Unable to contact %s' % url, 'reason': msg}
        data = json.dumps(data)
    return data
コード例 #2
0
ファイル: tools.py プロジェクト: vkuznet/reqmgr_web_prototype
 def wrapper (self, *args, **kwds):
     """Decorator wrapper"""
     cherrypy.response.headers['Content-Type'] = "application/json"
     func._cp_config = {'response.stream': True}
     head, data = func (self, *args, **kwds)
     yield json.dumps(head)[:-1] # do not yield }
     yield ', "data": ['
     if  isinstance(data, dict):
         for chunk in JSONEncoder().iterencode(data):
             yield chunk
     elif  isinstance(data, list) or isinstance(data, types.GeneratorType):
         sep = ''
         for rec in data:
             if  sep:
                 yield sep
             for chunk in JSONEncoder().iterencode(rec):
                 yield chunk
             if  not sep:
                 sep = ', '
     else:
         msg = 'jsonstreamer, improper data type %s' % type(data)
         raise Exception(msg)
     yield ']}'