Python google.appengine.api.urlfetch 模块,fetch() 实例源码

我们从Python开源项目中,提取了以下49个代码示例,用于说明如何使用google.appengine.api.urlfetch.fetch()

项目:resizr    作者:iamareebjamal    | 项目源码 | 文件源码
def get(self, width, height):
        url = self.request.get('url')
        key = '{}:{}:{}'.format(url, width, height)

        height, width = [int(height), int(width)]

        cached = Photo.get_by_id(key)
        if cached is None:
            img = urlfetch.fetch(url).content

            output = images.resize(img, width=width, height=height)

            cache = Photo(id = key, image = output)
            cache.put()
        else:
            output = cached.image
        self.response.headers['Content-Type'] = 'image/png'
        self.response.headers['Cache-Control'] = 'max-age=3600'
        self.response.write(output)
项目:Taigabot    作者:FrozenPigs    | 项目源码 | 文件源码
def request(self, method, url, body, headers):
      # Calculate the absolute URI, which fetch requires
      netloc = self.host
      if self.port:
        netloc = '%s:%s' % (self.host, self.port)
      absolute_uri = '%s://%s%s' % (self.scheme, netloc, url)
      try:
        response = fetch(absolute_uri, payload=body, method=method,
            headers=headers, allow_truncated=False, follow_redirects=False,
            deadline=self.timeout,
            validate_certificate=self.validate_certificate)
        self.response = ResponseDict(response.headers)
        self.response['status'] = str(response.status_code)
        self.response['reason'] = httplib.responses.get(response.status_code, 'Ok')
        self.response.status = response.status_code
        setattr(self.response, 'read', lambda : response.content)

      # Make sure the exceptions raised match the exceptions expected.
      except InvalidURLError:
        raise socket.gaierror('')
      except (DownloadError, ResponseTooLargeError, SSLCertificateError):
        raise httplib.HTTPException()
项目:Intranet-Penetration    作者:yuxiaokui    | 项目源码 | 文件源码
def request(self, method, url, body, headers):
      # Calculate the absolute URI, which fetch requires
      netloc = self.host
      if self.port:
        netloc = '%s:%s' % (self.host, self.port)
      absolute_uri = '%s://%s%s' % (self.scheme, netloc, url)
      try:
        response = fetch(absolute_uri, payload=body, method=method,
            headers=headers, allow_truncated=False, follow_redirects=False,
            deadline=self.timeout,
            validate_certificate=self.validate_certificate)
        self.response = ResponseDict(response.headers)
        self.response['status'] = str(response.status_code)
        self.response['reason'] = httplib.responses.get(response.status_code, 'Ok')
        self.response.status = response.status_code
        setattr(self.response, 'read', lambda : response.content)

      # Make sure the exceptions raised match the exceptions expected.
      except InvalidURLError:
        raise socket.gaierror('')
      except (DownloadError, ResponseTooLargeError, SSLCertificateError):
        raise httplib.HTTPException()
项目:MKFQ    作者:maojingios    | 项目源码 | 文件源码
def request(self, method, url, body, headers):
      # Calculate the absolute URI, which fetch requires
      netloc = self.host
      if self.port:
        netloc = '%s:%s' % (self.host, self.port)
      absolute_uri = '%s://%s%s' % (self.scheme, netloc, url)
      try:
        response = fetch(absolute_uri, payload=body, method=method,
            headers=headers, allow_truncated=False, follow_redirects=False,
            deadline=self.timeout,
            validate_certificate=self.validate_certificate)
        self.response = ResponseDict(response.headers)
        self.response['status'] = str(response.status_code)
        self.response['reason'] = httplib.responses.get(response.status_code, 'Ok')
        self.response.status = response.status_code
        setattr(self.response, 'read', lambda : response.content)

      # Make sure the exceptions raised match the exceptions expected.
      except InvalidURLError:
        raise socket.gaierror('')
      except (DownloadError, ResponseTooLargeError, SSLCertificateError):
        raise httplib.HTTPException()
项目:edx-video-pipeline    作者:edx    | 项目源码 | 文件源码
def request(self, method, url, body, headers):
      # Calculate the absolute URI, which fetch requires
      netloc = self.host
      if self.port:
        netloc = '%s:%s' % (self.host, self.port)
      absolute_uri = '%s://%s%s' % (self.scheme, netloc, url)
      try:
        response = fetch(absolute_uri, payload=body, method=method,
            headers=headers, allow_truncated=False, follow_redirects=False,
            deadline=self.timeout,
            validate_certificate=self.validate_certificate)
        self.response = ResponseDict(response.headers)
        self.response['status'] = response.status_code
        setattr(self.response, 'read', lambda : response.content)

      # Make sure the exceptions raised match the exceptions expected.
      except InvalidURLError:
        raise socket.gaierror('')
      except (DownloadError, ResponseTooLargeError, SSLCertificateError):
        raise httplib.HTTPException()
