← Back to Python Course | Chapter 13: Data Science & Web | Lesson 9 of 14

Python requests मॉड्यूल

requests मॉड्यूल आपके program को web addresses पर जाकर जानकारी वापस लाने देता है, जैसे कोई चिट्ठी भेजकर उसका जवाब पढ़ना। यही तरीका है जिससे Python websites से बात करता है।
Syntax
python
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

python
# 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

python
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

python
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

python
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

python
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__)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.