當前位置: 首頁>>代碼示例>>Python>>正文


Python request.headers方法代碼示例

本文整理匯總了Python中urllib.request.headers方法的典型用法代碼示例。如果您正苦於以下問題:Python request.headers方法的具體用法?Python request.headers怎麽用?Python request.headers使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在urllib.request的用法示例。


在下文中一共展示了request.headers方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: request_to_dict

# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import headers [as 別名]
def request_to_dict(self, request):
        '''
        Convert Request object to a dict.
        modified from scrapy.utils.reqser
        '''
        req_dict = {
            # urls should be safe (safe_string_url)
            'url': to_unicode(request.url),
            'method': request.method,
            'headers': dict(request.headers),
            'body': request.body,
            'cookies': request.cookies,
            'meta': request.meta,
            '_encoding': request._encoding,
            'priority': request.priority,
            'dont_filter': request.dont_filter,
             #  callback/errback are assumed to be a bound instance of the spider
            'callback': None if request.callback is None else request.callback.__name__,
            'errback': None if request.errback is None else request.errback.__name__,
        }
        return req_dict 
開發者ID:istresearch,項目名稱:scrapy-cluster,代碼行數:23,代碼來源:distributed_scheduler.py

示例2: modify_request

# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import headers [as 別名]
def modify_request(self, http_request):
    """Sets the Authorization header and includes a digital signature.

    Calculates a digital signature using the private RSA key, a timestamp
    (uses now at the time this method is called) and a random nonce.

    Args:
      http_request: The atom.http_core.HttpRequest which contains all of the
          information needed to send a request to the remote server. The
          URL and the method of the request must be already set and cannot be
          changed after this token signs the request, or the signature will
          not be valid.
    """
    timestamp = str(int(time.time()))
    nonce = ''.join([str(random.randint(0, 9)) for i in range(15)])
    data = build_auth_sub_data(http_request, timestamp, nonce)
    signature = generate_signature(data, self.rsa_private_key)
    http_request.headers['Authorization'] = (
        '%s%s sigalg="rsa-sha1" data="%s" sig="%s"' % (AUTHSUB_AUTH_LABEL,
            self.token_string, data, signature)) 
開發者ID:kuri65536,項目名稱:python-for-android,代碼行數:22,代碼來源:gauth.py

示例3: generate_request_for_access_token

# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import headers [as 別名]
def generate_request_for_access_token(
    request_token, auth_server_url=ACCESS_TOKEN_URL):
  """Creates a request to ask the OAuth server for an access token.

  Requires a request token which the user has authorized. See the
  documentation on OAuth with Google Data for more details:
  http://code.google.com/apis/accounts/docs/OAuth.html#AccessToken

  Args:
    request_token: An OAuthHmacToken or OAuthRsaToken which the user has
        approved using their browser.
    auth_server_url: (optional) The URL at which the OAuth access token is
        requested. Defaults to
        https://www.google.com/accounts/OAuthGetAccessToken

  Returns:
    A new HttpRequest object which can be sent to the OAuth server to
    request an OAuth Access Token.
  """
  http_request = atom.http_core.HttpRequest(auth_server_url, 'POST')
  http_request.headers['Content-Length'] = '0'
  return request_token.modify_request(http_request) 
開發者ID:kuri65536,項目名稱:python-for-android,代碼行數:24,代碼來源:gauth.py

示例4: __init__

# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import headers [as 別名]
def __init__(self, http_response, response_body=None):
        """Sets the HTTP information in the error.

        Args:
          http_response: The response from the server, contains error information.
          response_body: string (optional) specified if the response has already
                         been read from the http_response object.
        """
        body = response_body or http_response.read()

        self.status = http_response.status
        self.reason = http_response.reason
        self.body = body
        self.headers = atom.http_core.get_headers(http_response)

        self.error_msg = 'Invalid response %s.' % self.status
        try:
            json_from_body = simplejson.loads(body.decode('utf-8'))
            if isinstance(json_from_body, dict):
                self.error_msg = json_from_body.get('error', self.error_msg)
        except (ValueError, JSONDecodeError):
            pass 
開發者ID:dvska,項目名稱:gdata-python3,代碼行數:24,代碼來源:gauth.py

示例5: modify_request

# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import headers [as 別名]
def modify_request(self, http_request):
        """Sets the Authorization header and includes a digital signature.

        Calculates a digital signature using the private RSA key, a timestamp
        (uses now at the time this method is called) and a random nonce.

        Args:
          http_request: The atom.http_core.HttpRequest which contains all of the
              information needed to send a request to the remote server. The
              URL and the method of the request must be already set and cannot be
              changed after this token signs the request, or the signature will
              not be valid.
        """
        timestamp = str(int(time.time()))
        nonce = ''.join([str(random.randint(0, 9)) for i in range(15)])
        data = build_auth_sub_data(http_request, timestamp, nonce)
        signature = generate_signature(data, self.rsa_private_key)
        http_request.headers['Authorization'] = (
            '%s%s sigalg="rsa-sha1" data="%s" sig="%s"' % (AUTHSUB_AUTH_LABEL,
                                                           self.token_string, data, signature)) 
開發者ID:dvska,項目名稱:gdata-python3,代碼行數:22,代碼來源:gauth.py

示例6: upgrade_to_access_token

# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import headers [as 別名]
def upgrade_to_access_token(request_token, server_response_body):
    """Extracts access token information from response to an upgrade request.

    Once the server has responded with the new token info for the OAuth
    access token, this method modifies the request_token to set and unset
    necessary fields to create valid OAuth authorization headers for requests.

    Args:
      request_token: An OAuth token which this function modifies to allow it
          to be used as an access token.
      server_response_body: str The server's response to an OAuthAuthorizeToken
          request. This should contain the new token and token_secret which
          are used to generate the signature and parameters of the Authorization
          header in subsequent requests to Google Data APIs.

    Returns:
      The same token object which was passed in.
    """
    token, token_secret = oauth_token_info_from_body(server_response_body)
    request_token.token = token
    request_token.token_secret = token_secret
    request_token.auth_state = ACCESS_TOKEN
    request_token.next = None
    request_token.verifier = None
    return request_token 
開發者ID:dvska,項目名稱:gdata-python3,代碼行數:27,代碼來源:gauth.py

示例7: session

# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import headers [as 別名]
def session(callback):
    cookie_name = 'session'
    serializer = URLSafeSerializer(conf['SECRET'])

    def inner(*args, **kwargs):
        data_raw = data = request.get_cookie(cookie_name)
        if data_raw:
            try:
                data = serializer.loads(data_raw)
            except (BadSignature, BadData):
                data = None

        if data:
            conf['USER'] = data['username']

        request.session = data or {}

        try:
            return callback(*args, **kwargs)
        finally:
            if request.session:
                save(request.session)
            elif not data_raw:
                pass
            else:
                response.delete_cookie(cookie_name)

    def save(session):
        cookie_opts = {
            # keep session for 3 days
            'max_age': 3600 * 24 * 3,

            # for security
            'httponly': True,
            'secure': request.headers.get('X-Forwarded-Proto') == 'https',
        }
        data = serializer.dumps(session)
        response.set_cookie(cookie_name, data, **cookie_opts)
    return inner 
開發者ID:naspeh,項目名稱:mailur,代碼行數:41,代碼來源:web.py

示例8: nginx

# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import headers [as 別名]
def nginx():
    h = request.headers
    try:
        login, pw = h['Auth-User'], h['Auth-Pass']
        protocol = h['Auth-Protocol']
    except KeyError as e:
        return abort(400, repr(e))

    if login in conf['IMAP_OFF']:
        response.set_header('Auth-Status', 'Disabled')
        response.set_header('Auth-Wait', 3)
        return ''

    port = {'imap': '143', 'smtp': '25'}[protocol]

    try:
        local.connect(login, pw)
        response.set_header('Auth-Status', 'OK')
        response.set_header('Auth-Server', '127.0.0.1')
        response.set_header('Auth-Port', port)
    except imap.Error as e:
        response.set_header('Auth-Status', str(e))
        response.set_header('Auth-Wait', 3)
    return '' 
開發者ID:naspeh,項目名稱:mailur,代碼行數:26,代碼來源:web.py

示例9: proxy

# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import headers [as 別名]
def proxy(request, scheme, netloc, timeout=5):
    """Proxies and return the result from the other server.

    - scheme: http or https
    - netloc: proxy location
    """
    parsed = urlparse(request.url)
    path = parsed.path
    params = parsed.params
    query = parsed.query
    fragment = parsed.fragment
    url = urlunparse((scheme, netloc, path, params, query, fragment))
    method = request.method
    data = request.body

    # copying all X- headers
    xheaders = {}
    for header, value in list(request.headers.items()):
        if not header.startswith('X-'):
            continue
        xheaders[header] = value

    if 'X-Forwarded-For' not in request.headers:
        xheaders['X-Forwarded-For'] = request.remote_addr

    if hasattr(request, '_authorization'):
        xheaders['Authorization'] = request._authorization

    status, headers, body = get_url(url, method, data, timeout=timeout,
                                    extra_headers=xheaders)

    return Response(body, status, list(headers.items())) 
開發者ID:mozilla-services,項目名稱:shavar,代碼行數:34,代碼來源:http_helpers.py

示例10: _refresh

# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import headers [as 別名]
def _refresh(self, request):
    """Refresh the access_token using the refresh_token.

    Args:
       http: An instance of httplib2.Http.request
           or something that acts like it.
    """
    body = urllib.parse.urlencode({
      'grant_type': 'refresh_token',
      'client_id': self.client_id,
      'client_secret': self.client_secret,
      'refresh_token' : self.refresh_token
      })
    headers = {
        'user-agent': self.user_agent,
    }

    http_request = atom.http_core.HttpRequest(
        uri=self.token_uri, method='POST', headers=headers)
    http_request.add_body_part(
        body, mime_type='application/x-www-form-urlencoded')
    response = request(http_request)
    body = response.read()
    if response.status == 200:
      self._extract_tokens(body)
    else:
      self._invalid = True
    return response 
開發者ID:kuri65536,項目名稱:python-for-android,代碼行數:30,代碼來源:gauth.py

示例11: get_access_token

# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import headers [as 別名]
def get_access_token(self, code):
    """Exhanges a code for an access token.

    Args:
      code: string or dict, either the code as a string, or a dictionary
        of the query parameters to the redirect_uri, which contains
        the code.
    """

    if not (isinstance(code, str) or isinstance(code, str)):
      code = code['code']

    body = urllib.parse.urlencode({
      'grant_type': 'authorization_code',
      'client_id': self.client_id,
      'client_secret': self.client_secret,
      'code': code,
      'redirect_uri': self.redirect_uri,
      'scope': self.scope
      })
    headers = {
      'user-agent': self.user_agent,
    }
    http_client = atom.http_core.HttpClient()
    http_request = atom.http_core.HttpRequest(uri=self.token_uri, method='POST',
                                              headers=headers)
    http_request.add_body_part(data=body, mime_type='application/x-www-form-urlencoded')
    response = http_client.request(http_request)
    body = response.read()
    if response.status == 200:
      self._extract_tokens(body)
      return self
    else:
      error_msg = 'Invalid response %s.' % response.status
      try:
        d = simplejson.loads(body)
        if 'error' in d:
          error_msg = d['error']
      except:
        pass
      raise OAuth2AccessTokenError(error_msg) 
開發者ID:kuri65536,項目名稱:python-for-android,代碼行數:43,代碼來源:gauth.py

示例12: _refresh

# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import headers [as 別名]
def _refresh(self, request):
        """Refresh the access_token using the refresh_token.

        Args:
          request: The atom.http_core.HttpRequest which contains all of the
              information needed to send a request to the remote server.
        """
        body = urllib.parse.urlencode({
            'grant_type': 'refresh_token',
            'client_id': self.client_id,
            'client_secret': self.client_secret,
            'refresh_token': self.refresh_token
        })
        headers = {
            'user-agent': self.user_agent,
        }

        http_request = atom.http_core.HttpRequest(
            uri=self.token_uri, method='POST', headers=headers)
        http_request.add_body_part(
            body, mime_type='application/x-www-form-urlencoded')
        response = request(http_request)
        body = response.read()
        if response.status == 200:
            self._extract_tokens(body)
        else:
            self._invalid = True
        return response 
開發者ID:dvska,項目名稱:gdata-python3,代碼行數:30,代碼來源:gauth.py

示例13: __query

# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import headers [as 別名]
def __query(self, requestString, params):
        request = self._queryRequest(self._endpoint, requestString, params=params)
        if not isinstance(request, urllib.request.Request):
            request = urllib.request.Request(request)
        request.headers['User-Agent'] = self._userAgent()
        try:
            response = urllib.request.urlopen(request)
        except urllib.request.HTTPError as err:
            raise Exception('The requested data could not be downloaded. ' + str(err))
        except:
            raise Exception('The requested data could not be downloaded.  Please check whether your internet connection is working.')
        encoding = response.info().get_content_charset('utf-8')
        r = response.read().decode(encoding)
        return ujson.loads(r) if self.__jsonResult else r 