项目:oriskami-python    作者:oriskami    | 项目源码 | 文件源码
def request(self, method, url, headers, post_data=None):
        try:
            result = urlfetch.fetch(
                url=url,
                method=method,
                headers=headers,
                # Google App Engine doesn't let us specify our own cert bundle.
                # However, that's ok because the CA bundle they use recognizes
                # api.ubivar.com.
                validate_certificate=self._verify_ssl_certs,
                deadline=self._deadline,
                payload=post_data
            )
        except urlfetch.Error as e:
            self._handle_request_error(e, url)

        return result.content, result.status_code, result.headers
项目:ifttt-line    作者:MypaceEngine    | 项目源码 | 文件源码
def getAddress(longitude, latitude):
    gsp_key = "gps-" + str(longitude) + "," + str(latitude)
    resultData = memcache.get(key=gsp_key)
    if resultData == None:
        url = "https://maps.googleapis.com/maps/api/geocode/json?language=ja&sensor=false&key=" + const.GOOGLE_API_KEY + "&latlng=" + str(
            longitude) + "," + str(latitude)
        logging.debug(url)
        result = urlfetch.fetch(
            url=url,
            method=urlfetch.GET,
            headers={
            }
        )
        if result.status_code == 200:
            logging.debug(result.content)
        else:
            logging.debug(result.content)
        jsonstr = result.content
        jsonobj = json.loads(jsonstr)
        if len(jsonobj["results"]) > 0:
            memcache.set(key=gsp_key, value=jsonobj, time=3600)
        resultData = jsonobj;
    else:
        logging.debug(resultData)
    return resultData["results"]
项目:ifttt-line    作者:MypaceEngine    | 项目源码 | 文件源码
def getUserProfine(mid):
#    midstr= ','.join(mids)
    url = "https://api.line.me/v2/bot/profile/"+mid
    result = urlfetch.fetch(
                             url=url,
                             method=urlfetch.GET,
                             headers={
                                      'Authorization': 'Bearer '+const.ChannelAccessToken
                                      }
                            )
    if result.status_code == 200:
        logging.debug(result.content)
    else:
        logging.debug(result.content)
    jsonstr = result.content
    jsonobj = json.loads(jsonstr)
    return jsonobj
项目:YouPBX    作者:JoneXiong    | 项目源码 | 文件源码
def _appengine_fetch(self, uri, params, method):
        if method == 'GET':
            uri = self._build_get_uri(uri, params)

        try:
            httpmethod = getattr(urlfetch, method)
        except AttributeError:
            raise NotImplementedError(
                "Google App Engine does not support method '%s'" % method)

        authstring = base64.encodestring('%s:%s' % (self.auth_id, self.auth_token))
        authstring = authstring.replace('\n', '')
        r = urlfetch.fetch(url=uri, payload=urllib.urlencode(params),
            method=httpmethod,
            headers={'Content-Type': 'application/x-www-form-urlencoded',
                'Authorization': 'Basic %s' % authstring})
        if r.status_code >= 300:
            raise HTTPErrorAppEngine("HTTP %s: %s" % \
                (r.status_code, r.content))
        return r.content
项目:YouPBX    作者:JoneXiong    | 项目源码 | 文件源码
def _appengine_fetch(self, uri, params, method):
        if method == 'GET':
            uri = self._build_get_uri(uri, params)

        try:
            httpmethod = getattr(urlfetch, method)
        except AttributeError:
            raise NotImplementedError(
                "Google App Engine does not support method '%s'" % method)

        authstring = base64.encodestring('%s:%s' % (self.auth_id, self.auth_token))
        authstring = authstring.replace('\n', '')
        r = urlfetch.fetch(url=uri, payload=urllib.urlencode(params),
            method=httpmethod,
            headers={'Content-Type': 'application/x-www-form-urlencoded',
                'Authorization': 'Basic %s' % authstring})
        if r.status_code >= 300:
            raise HTTPErrorAppEngine("HTTP %s: %s" % \
                (r.status_code, r.content))
        return r.content
项目:YouPBX    作者:JoneXiong    | 项目源码 | 文件源码
def _appengine_fetch(self, uri, params, method):
        if method == 'GET':
            uri = self._build_get_uri(uri, params)

        try:
            httpmethod = getattr(urlfetch, method)
        except AttributeError:
            raise NotImplementedError(
                "Google App Engine does not support method '%s'" % method)

        authstring = base64.encodestring('%s:%s' % (self.auth_id, self.auth_token))
        authstring = authstring.replace('\n', '')
        r = urlfetch.fetch(url=uri, payload=urllib.urlencode(params),
            method=httpmethod,
            headers={'Content-Type': 'application/x-www-form-urlencoded',
                'Authorization': 'Basic %s' % authstring})
        if r.status_code >= 300:
            raise HTTPErrorAppEngine("HTTP %s: %s" % \
                (r.status_code, r.content))
        return r.content
