Python Requests.put()方法用于向指定 URL 发送 PUT 请求。此方法用于将给定 URL 中的资源更新或替换为提供的数据。
put() 方法类似于 requests.post(),但专门用于替换现有资源而不是创建新资源。它需要 URL、数据、标头、文件、身份验证、超时等参数来自定义请求。
此方法返回一个响应对象,其中包含有关请求状态、标头和内容的信息,这些信息可以根据应用程序的需要进一步处理。
语法
以下是 Python Requests.put() 方法的语法和参数 -
requests.get(url, params=None, **kwargs)
参数
以下是 Python Requests.put() 方法的参数 -
- url:url 是 PUT 请求发送到的资源的 URL。
- data(可选):这是要与 PUT 请求一起发送的数据。
- json(可选):要在正文中发送的 Json 数据。
- kwargs(可选):可以传递的可选参数,包括 headers、cookie、auth、timeout 等。
返回值
此方法返回 Response 对象。
示例 1
以下是使用 python Requests.put() 方法的基本 PUT 请求的基本示例 -
import requests
# Define the URL
url = 'https://httpbin.org/put'
# Define the data to be sent in the request body
data = {'key1': 'value1', 'key2': 'value2'}
# Send the PUT request with the data
response = requests.put(url, json=data)
# Print the response status code
print('Status Code:', response.status_code)
# Print the response content
print('Response Content:', response.text)
输出
Status Code: 200
Response Content: {
"args": {},
----------------
----------------
----------------
},
"origin": "110.226.149.205",
"url": "https://httpbin.org/put"
}
Response Content: {
"args": {},
----------------
----------------
----------------
},
"origin": "110.226.149.205",
"url": "https://httpbin.org/put"
}
示例 2
使用 Python 中的 requests 模块发送 PUT 请求时,我们可以处理请求过程中可能发生的错误。以下是处理 put 请求中错误的示例 -
import requests
try:
response = requests.put('https://api.example.com/nonexistent', timeout=5)
response.raise_for_status() # Raises an HTTPError if the status code is 4xx, 5xx
except requests.exceptions.HTTPError as err:
print(f'HTTP error occurred: {err}')
except requests.exceptions.RequestException as err:
print(f'Request error occurred: {err}')
输出
Request error occurred: HTTPSConnectionPool(host='api.example.com', port=443): Max retries exceeded with url: /nonexistent (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x000001BC03605890>: Failed to resolve 'api.example.com' ([Errno 11001] getaddrinfo failed)"))
示例 3
这是一个使用 python Requests.put() 方法的超时 PUT 请求示例 -
import requests
# Define the URL
url = 'https://httpbin.org/put'
# Define the data to be sent in the request body
data = {'key1': 'value1', 'key2': 'value2'}
# Set the timeout for the request (in seconds)
timeout = 5
try:
# Send the PUT request with the data and timeout
response = requests.put(url, json=data, timeout=timeout)
# Print the response status code
print('Status Code:', response.status_code)
# Print the response content
print('Response Content:', response.text)
except requests.Timeout:
# Handle timeout error
print('Timeout Error: Request timed out.')
except requests.RequestException as e:
# Handle other request exceptions
print('Request Exception:', e)
输出
Status Code: 200
Response Content: {
"args": {},
-------------------
-------------------
-------------------
},
"origin": "110.226.149.205",
"url": "https://httpbin.org/put"
}
Response Content: {
"args": {},
-------------------
-------------------
-------------------
},
"origin": "110.226.149.205",
"url": "https://httpbin.org/put"
}