杰瑞科技汇

Python如何发起HTTP请求?

Of course! Making HTTP requests is a fundamental task in Python, whether you're fetching data from a public API, scraping a website, or interacting with a web service.

Python如何发起HTTP请求?-图1
(图片来源网络,侵删)

Here’s a comprehensive guide covering the most common ways to make HTTP requests in Python, from the built-in standard library to the most popular third-party libraries.

The Short Answer: The Easiest Way (For Most People)

For most modern Python projects, the requests library is the de-facto standard. It's incredibly user-friendly and powerful.

Installation First, you need to install it:

pip install requests

Simple GET Request This is the most common type of request, used to retrieve data.

Python如何发起HTTP请求?-图2
(图片来源网络,侵删)
import requests
# The URL you want to get data from
url = "https://api.github.com"
try:
    # Make the GET request
    response = requests.get(url)
    # Raise an exception if the request was unsuccessful (e.g., 404, 500)
    response.raise_for_status()
    # The response content is in JSON format, so we parse it
    data = response.json()
    # Print some of the data
    print(f"GitHub API Rate Limit Info: {data.get('rate_limit_url')}")
    print(f"Login successful for user: {data.get('current_user_url')}")
except requests.exceptions.HTTPError as errh:
    print(f"Http Error: {errh}")
except requests.exceptions.ConnectionError as errc:
    print(f"Error Connecting: {errc}")
except requests.exceptions.Timeout as errt:
    print(f"Timeout Error: {errt}")
except requests.exceptions.RequestException as err:
    print(f"Oops: Something Else: {err}")

Detailed Breakdown of Methods

Here are the most common methods for making HTTP requests in Python, ordered from most recommended to least.

Method 1: requests Library (Highly Recommended)

The requests library simplifies HTTP requests dramatically. It handles URL encoding, headers, session management, and JSON parsing automatically.

Key Features:

  • Simple, intuitive API.
  • Automatic decoding of response content (JSON, text, etc.).
  • Built-in timeout handling.
  • Session objects for persisting parameters across requests.
  • Excellent documentation and community support.

GET Request (with Parameters and Headers)

Often, you need to send query parameters or custom headers.

import requests
# API endpoint for searching for repositories
url = "https://api.github.com/search/repositories"
# Parameters for the query
params = {
    "q": "language:python",
    "sort": "stars",
    "order": "desc"
}
# Custom headers
headers = {
    "Accept": "application/vnd.github.v3+json",
    "User-Agent": "My-Python-App/1.0" # It's good practice to identify your app
}
try:
    response = requests.get(url, params=params, headers=headers, timeout=10)
    response.raise_for_status()  # Check for HTTP errors
    results = response.json()
    print(f"Found {results['total_count']} Python repositories.")
    # Print the name of the top 5 repos
    for repo in results['items'][:5]:
        print(f"- {repo['full_name']} (Stars: {repo['stargazers_count']})")
except requests.exceptions.RequestException as e:
    print(f"An error occurred: {e}")

POST Request (Sending Data)

POST requests are used to send data to a server, typically to create a new resource.

Python如何发起HTTP请求?-图3
(图片来源网络,侵删)
import requests
import json # For pretty printing the JSON response
url = "https://httpbin.org/post" # A test endpoint that echoes back what you send
# Data to send in the request body
payload = {
    "username": "testuser",
    "password": "supersecret",
    "email": "testuser@example.com"
}
# The 'json' parameter automatically sets the Content-Type header to application/json
# and serializes the Python dictionary to a JSON string.
try:
    response = requests.post(url, json=payload, timeout=10)
    response.raise_for_status()
    # The server's response is also JSON
    response_data = response.json()
    print("Server echoed back our data:")
    print(json.dumps(response_data, indent=2))
    # You can access specific parts of the response
    print("\nThe JSON data we sent was:")
    print(json.dumps(response_data['json'], indent=2))
except requests.exceptions.RequestException as e:
    print(f"An error occurred: {e}")

Method 2: httpx Library (The Modern Alternative)

httpx is a modern, fully featured HTTP client for Python 3. It's a great alternative to requests and has a very similar API. Its main advantage is that it supports both traditional HTTP/1.1 and the new HTTP/2 protocol.

Key Features:

  • Same great API as requests.
  • Supports both HTTP/1.1 and HTTP/2.
  • Supports asynchronous requests (async/await), which is crucial for high-performance applications.

Example (Synchronous)

pip install httpx
import httpx
url = "https://api.github.com"
with httpx.Client() as client:
    try:
        response = client.get(url)
        response.raise_for_status()
        data = response.json()
        print(f"Login URL: {data.get('current_user_url')}")
    except httpx.HTTPStatusError as e:
        print(f"Error response {e.response.status_code} while requesting {e.request.url}.")
    except httpx.RequestError as e:
        print(f"An error occurred while requesting {e.request.url}.")

Method 3: Built-in urllib (Standard Library)

You don't need to install anything to use urllib, as it's part of Python's standard library. However, its API is more verbose and less intuitive than requests or httpx. It's generally recommended to use requests unless you are in an environment where you cannot install external packages.

Example GET Request

import urllib.request
import json
url = "https://api.github.com"
try:
    # Create a request object and add headers
    req = urllib.request.Request(url, headers={'User-Agent': 'My-Python-App/1.0'})
    # Open the URL and read the response
    with urllib.request.urlopen(req, timeout=10) as response:
        # The response data is in bytes, so we decode it
        data = response.read().decode('utf-8')
        # Parse the JSON string
        data_dict = json.loads(data)
        print(f"GitHub API Rate Limit Info: {data_dict.get('rate_limit_url')}")
except urllib.error.HTTPError as e:
    print(f"HTTP Error: {e.code} {e.reason}")
except urllib.error.URLError as e:
    print(f"URL Error: {e.reason}")
except Exception as e:
    print(f"An error occurred: {e}")

Comparison and Recommendation

Feature requests httpx urllib (Standard Library)
Ease of Use Excellent (very intuitive) Excellent (API mirrors requests) Poor (verbose, complex)
Dependencies pip install requests pip install httpx None (built-in)
HTTP/2 Support No Yes No
Async Support No (use httpx or aiohttp) Yes (native async/await) No
JSON Handling Automatic (response.json()) Automatic (response.json()) Manual (json.loads())
Recommendation Best for most projects. Best for modern/async/HTTP2 apps. Only for no-dependency scripts.

Summary

Use Case Recommended Library
General purpose scripts, web scraping, API calls requests
High-performance, async, or HTTP/2 applications httpx
Quick scripts where you can't install external libs urllib
Low-level control, understanding the protocol urllib

For 99% of Python developers starting a new project, requests is the perfect choice. It's powerful, easy to learn, and will handle almost everything you need.

分享:
扫描分享到社交APP
上一篇
下一篇