项目:docforever    作者:Udala    | 项目源码 | 文件源码
def pushtxn(raw_tx):
  '''Insight send raw tx API''' 
  url = PUSH_TX_URL
  payload = urllib.urlencode({
    "rawtx": raw_tx 
  })
  result = urlfetch.fetch(url,
    method=urlfetch.POST,
    payload=payload
  )

  if result.status_code == 200:  
    j = json.loads(result.content)
    txid = j.get('txid')
    return txid, raw_tx
  else: 
    msg = 'Error accessing insight API:'+str(result.status_code)+" "+str(result.content)
    ErrorNotification.new(msg) 
    return None, msg
项目:xxNet    作者:drzorm    | 项目源码 | 文件源码
def request(self, method, url, body, headers):
      # Calculate the absolute URI, which fetch requires
      netloc = self.host
      if self.port:
        netloc = '%s:%s' % (self.host, self.port)
      absolute_uri = '%s://%s%s' % (self.scheme, netloc, url)
      try:
        response = fetch(absolute_uri, payload=body, method=method,
            headers=headers, allow_truncated=False, follow_redirects=False,
            deadline=self.timeout,
            validate_certificate=self.validate_certificate)
        self.response = ResponseDict(response.headers)
        self.response['status'] = str(response.status_code)
        self.response['reason'] = httplib.responses.get(response.status_code, 'Ok')
        self.response.status = response.status_code
        setattr(self.response, 'read', lambda : response.content)

      # Make sure the exceptions raised match the exceptions expected.
      except InvalidURLError:
        raise socket.gaierror('')
      except (DownloadError, ResponseTooLargeError, SSLCertificateError):
        raise httplib.HTTPException()
项目:server    作者:viur-framework    | 项目源码 | 文件源码
def SetExpressCheckout(self, amount):
            params = {
                'METHOD' : "SetExpressCheckout",
                'NOSHIPPING' : 1,
                'PAYMENTACTION' : 'Authorization',
                'RETURNURL' : self.returnurl,
                'CANCELURL' : self.cancelurl,
                'AMT' : amount,
                'CURRENCYCODE': self.currency
            }
            params_string = self.signature + urllib.urlencode(params)
            response = urlfetch.fetch(self.API_ENDPOINT, params_string.encode("UTF-8"),"POST",deadline=10).content.decode("UTF-8")
            response_token = ""
            for token in response.split('&'):
                if token.find("TOKEN=") != -1:
                    response_token = token[ (token.find("TOKEN=")+6):]
            return response_token
项目:server    作者:viur-framework    | 项目源码 | 文件源码
def DoExpressCheckoutPayment(self, token, payer_id, amt):
            params = {
                'METHOD' : "DoExpressCheckoutPayment",
                'PAYMENTACTION' : 'Sale',
                'RETURNURL' : self.returnurl,
                'CANCELURL' : self.cancelurl,
                'TOKEN' : token,
                'AMT' : amt,
                'PAYERID' : payer_id,
                'CURRENCYCODE': self.currency,
            }
            params_string = self.signature + urllib.urlencode(params)
            response = urlfetch.fetch(self.API_ENDPOINT, params_string.encode("UTF-8"),"POST",deadline=10).content.decode("UTF-8")
            response_tokens = {}
            for token in response.split('&'):
                response_tokens[token.split("=")[0]] = token.split("=")[1]
            for key in list(response_tokens.keys()):
                    response_tokens[key] = urllib.unquote(response_tokens[key])
            return response_tokens
项目:server    作者:viur-framework    | 项目源码 | 文件源码
def exportItems( module, target, importKey, startCursor, endCursor):
    Skel = skeletonByKind( module )
    query = Skel().all().cursor( startCursor, endCursor )

    for item in query.run(250):
        flatItem = DbTransfer.genDict( item )
        formFields = {
            "e": pickle.dumps(flatItem).encode("HEX"),
            "key": importKey
        }
        result = urlfetch.fetch(        url=target,
                                        payload=urllib.urlencode(formFields),
                                        method=urlfetch.POST,
                                        headers={'Content-Type': 'application/x-www-form-urlencoded'})

    if startCursor == endCursor:
        try:
            utils.sendEMailToAdmins("Export of kind %s finished" % module,
                                    "ViUR finished to export kind %s to %s.\n" % (module, target))
        except: #OverQuota, whatever
            pass


# --- import ---
项目:gae-sports-data    作者:jnguyen-ca    | 项目源码 | 文件源码
def __init__(self, league_id, start_date, end_date):
        """
        Args:
            league_id (string): a valid league_id from constants.py
            start_date (string): date string in iso 8601 format
            end_date (string): date string in iso 8601 format
        """
        self.league = league_id
        self.start_date = start_date
        self.end_date = end_date

        logging.info("Requesting %s for %s until %s" % (self.league, self.start_date, self.end_date))
        response = urlfetch.fetch(self.url)
        self.response_dict = json.loads(response.content)

        self.game_list = self.parse_game_list()
项目:gae-sports-data    作者:jnguyen-ca    | 项目源码 | 文件源码
def _request(self):
        """Sends the request
        Returns:
            list
        """
        if not memcache.add(type(self).__name__, True, 3):
            time.sleep(3)
        logging.info('Scraping %s' % (type(self).__name__))

        url = "https://www.usatoday.com/sports/mlb/sagarin/2017/team/"
        response = urlfetch.fetch(url)

        try:
            ratings = self._scrape(response.content)
        except (AttributeError, IndexError) as e:
            logging.exception(e)
            ratings = []

        return ratings
