杰瑞科技汇

Python httplib GET请求如何使用?

Of course! The Python httplib library is the foundational, low-level HTTP client library. It's important to understand it, but for most applications, you'll want to use a higher-level library like requests.

Python httplib GET请求如何使用?-图1
(图片来源网络,侵删)

Let's break it down into three parts:

  1. The Modern Way: Using the requests library (Recommended)
  2. The Old Way: Using httplib (Python 2) and http.client (Python 3)
  3. A Practical Comparison

The Modern Way: Using the requests Library (Recommended)

For any new project, you should use the requests library. It's designed to be more human-friendly and handles many complexities of HTTP (like encoding, headers, and sessions) automatically.

First, you need to install it:

pip install requests

GET Request with requests

The syntax is incredibly simple.

Basic GET Request This will download the HTML content from example.com.

import requests
# The URL you want to send a GET request to
url = 'https://www.example.com'
try:
    # Send the GET request. The 'response' object contains the server's answer.
    response = requests.get(url)
    # Raise an exception if the request was unsuccessful (e.g., 404 Not Found, 500 Server Error)
    response.raise_for_status()
    # Print the status code
    print(f"Status Code: {response.status_code}")
    # Print the headers from the response
    print("\nResponse Headers:")
    print(response.headers)
    # Print the content of the response (the HTML)
    print("\nResponse Content (first 200 chars):")
    print(response.text[:200])
except requests.exceptions.RequestException as e:
    # This will catch any errors related to the request (e.g., connection error, timeout)
    print(f"An error occurred: {e}")

GET Request with Query Parameters To add parameters like ?key1=value1&key2=value2, you can pass them in a dictionary.

import requests
# The base URL
url = 'https://httpbin.org/get'
# The parameters to add to the URL
params = {
    'name': 'Alice',
    'age': 30,
    'is_student': False # requests will convert this to 'false'
}
try:
    # requests.get() automatically builds the full URL with the query parameters
    response = requests.get(url, params=params)
    response.raise_for_status()
    # The final URL that was requested
    print(f"Requested URL: {response.url}")
    # The response from httpbin.org will echo back the parameters we sent
    print("\nResponse JSON (sent parameters):")
    # .json() parses the JSON response into a Python dictionary
    print(response.json())
except requests.exceptions.RequestException as e:
    print(f"An error occurred: {e}")

The Old Way: Using httplib / http.client

The httplib library was renamed to http.client in Python 3. It's more verbose and requires you to handle more details manually. You should only use this if you're working on very old code or have a specific need for low-level control.

GET Request with http.client (Python 3)

This is how you'd perform the same basic GET request as above.

import http.client
import json # For pretty printing the JSON response
# The URL and path
# Note: http.client requires you to separate the host and the path
host = 'www.example.com'
path = '/'
try:
    # 1. Create a connection object.
    #    - We use HTTPSConnection for secure (HTTPS) connections.
    #    - For HTTP, you would use HTTPConnection(host).
    connection = http.client.HTTPSConnection(host)
    # 2. Send the GET request.
    #    The method is 'GET', the path is '/', and we send some headers.
    #    'Host' is a required header.
    connection.request("GET", path, headers={"Host": host})
    # 3. Get the response from the server.
    response = connection.getresponse()
    # 4. Check the status code.
    print(f"Status Code: {response.status} {response.reason}")
    # 5. Read the response data.
    #    .read() returns bytes, so we decode it to a string.
    data = response.read().decode('utf-8')
    print(f"\nResponse Content (first 200 chars):")
    print(data[:200])
    # 6. Close the connection to release the resources.
    connection.close()
except Exception as e:
    print(f"An error occurred: {e}")

GET Request with Query Parameters in http.client

With http.client, you have to manually construct the URL with query parameters.

import http.client
import urllib.parse # Used for URL encoding
# The base URL and path
host = 'httpbin.org'
path = '/get'
# The parameters to add
params = {
    'name': 'Alice',
    'age': 30,
    'city': 'New York' # This value needs to be URL-safe
}
# 1. URL-encode the parameters
#    This turns 'city': 'New York' into 'city=New%20York'
encoded_params = urllib.parse.urlencode(params)
# 2. Construct the full path with the query string
#    This creates '/get?name=Alice&age=30&city=New+York'
full_path = f"{path}?{encoded_params}"
try:
    # 1. Create a connection
    connection = http.client.HTTPSConnection(host)
    # 2. Send the GET request with the manually constructed path
    connection.request("GET", full_path)
    # 3. Get the response
    response = connection.getresponse()
    print(f"Status Code: {response.status} {response.reason}")
    # 4. Read and parse the JSON response
    data = response.read().decode('utf-8')
    response_data = json.loads(data)
    print("\nResponse JSON (sent parameters):")
    print(json.dumps(response_data, indent=2))
    # 5. Close the connection
    connection.close()
except Exception as e:
    print(f"An error occurred: {e}")

Practical Comparison: requests vs. http.client

Feature requests (Recommended) http.client (Low-Level)
Ease of Use Excellent. Simple, intuitive API. Poor. Verbose, requires manual handling of many details.
URL Construction Automatic. Pass a dict to params. Manual. You must build the query string yourself.
Response Handling Automatic. .text for string, .json() for parsed JSON. Manual. You must read bytes and decode them. JSON parsing is separate.
Sessions Built-in requests.Session() for cookies and connection pooling. No built-in concept. You must manage connections and cookies yourself.
Error Handling response.raise_for_status() is simple. Also has specific exception classes. You must manually check response.status against codes (e.g., 200, 404).
Dependencies Requires an external library (pip install requests). Built-in to Python standard library.
When to Use 99% of all use cases. Web scraping, APIs, interacting with services. Legacy systems, embedded environments where you can't install packages, or learning about HTTP internals.

Summary

  • For any new project, use requests. It will save you time and make your code cleaner and more robust.
  • Use http.client only when you have a specific reason to, such as being in an environment where you cannot install external libraries or when you need fine-grained control over every aspect of the HTTP connection.
  • If you see code using httplib, it's almost certainly Python 2 code and should be updated to use http.client (for Python 3) or, better yet, requests.
分享:
扫描分享到社交APP
上一篇
下一篇