With the advent of Internet and Big Data, handling HTTP Request is essential nowadays. Python provides 3 important libraries for this purpose:
- UrlLib (simple, high-level interface for HttpLib)
- Requests (3rd party, thus requires additional installation)
- HttpLib (allows complex customizations)
UrlLib and Requests are high-level libraries i.e. they hide certain complexities from the developers and therefore easy to be used.
HttpLib (a kind of a lower-level library) defines classes which implement the client side of the HTTP and HTTPS protocols which are then used by UrlLib (Python HTTP protocol client).
The Urllib module has been split into parts and renamed in Python 3 to urllib.request, urllib.parse, and urllib.error. (urllib — Open arbitrary resources by URL)
Requests is an elegant and simple HTTP library for Python, built for human beings. (Requests: HTTP for Humans)
As a rule of thumb, use Requests if you require simple HTTP client interface (Python Docs).
Read further:
https://www.geeksforgeeks.org/get-post-requests-using-python/
UrlLib
import urllib.request
with urllib.request.urlopen('http://www.python.org/') as f:
print(f.read(300))
Read further: - https://docs.python.org/3.9/library/urllib.request.html#:~:text=Examples
- https://realpython.com/urllib-request/
- https://www.javatpoint.com/python-urllib-library
- https://www.educative.io/answers/what-is-the-urllib-module-in-python-3
- https://pythonprogramming.net/urllib-tutorial-python-3/
- https://www.geeksforgeeks.org/python-urllib-module/
Requests
import requests r = requests.get('https://api.github.com/events') r.text
- https://requests.readthedocs.io/en/latest/user/quickstart/
- https://tech4lib.wordpress.com/2015/06/21/sending-http-requests-with-python-httplib-vs-requests/
- https://www.geeksforgeeks.org/python-requests-post-request-with-headers-and-body/
- https://www.scrapehero.com/how-to-fake-and-rotate-user-agents-using-python-3/
- https://www.shellhacks.com/python-requests-user-agent-web-scraping/
- https://stackoverflow.com/questions/1393324/given-a-url-to-a-text-file-what-is-the-simplest-way-to-read-the-contents-of-the
- https://www.geeksforgeeks.org/http-request-methods-python-requests/
- https://www.geeksforgeeks.org/python-requests-tutorial/
- https://www.w3schools.com/python/module_requests.asp
HttpLib
import http.client conn = http.client.HTTPSConnection("www.python.org") conn.request("GET", "/") r1 = conn.getresponse() print(r1.status, r1.reason)Read further:
- https://docs.python.org/3.9/library/http.client.html#:~:text=Examples
- https://python.readthedocs.io/en/v2.7.2/library/httplib.html
- https://cppsecrets.com/users/162697115104105115104103111121971084964121109971051084699111109/Python-Accessing-the-Web-Through-httplib-httpclient.php
- https://www.programcreek.com/python/example/187/httplib.HTTPConnection
- https://github.com/httplib2/httplib2
~
0 Comments