项目:montage    作者:storyful    | 项目源码 | 文件源码
def fetch(self, at_milliseconds):
        """
            Tries to fetch the thumbnail from the cache.

            If it is not there, tries to retrieve it from YouTube
            and caches the result if successful

            Returns:
            either the thumbnail or the default thumbnail image
            whether the thumbnail fetch was successful or not
        """
        cached_key = self.create_key(at_milliseconds)

        image_content = self.fetch_cached_thumbnail(cached_key)

        if not image_content:
            image_content, found = self.fetch_thumbnail_from_youtube(
                at_milliseconds)

            if found:
                self.cache_thumbnail(image_content, cached_key)
        else:
            found = True

        return image_content, found
项目:montage    作者:storyful    | 项目源码 | 文件源码
def fetch_thumbnail_from_youtube(self, at_milliseconds):
        """
            Retrieves the YT thumbnail.

            Returns the raw image content with the
        """
        url = \
        "http://img.youtube.com/vd?id={yt_id}&ats={at_milliseconds}".format(
            yt_id=self.youtube_id, at_milliseconds=at_milliseconds)

        retries = 0
        content = None
        found = False
        while retries <= self.max_fetch_retries:
            result = urlfetch.fetch(url, deadline=50)
            if result.status_code in (200, 404,):
                content = result.content

            if result.status_code == 200:
                found = True
                break
            else:
                retries += 1

        return content, found
项目:appbackendapi    作者:codesdk    | 项目源码 | 文件源码
def get(self):
        auth_token, _ = app_identity.get_access_token(
            'https://www.googleapis.com/auth/cloud-platform')
        logging.info(
            'Using token {} to represent identity {}'.format(
                auth_token, app_identity.get_service_account_name()))

        response = urlfetch.fetch(
            'https://www.googleapis.com/storage/v1/b?project={}'.format(
                app_identity.get_application_id()),
            method=urlfetch.GET,
            headers={
                'Authorization': 'Bearer {}'.format(auth_token)
            }
        )

        if response.status_code != 200:
            raise Exception(
                'Call failed. Status code {}. Body {}'.format(
                    response.status_code, response.content))

        result = json.loads(response.content)
        self.response.headers['Content-Type'] = 'application/json'
        self.response.write(json.dumps(result, indent=2))
项目:dancedeets-monorepo    作者:mikelambert    | 项目源码 | 文件源码
def check_language(text):
    if len(text) > MAX_LENGTH:
        logging.info("trimming text from %s to %s", len(text), MAX_LENGTH)
        text = text[:MAX_LENGTH]
    base_url = 'https://www.googleapis.com/language/translate/v2/detect'
    params = {'key': API_KEY, 'q': text}
    form_data = urls.urlencode(params)
    if urllib2_fallback:
        request = urllib2.Request(base_url, form_data, {'X-HTTP-Method-Override': 'GET'})
        response_content = urllib2.urlopen(request).read()
    else:
        result = urlfetch.fetch(url=base_url, payload=form_data, method=urlfetch.POST, headers={'X-HTTP-Method-Override': 'GET'})
        if result.status_code != 200:
            error = "result status code is %s for content %s" % (result.status_code, result.content)
            logging.error(error)
            raise Exception("Error in translation: %s" % error)
        response_content = result.content
    json_content = json.loads(response_content)
    real_results = json_content['data']['detections'][0][0]
    logging.info("text classification returned %s", real_results)
    if real_results['confidence'] > 0.10:
        return real_results['language']
    else:
        return None
项目:Deploy_XXNET_Server    作者:jzp820927    | 项目源码 | 文件源码
def request(self, method, url, body, headers):
      # Calculate the absolute URI, which fetch requires
      netloc = self.host
      if self.port:
        netloc = '%s:%s' % (self.host, self.port)
      absolute_uri = '%s://%s%s' % (self.scheme, netloc, url)
      try:
        response = fetch(absolute_uri, payload=body, method=method,
            headers=headers, allow_truncated=False, follow_redirects=False,
            deadline=self.timeout,
            validate_certificate=self.validate_certificate)
        self.response = ResponseDict(response.headers)
        self.response['status'] = str(response.status_code)
        self.response['reason'] = httplib.responses.get(response.status_code, 'Ok')
        self.response.status = response.status_code
        setattr(self.response, 'read', lambda : response.content)

      # Make sure the exceptions raised match the exceptions expected.
      except InvalidURLError:
        raise socket.gaierror('')
      except (DownloadError, ResponseTooLargeError, SSLCertificateError):
        raise httplib.HTTPException()
