Skip to content

Instantly share code, notes, and snippets.

@Denilson-Semedo
Last active June 8, 2023 01:23
Show Gist options
  • Save Denilson-Semedo/818b3e79a4866223c90bb05cabe07696 to your computer and use it in GitHub Desktop.
Save Denilson-Semedo/818b3e79a4866223c90bb05cabe07696 to your computer and use it in GitHub Desktop.
httplib2

httplib2

httplib2 is a Python library that provides a comprehensive HTTP client for making requests and handling responses. It offers a simple and intuitive interface, making it easier to work with HTTP protocols in your Python applications. The library supports both HTTP/1.1 and HTTPS, handling redirects, cookies, authentication, caching, and more.

Installation

To install httplib2 using pip, you can run the following command:

pip install httplib2 

Make sure you have pip installed and configured properly on your system before running the above command. This will fetch and install the latest version of httplib2 from the Python Package Index (PyPI), making it ready for use in your Python projects.

httplib2 is not included in the Python standard library, so it's necessary to install it separately. By using pip, you can easily manage dependencies and keep the library up to date.

Official Documentation

Command/Function Description Example
Importing
import httplib2 Imports the httplib2 library for use in your Python program. import httplib2
Creating an HTTP object
http = httplib2.Http() Creates an instance of the Http class to make HTTP requests. http = httplib2.Http()
Making a GET request
response, content = http.request(url, 'GET') Sends a GET request to the specified URL and returns the response and content. response, content = http.request('http://example.com', 'GET')
Making a POST request
response, content = http.request(url, 'POST', headers=headers, body=body) Sends a POST request to the specified URL with optional headers and body, and returns the response and content. python\nheaders = {'Content-Type': 'application/json'}\nbody = '{"key": "value"}'\nresponse, content = http.request('http://example.com', 'POST', headers=headers, body=body)\n
Handling response status
response.status Returns the HTTP status code of the response. print(response.status)
Handling response content
content.decode() Decodes the response content to a string. print(content.decode())
Following redirects
http.follow_redirects Determines whether to automatically follow redirects (default: True). http.follow_redirects = False
Setting timeout
http.timeout Sets the maximum time to wait for a response in seconds (default: None). http.timeout = 10
Enabling caching
http.cache = '.cache' Enables response caching and specifies the cache directory. http.cache = '.cache'
Enabling proxy
http.proxy_info = httplib2.ProxyInfo(httplib2.socks.PROXY_TYPE_HTTP, 'proxy.example.com', 8080) Enables HTTP proxy with the specified host and port. http.proxy_info = httplib2.ProxyInfo(httplib2.socks.PROXY_TYPE_HTTP, 'proxy.example.com', 8080)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment