5.2.1. HTTP — Manages interaction with web services via HTTP¶
| Threading: | This module is thread-safe |
|---|
The HTTP module contains functions that allow plug-ins to access resources via the HTTP protocol. Many tasks commonly associated with HTTP requests (like cookie handling and page caching) can be handled automatically by the framework. Many other framework modules, including XML, RSS, YAML and JSON share cookies and the page cache with this module.
This module defines the following functions:
- HTTP.Request(url, values=None, headers={}, cacheTime=None)¶
Returns the response from a HTTP request. The framework maintains a separate cookie jar and page cache for each plug-in, and automatically handles gzipped responses.
Parameters: - url (str) – The URL to request data from
- values (dict) – The values to use if making a POST request
- headers (dict) – Additional headers to include in the request
- cacheTime (int) – The maximum number of seconds the response should be cached for
Returns: The response of the HTTP request
Return type: str or data
- HTTP.PreCache(url, values=None, headers={}, cacheTime=None)¶
Behaves similarly to HTTP.Request(), but returns immediately & makes the request asynchronously. This function is useful when the developer wishes to cache data they may need shortly (for example, the next page of results in a search function).
Parameters: - url (str) – The URL to request data from
- values (dict) – The values to use if making a POST request
- headers (dict) – Additional headers to include in the request
- cacheTime (int) – The maximum number of seconds the response should be cached for
- HTTP.SetCacheTime(cacheTime)¶
Sets the global cache time for all requests. This value is used as a default value when no per-request cache time value is provided.
Parameter: cacheTime (int) – The maximum number of seconds to cache HTTP responses for
- HTTP.SetPassword(url, username, password, realm=None)¶
Sets the username & password to use for sites requiring HTTP basic authentication before granting access.
Parameters: - url (str) – The URL to set credentials for
- username (str) – The username to use for the given URL
- password (str) – The password to use for the given URL
- realm (str) – The realm to use for the given URL (optional, leave blank to match any realm)
- HTTP.SetHeader(key, value)¶
Sets a global HTTP header for all requests. This header will be added to all subsequent requests, unless overridden by a per-request header.
Parameters: - key (str) – The name of the header to set
- value (str) – The value to set for the given header