Python httplib.PRECONDITION_FAILED Examples

The following are 3 code examples of httplib.PRECONDITION_FAILED(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module httplib , or try the search function .
Example #1
Source File: directory.py    From loaner with Apache License 2.0 6 votes vote down vote up
def disable_chrome_device(self, device_id):
    """Disable a Chrome device within an organization.

    Args:
      device_id: String, The unique Chrome device id.

    Raises:
      DirectoryRPCError: An error when the RPC call to the directory API fails.
    """
    logging.info('Disabling chrome device %s.', device_id)
    try:
      self._client.chromeosdevices().action(
          customerId=constants.CUSTOMER_ID,
          resourceId=device_id,
          body={
              'action': 'disable'
          }).execute()
    except errors.HttpError as err:
      if err.resp.status == httplib.PRECONDITION_FAILED:
        raise DeviceAlreadyDisabledError(err.resp.reason)
      logging.error(
          'Directory API call to disable Chrome device failed with a %s '
          'exception because %s.', str(type(err)), err.resp.reason)
      raise DirectoryRPCError(err.resp.reason) 
Example #2
Source File: dev_appserver.py    From browserscope with Apache License 2.0 4 votes vote down vote up
def Dispatch(self, request, outfile, base_env_dict=None):
    """Reads the file and returns the response status and data."""
    full_path = self._path_adjuster.AdjustPath(request.path)
    status, data = self._read_data_file(full_path)
    content_type = self._static_file_config_matcher.GetMimeType(request.path)
    static_file = self._static_file_config_matcher.IsStaticFile(request.path)
    expiration = self._static_file_config_matcher.GetExpiration(request.path)
    current_etag = self.CreateEtag(data)
    if_match_etag = request.headers.get('if-match', None)
    if_none_match_etag = request.headers.get('if-none-match', '').split(',')

    http_headers = self._static_file_config_matcher.GetHttpHeaders(request.path)
    def WriteHeader(name, value):
      if http_headers.Get(name) is None:
        outfile.write('%s: %s\r\n' % (name, value))





    if if_match_etag and not self._CheckETagMatches(if_match_etag.split(','),
                                                    current_etag,
                                                    False):
      outfile.write('Status: %s\r\n' % httplib.PRECONDITION_FAILED)
      WriteHeader('ETag', current_etag)
      outfile.write('\r\n')
    elif self._CheckETagMatches(if_none_match_etag, current_etag, True):
      outfile.write('Status: %s\r\n' % httplib.NOT_MODIFIED)
      WriteHeader('ETag', current_etag)
      outfile.write('\r\n')
    else:



      outfile.write('Status: %d\r\n' % status)

      WriteHeader('Content-Type', content_type)


      if expiration:
        fmt = email.Utils.formatdate
        WriteHeader('Expires', fmt(time.time() + expiration, usegmt=True))
        WriteHeader('Cache-Control', 'public, max-age=%i' % expiration)


      if static_file:
        WriteHeader('ETag', '"%s"' % current_etag)

      for header in http_headers.iteritems():
        outfile.write('%s: %s\r\n' % header)

      outfile.write('\r\n')
      outfile.write(data) 
Example #3
Source File: stub_dispatcher.py    From python-compat-runtime with Apache License 2.0 4 votes vote down vote up
def _handle_put(gcs_stub, filename, param_dict, headers, payload):
  """Handle PUT."""
  if _iscopy(headers):
    return _copy(gcs_stub, filename, headers)


  token = _get_param('upload_id', param_dict)
  content_range = _ContentRange(headers)

  if _is_query_progress(content_range):
    return _find_progress(gcs_stub, filename, token)

  if not content_range.value:
    raise ValueError('Missing header content-range.', httplib.BAD_REQUEST)




  if (headers.get('x-goog-if-generation-match', None) == '0' and
      gcs_stub.head_object(filename) is not None):
    return _FakeUrlFetchResult(httplib.PRECONDITION_FAILED, {}, '')



  if not token:

    if content_range.length is None:
      raise ValueError('Content-Range must have a final length.',
                       httplib.BAD_REQUEST)
    elif not content_range.no_data and content_range.range[0] != 0:
      raise ValueError('Content-Range must specify complete object.',
                       httplib.BAD_REQUEST)
    else:

      token = gcs_stub.post_start_creation(filename, headers)

  try:
    gcs_stub.put_continue_creation(token,
                                   payload,
                                   content_range.range,
                                   content_range.length)
  except ValueError, e:
    return _FakeUrlFetchResult(e.args[1], {}, e.args[0])