Python requests मॉड्यूल
In this page:
import requests
response = requests.get("url")
response.status_code
response.json()
requests का परिचय
requests Python से HTTP calls करने के लिए standard third-party library है, जिसे इसके काफी simple API के कारण built-in urllib से ज़्यादा पसंद किया जाता है।
इनमें से कोई भी example चलाने से पहले इसे pip install requests से install करें, क्योंकि यह Python की standard library का हिस्सा नहीं है।
उदाहरण: Introduction to requests
# pip install requests
import requests
print(requests.__name__)
GET Requests भेजना
एक GET request server-side कुछ भी बदले बिना server से data प्राप्त करती है।
requests.get(url) को call करना एक Response object लौटाता है जिसका .text raw response body देता है और .status_code numeric HTTP status देता है, जिससे आप लौटाए गए data पर भरोसा करने से पहले सफलता जाँच सकते हैं।
उदाहरण: Sending GET Requests
from unittest.mock import patch
import requests
with patch("requests.get") as mock_get: # simulates the network call without a real request
mock_get.return_value.status_code = 200
mock_get.return_value.text = "response body"
response = requests.get("https://api.example.com/data")
print(response.status_code, response.text)
POST Requests भेजना
एक POST request server को data भेजती है, आमतौर पर कोई resource बनाने या update करने के लिए।
requests.post() के data parameter को कोई dictionary पास करना उसे request body में form data के रूप में अपने-आप encode कर देता है, ठीक वैसे ही जैसे कोई HTML form submission वही जानकारी भेजता।
उदाहरण: Sending POST Requests
from unittest.mock import patch
import requests
with patch("requests.post") as mock_post:
mock_post.return_value.status_code = 201
response = requests.post("https://api.example.com/users", data={"name": "Alex"}) # dict is sent as form data
print(response.status_code)
Request Headers संभालना
HTTP headers किसी request के साथ metadata ले जाते हैं -- जैसे body किस format में है यह बताने के लिए Content-Type, या अपने client की पहचान बताने के लिए कोई custom User-Agent। इन्हें सेट करने के लिए किसी भी request method के headers parameter में एक dictionary पास करें।
उदाहरण: Handling Request Headers
from unittest.mock import patch
import requests
with patch("requests.get") as mock_get:
mock_get.return_value.status_code = 200
headers = {"User-Agent": "MyApp/1.0"} # metadata sent alongside the request
requests.get("https://api.example.com", headers=headers)
mock_get.assert_called_with("https://api.example.com", headers=headers)
print("Headers sent:", headers)
Timeouts और Exceptions संभालना
अगर कोई server धीमा या unresponsive हो तो network calls अनंत काल तक hang हो सकती हैं, इसलिए किसी request को timeout=N (seconds) पास करना requests को हमेशा के लिए block होने की बजाय एक exception raise करने के लिए कहता है।
Calls को try/except requests.exceptions.RequestException block में लपेटना आपके program को timeouts और connection failures दोनों को gracefully संभालने देता है।
उदाहरण: Handling Timeouts and Exceptions
import requests
try:
requests.get("https://api.example.com", timeout=0.001) # raises instead of blocking forever
except requests.exceptions.RequestException as e:
print("Request failed:", type(e).__name__)
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Python NumPy Introduction
- Python NumPy Arrays
- Python Pandas Introduction
- Python Pandas DataFrame
- Python Matplotlib Basics
- Python Data Visualization
- Python Statistics Module
- Python CSV & Data Analysis
- Python requests Module
- Python JSON & APIs
- Python Web Scraping Basics
- Python Flask Introduction
- Python Django Introduction
- Python MongoDB