開發者ID:mocnik-science,項目名稱:osm-python-tools,代碼行數:16,代碼來源:cacheObject.py

示例14: download_and_extract_zip

# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import headers [as 別名]
def download_and_extract_zip(url: str, name: str) -> None:
    """
    Downloads a zip file under a URL, extracts its contents into a folder with the name
    argument and gives chmod 755 to all the files it contains. Files are downloaded and
    extracted into special folders in the temp folder of the machine.
    :param url: The URL of the zip file
    :param name: The name that will be given to the folder containing the extracted data
    """
    zip_dir, bin_dir = get_tmp_dir()
    url_hash = "-" + hashlib.md5(url.encode()).hexdigest()
    binary_path = os.path.join(bin_dir, name + url_hash)
    if os.path.exists(binary_path):
        shutil.rmtree(binary_path)

    # Download zip
    try:
        request = urllib.request.urlopen(url, timeout=30)
    except urllib.error.HTTPError as e:  # type: ignore
        e.msg += " " + url
        raise
    zip_size = int(request.headers["content-length"])
    zip_file_path = os.path.join(zip_dir, str(uuid.uuid4()) + ".zip")
    with open(zip_file_path, "wb") as zip_file:
        downloaded = 0
        while True:
            buffer = request.read(BLOCK_SIZE)
            if not buffer:
                # There is nothing more to read
                break
            downloaded += len(buffer)
            zip_file.write(buffer)
            downloaded_percent = downloaded / zip_size * 100
            print_progress(f"  Downloading {name}", downloaded_percent)
        print("")

    # Extraction
    with ZipFileWithProgress(zip_file_path, "r") as zip_ref:
        zip_ref.extract_zip(f"  Extracting  {name}", binary_path)  # type: ignore
    print("")

    # Clean up zip
    print_progress(f"  Cleaning up {name}", 0)
    os.remove(zip_file_path)

    # Give permission
    for f in glob.glob(binary_path + "/**/*", recursive=True):
        # 16877 is octal 40755, which denotes a directory with permissions 755
        os.chmod(f, 16877)
    print_progress(f"  Cleaning up {name}", 100)
    print("") 
開發者ID:StepNeverStop,項目名稱:RLs,代碼行數:52,代碼來源:binary_utils.py

示例15: instructions

# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import headers [as 別名]
def instructions(keys):
	authenticated = False
	if "Cookie" in request.headers:
		cookies = request.headers["Cookie"].split(";")
		for c in cookies:
			if c.strip().startswith("apikey="):
				authenticated = checkAPIkey(c.strip()[7:])

	if "token" in keys and authenticated:
		token = keys.get("token")
		parameters = {
			"method":"auth.getSession",
			"token":token,
			"api_key":get_settings("LASTFM_API_KEY")
		}
		response = urllib.request.urlopen("http://ws.audioscrobbler.com/2.0/?" + lfmbuild(parameters))
		xml = response.read()
		data = ET.fromstring(xml)
		if data.attrib.get("status") == "ok":
			username = data.find("session").find("name").text
			sessionkey = data.find("session").find("key").text

			update_settings("settings/settings.ini",{"LASTFM_API_SK":sessionkey,"LASTFM_USERNAME":username},create_new=True)

		return "/proxy"

	else:
		key,secret,sessionkey,name = get_settings("LASTFM_API_KEY","LASTFM_API_SECRET","LASTFM_API_SK","LASTFM_USERNAME")

		if key is None:
			lastfm = "<td>No Last.fm key provided</td>"
		elif secret is None:
			lastfm = "<td>No Last.fm secret provided</td>"
		elif sessionkey is None and authenticated:
			url = "http://www.last.fm/api/auth/?api_key=" + key + "&cb="
			lastfm = "<td class='button'><a id='lastfmlink' href='" + url + "'><div>Connect</div></a></td>"
		elif sessionkey is None:
			lastfm = "<td>Not active</td>"
		else:

			lastfm = "<td>Account: " + name + "</td>"



	return {"KEY_STATUS_LASTFM":lastfm},[] 
開發者ID:krateng,項目名稱:maloja,代碼行數:47,代碼來源:proxy.py


注:本文中的urllib.request.headers方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。