如何使用Python调用指定Curl请求获取数据?
I want to use this cURL request:
curl 'https://cloud.estimote.com/v2/analytics/visits?from=1448928000&to=1451606399&granularity=daily' -X GET -u YOUR_SDK_APP_ID:YOUR_SDK_APP_TOKEN
The response format looks like this:
{ "data": [ { "date": "2016-02-22", "count": 183 }, { "date": "2016-02-23", "count": 162 } ] }
How can I call this cURL request using Python?
Using the requests library (Recommended)
The requests library makes HTTP requests in Python extremely straightforward—this is the go-to method for most developers. Here's how to replicate your cURL call:
First, install the library if you haven't already:
pip install requestsThen use this code snippet:
import requests # Replace these with your actual credentials SDK_APP_ID = "YOUR_SDK_APP_ID" SDK_APP_TOKEN = "YOUR_SDK_APP_TOKEN" # The API endpoint and query parameters url = "https://cloud.estimote.com/v2/analytics/visits" params = { "from": 1448928000, "to": 1451606399, "granularity": "daily" } # Send the GET request with basic authentication (matches the -u flag in cURL) response = requests.get(url, params=params, auth=(SDK_APP_ID, SDK_APP_TOKEN)) # Handle the response if response.status_code == 200: # Parse the JSON response into a Python dictionary data = response.json() print("Fetched visit data:") for entry in data["data"]: print(f"Date: {entry['date']}, Visit count: {entry['count']}") else: print(f"Request failed with status code {response.status_code}: {response.text}")
This code mirrors exactly what your cURL command does: it passes the query parameters cleanly, handles basic auth automatically, and makes parsing the JSON response trivial.
Using Python's built-in urllib library (No external dependencies)
If you can't install external libraries, you can use Python's standard urllib module. It's a bit more verbose, but works out of the box:
import urllib.request import json from base64 import b64encode # Replace with your actual credentials SDK_APP_ID = "YOUR_SDK_APP_ID" SDK_APP_TOKEN = "YOUR_SDK_APP_TOKEN" # Build the full URL with query parameters url = "https://cloud.estimote.com/v2/analytics/visits?from=1448928000&to=1451606399&granularity=daily" # Manually encode credentials for basic authentication (what cURL's -u does under the hood) credentials = f"{SDK_APP_ID}:{SDK_APP_TOKEN}".encode() encoded_auth = b64encode(credentials).decode("utf-8") headers = {"Authorization": f"Basic {encoded_auth}"} # Create and send the request request = urllib.request.Request(url, headers=headers, method="GET") try: with urllib.request.urlopen(request) as response: # Read and parse the JSON response response_data = json.loads(response.read().decode()) print("Fetched visit data:") for entry in response_data["data"]: print(f"Date: {entry['date']}, Visit count: {entry['count']}") except urllib.error.HTTPError as e: print(f"Request failed with status code {e.code}: {e.read().decode()}")
This approach handles the authentication header manually, so you don't need any third-party packages.
内容的提问来源于stack exchange,提问作者wawaloo_17