项目:Deploy_XXNET_Server    作者:jzp820927    | 项目源码 | 文件源码
def list_bucket_files(bucket_name, prefix, max_keys=1000):
  """Returns a listing of of a bucket that matches the given prefix."""
  scope = config.GoogleApiScope('devstorage.read_only')
  bucket_url = config.GsBucketURL(bucket_name)
  url = bucket_url + '?'
  query = [('max-keys', max_keys)]
  if prefix:
    query.append(('prefix', prefix))
  url += urllib.urlencode(query)
  auth_token, _ = app_identity.get_access_token(scope)
  result = urlfetch.fetch(url, method=urlfetch.GET, headers={
      'Authorization': 'OAuth %s' % auth_token,
      'x-goog-api-version': '2'})
  if result and result.status_code == 200:
    doc = xml.dom.minidom.parseString(result.content)
    return [node.childNodes[0].data for node in doc.getElementsByTagName('Key')]
  raise BackupValidationError('Request to Google Cloud Storage failed')
项目:Deploy_XXNET_Server    作者:jzp820927    | 项目源码 | 文件源码
def get_gs_object(bucket_name, path):
  """Returns a listing of of a bucket that matches the given prefix."""
  scope = config.GoogleApiScope('devstorage.read_only')
  bucket_url = config.GsBucketURL(bucket_name)
  url = bucket_url + path
  auth_token, _ = app_identity.get_access_token(scope)
  result = urlfetch.fetch(url, method=urlfetch.GET, headers={
      'Authorization': 'OAuth %s' % auth_token,
      'x-goog-api-version': '2'})
  if result and result.status_code == 200:
    return result.content
  if result and result.status_code == 403:
    raise BackupValidationError(
        'Requested path %s is not accessible/access denied' % url)
  if result and result.status_code == 404:
    raise BackupValidationError('Requested path %s was not found' % url)
  raise BackupValidationError('Error encountered accessing requested path %s' %
                              url)
项目:cloud-memory    作者:onejgordon    | 项目源码 | 文件源码
def fetch(self):
        logging.debug("Fetching google calendar data")
        self.build_service('calendar', 'v3')
        timeMin = self.date_dt.isoformat() + 'Z'
        timeMax = self.next_date_dt.isoformat() + 'Z'
        results = self.service.events().list(calendarId='primary',
                                             maxResults=self.limit,
                                             timeMin=timeMin,
                                             timeMax=timeMax).execute()
        if results:
            items = [
                Item(
                    svc=SERVICE.GCAL,
                    title=r.get('summary'),
                    details=r.get('description'),
                    id=r.get('id'),
                    type=SERVICE.EVENT).json() for r in results.get(
                    'items',
                    [])]
            return items
        return []
项目:naziscore    作者:rbanffy    | 项目源码 | 文件源码
def get_access_token(force=False):
    """Tries to obtain access token from memcache and, if it fails,
    obtains a new set and stores in memcache.

    See https://dev.twitter.com/oauth/application-only.

    Deleting the memcache key `access_token` will trigger a token refresh.
    """
    token = memcache.get('access_token')
    if force or token is None:
        logging.warning('Needed to fetch access_token')
        encoded_key = urllib.quote_plus(CUSTOMER_KEY)
        encoded_secret = urllib.quote_plus(CUSTOMER_SECRET)
        encoded_credentials = base64.b64encode(
            "{}:{}".format(encoded_key, encoded_secret))
        response = urlfetch.fetch(
            'https://api.twitter.com/oauth2/token',
            payload='grant_type=client_credentials',
            method=urlfetch.POST,
            headers={'Authorization': 'Basic ' + encoded_credentials})
        if response.status_code == urlfetch.httplib.OK:
            response_data = json.loads(response.content)
            token = response_data['access_token']
            memcache.set('access_token', token, 2592000)  # 30 days
    return token
项目:naziscore    作者:rbanffy    | 项目源码 | 文件源码
def authenticated_get(
        url, customer_key=CUSTOMER_KEY, customer_secret=CUSTOMER_SECRET):
    """Performs an authenticated GET to the given URL, returns the response's
       content.

    See https://dev.twitter.com/oauth/application-only

    """
    token = get_access_token()
    response = urlfetch.fetch(
        url,
        method=urlfetch.GET,
        headers={'Authorization': 'Bearer ' + token})
    if response.status_code == urlfetch.httplib.OK:
        return response.content
    elif response.status_code == urlfetch.httplib.UNAUTHORIZED:
        return response.content  # User is probably suspended
    else:
        message = 'Url ' + url + ' returned ' + response.content
        logging.warning(message)
        raise urlfetch.httplib.HTTPException(message)
项目:helios-server-mixnet    作者:RunasSudo    | 项目源码 | 文件源码
def urlread(url, data=None, headers=None):
        if data is not None:
            if headers is None:
                headers = {"Content-type": "application/x-www-form-urlencoded"}
            method = urlfetch.POST
        else:
            if headers is None:
                headers = {}
            method = urlfetch.GET

        result = urlfetch.fetch(url, method=method,
                                payload=data, headers=headers)

        if result.status_code == 200:
            return result.content
        else:
            raise urllib2.URLError("fetch error url=%s, code=%d" % (url, result.status_code))
项目:Docker-XX-Net    作者:kuanghy    | 项目源码 | 文件源码
def request(self, method, url, body, headers):
      # Calculate the absolute URI, which fetch requires
      netloc = self.host
      if self.port:
        netloc = '%s:%s' % (self.host, self.port)
      absolute_uri = '%s://%s%s' % (self.scheme, netloc, url)
      try:
        response = fetch(absolute_uri, payload=body, method=method,
            headers=headers, allow_truncated=False, follow_redirects=False,
            deadline=self.timeout,
            validate_certificate=self.validate_certificate)
        self.response = ResponseDict(response.headers)
        self.response['status'] = str(response.status_code)
        self.response['reason'] = httplib.responses.get(response.status_code, 'Ok')
        self.response.status = response.status_code
        setattr(self.response, 'read', lambda : response.content)

      # Make sure the exceptions raised match the exceptions expected.
      except InvalidURLError:
        raise socket.gaierror('')
      except (DownloadError, ResponseTooLargeError, SSLCertificateError):
        raise httplib.HTTPException()
项目:flow-dashboard    作者:onejgordon    | 项目源码 | 文件源码
def validate_google_id_token(self, token):
        from settings import secrets
        success = False
        email = name = None
        g_response = urlfetch.fetch("https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=%s" % token)
        if g_response.status_code == 200:
            json_response = json.loads(g_response.content)
            if 'aud' in json_response:
                aud = json_response['aud']
                if aud == secrets.GOOGLE_CLIENT_ID:
                    success = True
                    email = json_response.get("email", None)
                    name = json_response.get("name", None)
                else:
                    logging.error("Client ID mismatch")
        return (success, email, name)
项目:flow-dashboard    作者:onejgordon    | 项目源码 | 文件源码
def get_request_token(base):
    '''
    Get request token
    '''
    data = urllib.urlencode({
        'consumer_key': POCKET_CONSUMER_KEY,
        'redirect_uri': base + POCKET_FINISH_REDIRECT
    })
    logging.debug(data)
    res = urlfetch.fetch(
        url=POCKET_OAUTH_REQUEST,
        method=urlfetch.POST,
        payload=data,
        validate_certificate=True)
    code = redirect = None
    logging.debug(res.status_code)
    if res.status_code == 200:
        result = res.content
        if 'code=' in result:
            code = result.replace('code=','')
            redirect = POCKET_AUTHORIZE_REDIR + '?request_token=%s&redirect_uri=%s' % (code, base + POCKET_FINISH_REDIRECT)
    return (code, redirect)
项目:flow-dashboard    作者:onejgordon    | 项目源码 | 文件源码
def get_access_token(code):
    '''
    Get request token
    '''
    data = urllib.urlencode({
        'consumer_key': POCKET_CONSUMER_KEY,
        'code': code
    })
    logging.debug(data)
    res = urlfetch.fetch(
        url=POCKET_OAUTH_AUTHORIZE,
        method=urlfetch.POST,
        payload=data,
        validate_certificate=True)
    code = redirect = None
    logging.debug(res.status_code)
    if res.status_code == 200:
        result = res.content
        data = urlparse.parse_qs(result)
        access_token = data.get('access_token', [None])[0]
        return access_token
项目:oscars2016    作者:0x0ece    | 项目源码 | 文件源码
def _new_fixed_fetch(validate_certificate):
        def fixed_fetch(url, payload=None, method="GET", headers={},
                        allow_truncated=False, follow_redirects=True,
                        deadline=None):
            if deadline is None:
                deadline = socket.getdefaulttimeout() or 5
            return fetch(url, payload=payload, method=method, headers=headers,
                         allow_truncated=allow_truncated,
                         follow_redirects=follow_redirects, deadline=deadline,
                         validate_certificate=validate_certificate)
        return fixed_fetch
项目:sndlatr    作者:Schibum    | 项目源码 | 文件源码
def _new_fixed_fetch(validate_certificate):
        def fixed_fetch(url, payload=None, method="GET", headers={},
                        allow_truncated=False, follow_redirects=True,
                        deadline=5):
            return fetch(url, payload=payload, method=method, headers=headers,
                         allow_truncated=allow_truncated,
                         follow_redirects=follow_redirects, deadline=deadline,
                         validate_certificate=validate_certificate)
        return fixed_fetch
项目:GAMADV-XTD    作者:taers232c    | 项目源码 | 文件源码
def _new_fixed_fetch(validate_certificate):
    def fixed_fetch(url, payload=None, method="GET", headers={},
                    allow_truncated=False, follow_redirects=True,
                    deadline=None):
        if deadline is None:
            deadline = socket.getdefaulttimeout()
        return fetch(url, payload=payload, method=method, headers=headers,
                     allow_truncated=allow_truncated,
                     follow_redirects=follow_redirects, deadline=deadline,
                     validate_certificate=validate_certificate)
    return fixed_fetch
项目:httplib2    作者:httplib2    | 项目源码 | 文件源码
def _new_fixed_fetch(validate_certificate):
    def fixed_fetch(url, payload=None, method="GET", headers={},
                    allow_truncated=False, follow_redirects=True,
                    deadline=None):
        if deadline is None:
            deadline = socket.getdefaulttimeout()
        return fetch(url, payload=payload, method=method, headers=headers,
                     allow_truncated=allow_truncated,
                     follow_redirects=follow_redirects, deadline=deadline,
                     validate_certificate=validate_certificate)
    return fixed_fetch
项目:Texty    作者:sarthfrey    | 项目源码 | 文件源码
def _new_fixed_fetch(validate_certificate):
        def fixed_fetch(url, payload=None, method="GET", headers={},
                        allow_truncated=False, follow_redirects=True,
                        deadline=None):
            if deadline is None:
                deadline = socket.getdefaulttimeout() or 5
            return fetch(url, payload=payload, method=method, headers=headers,
                         allow_truncated=allow_truncated,
                         follow_redirects=follow_redirects, deadline=deadline,
                         validate_certificate=validate_certificate)
        return fixed_fetch
项目:office-interoperability-tools    作者:milossramek    | 项目源码 | 文件源码
def request(self, method, url, body, headers):
      # Calculate the absolute URI, which fetch requires
      netloc = self.host
      if self.port:
        netloc = '%s:%s' % (self.host, self.port)
      absolute_uri = '%s://%s%s' % (self.scheme, netloc, url)
      try:
        try: # 'body' can be a stream.
          body = body.read()
        except AttributeError:
          pass
        response = fetch(absolute_uri, payload=body, method=method,
            headers=headers, allow_truncated=False, follow_redirects=False,
            deadline=self.timeout,
            validate_certificate=self.validate_certificate)
        self.response = ResponseDict(response.headers)
        self.response['status'] = str(response.status_code)
        self.response['reason'] = httplib.responses.get(response.status_code, 'Ok')
        self.response.status = response.status_code
        setattr(self.response, 'read', lambda : response.content)

      # Make sure the exceptions raised match the exceptions expected.
      except InvalidURLError:
        raise socket.gaierror('')
      except (DownloadError, ResponseTooLargeError, SSLCertificateError):
        raise httplib.HTTPException()
项目:touch-pay-client    作者:HackPucBemobi    | 项目源码 | 文件源码
def fetch(url, data=None, headers=None,
          cookie=Cookie.SimpleCookie(),
          user_agent='Mozilla/5.0'):
    headers = headers or {}
    if data is not None:
        data = urllib.urlencode(data)
    if user_agent:
        headers['User-agent'] = user_agent
    headers['Cookie'] = ' '.join(
        ['%s=%s;' % (c.key, c.value) for c in cookie.values()])
    try:
        from google.appengine.api import urlfetch
    except ImportError:
        req = urllib2.Request(url, data, headers)
        html = urllib2.urlopen(req).read()
    else:
        method = ((data is None) and urlfetch.GET) or urlfetch.POST
        while url is not None:
            response = urlfetch.fetch(url=url, payload=data,
                                      method=method, headers=headers,
                                      allow_truncated=False, follow_redirects=False,
                                      deadline=10)
            # next request will be a get, so no need to send the data again
            data = None
            method = urlfetch.GET
            # load cookies from the response
            cookie.load(response.headers.get('set-cookie', ''))
            url = response.headers.get('location')
        html = response.content
    return html
项目:touch-pay-client    作者:HackPucBemobi    | 项目源码 | 文件源码
def geocode(address):
    try:
        a = urllib_quote(address)
        txt = fetch('http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&address=%s' % a)
        item = regex_geocode.search(txt)
        (la, lo) = (float(item.group('la')), float(item.group('lo')))
        return (la, lo)
    except:
        return (0.0, 0.0)
项目:touch-pay-client    作者:HackPucBemobi    | 项目源码 | 文件源码
def reverse_geocode(lat, lng, lang=None):
    """ Try to get an approximate address for a given latitude, longitude. """
    if not lang:
        lang = current.T.accepted_language
    try:
        return json.loads(fetch('http://maps.googleapis.com/maps/api/geocode/json?latlng=%(lat)s,%(lng)s&language=%(lang)s' % locals()))['results'][0]['formatted_address']
    except:
        return ''
项目:daytravel    作者:warsawpact    | 项目源码 | 文件源码
def obtain_bearer_token(host, path):
    """Given a bearer token, send a GET request to the API.

    Args:
        host (str): The domain host of the API.
        path (str): The path of the API after the domain.
        params (dict): An optional set of query parameters in the request.

    Returns:
        str: OAuth bearer token, obtained using client_id and client_secret.

    Raises:
        HTTPError: An error occurs from the HTTP request.
    """
    url = '{0}{1}'.format(host, quote(path.encode('utf8')))
    data = urlencode({
        'client_id': CLIENT_ID,
        'client_secret': CLIENT_SECRET,
        'grant_type': GRANT_TYPE,
    })
    print('@@@@@@@@@' + CLIENT_ID)
    headers = {
        'content-type': 'application/x-www-form-urlencoded',
    }
    result = urlfetch.fetch(
        url=url,
        payload=data,
        method=urlfetch.POST,
        headers=headers)
    print('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@' + result.content)
    return "BIO6_LpbIcFkeKDB9SsSAONt3lE2IwrdiTxUeq-Ag1MKOzSc4m-8QyPjdV6WmI27ySuLEKv7czHoJmJjFHrCyjfgxucTvKPpJG9JCsg_08KCz4J-WrEfeaiACoJ2WXYx"
项目:daytravel    作者:warsawpact    | 项目源码 | 文件源码
def request(host, path, bearer_token, params):
    """Given a bearer token, send a GET request to the API.

    Args:
        host (str): The domain host of the API.
        path (str): The path of the API after the domain.
        bearer_token (str): OAuth bearer token, obtained using client_id and client_secret.
        params (dict): An optional set of query parameters in the request.

    Returns:
        dict: The JSON response from the request.

    Raises:
        HTTPError: An error occurs from the HTTP request.
    """
    url = '{0}{1}?{2}'.format(
      host,
      quote(path.encode('utf8')),
      urllib.urlencode(params))
    headers = {
        'Authorization': 'Bearer %s' % bearer_token,
    }

    logging.info(u'Querying {0} ...'.format(url))
    result = urlfetch.fetch(
        url=url,
        method=urlfetch.GET,
        headers=headers)
    logging.info('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@' + result.content)
    return json.loads(result.content)
项目:plaid-python-legacy    作者:plaid    | 项目源码 | 文件源码
def _urlfetch_http_request(url, method, data):
    from google.appengine.api import urlfetch

    method = method.upper()
    qs = urlencode(data)
    if method == 'POST':
        payload = qs
    else:
        payload = None
        url += '?' + qs

    headers = {
        'User-Agent': 'Plaid Python v{}'.format(__version__)
    }

    res = urlfetch.fetch(
        url,
        follow_redirects=True,
        method=method,
        payload=payload,
        headers=headers,
        deadline=60  # seconds
    )

    # Add consistent interface across requests library and urlfetch
    res.ok = res.status_code >= 200 and res.status_code < 300
    res.text = res.content
    return res
项目:true_review_web2py    作者:lucadealfaro    | 项目源码 | 文件源码
def fetch(url, data=None, headers=None,
          cookie=Cookie.SimpleCookie(),
          user_agent='Mozilla/5.0'):
    headers = headers or {}
    if not data is None:
        data = urllib.urlencode(data)
    if user_agent:
        headers['User-agent'] = user_agent
    headers['Cookie'] = ' '.join(
        ['%s=%s;' % (c.key, c.value) for c in cookie.values()])
    try:
        from google.appengine.api import urlfetch
    except ImportError:
        req = urllib2.Request(url, data, headers)
        html = urllib2.urlopen(req).read()
    else:
        method = ((data is None) and urlfetch.GET) or urlfetch.POST
        while url is not None:
            response = urlfetch.fetch(url=url, payload=data,
                                      method=method, headers=headers,
                                      allow_truncated=False, follow_redirects=False,
                                      deadline=10)
            # next request will be a get, so no need to send the data again
            data = None
            method = urlfetch.GET
            # load cookies from the response
            cookie.load(response.headers.get('set-cookie', ''))
            url = response.headers.get('location')
        html = response.content
    return html
项目:true_review_web2py    作者:lucadealfaro    | 项目源码 | 文件源码
def geocode(address):
    try:
        a = urllib.quote(address)
        txt = fetch('http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&address=%s' % a)
        item = regex_geocode.search(txt)
        (la, lo) = (float(item.group('la')), float(item.group('lo')))
        return (la, lo)
    except:
        return (0.0, 0.0)
项目:true_review_web2py    作者:lucadealfaro    | 项目源码 | 文件源码
def reverse_geocode(lat, lng, lang=None):
    """ Try to get an approximate address for a given latitude, longitude. """
    if not lang:
        lang = current.T.accepted_language
    try:
        return json_parser.loads(fetch('http://maps.googleapis.com/maps/api/geocode/json?latlng=%(lat)s,%(lng)s&language=%(lang)s' % locals()))['results'][0]['formatted_address']
    except:
        return ''
项目:theopencorps    作者:theopencorps    | 项目源码 | 文件源码
def request(self, resource, **kwargs):
        """
        Convenience for making requests

        Synchronous returns an object with status_code and content members.

        FIXME this should really return JSON to match ASync
        """
        request_args = self._create_request_args(**kwargs)
        result = urlfetch.fetch(self._endpoint + resource, **request_args)

        msg = "%s: %s%s %d (returned %d bytes)" % (
            request_args["method"], self._endpoint, resource,
            result.status_code, len(result.content))

        if result.status_code == 200:
            self.log.info(msg)
            self.log.debug("Sent: %s", repr(request_args["headers"]))
            self.log.debug("payload: %s", repr(request_args["payload"]))
            self.log.debug("Got:  %s", repr(result.content))
        else:
            self.log.warning(msg)
            self.log.info("Sent %s", repr(request_args["headers"]))
            self.log.debug("payload: %s", repr(request_args["payload"]))
            self.log.info(result.content)
        return result