我用python 如下代码请求本地程序,被据否 url = "http://localhost:1235" requestBody = json.dumps({"temperature": temperature, "bank": bank}) headers = { "Content-Type": "application/json", } response = requests.post(url, data=requestBody, headers=headers) 但是我用 rest client 可以成功 POST http://localhost:1235 content-type: application/json { "temperature": 34.7, "bank": 88.0 } 能告诉我为什么吗
时间: 2023-07-21 15:10:30 浏览: 67
根据你提供的代码,其中可能存在一个问题导致请求被拒绝。在使用 Python 进行请求时,使用 `data` 参数传递请求体数据,但实际上你应该使用 `json` 参数来传递 JSON 格式的数据。修改代码如下:
```python
import json
import requests
url = "http://localhost:1235"
requestBody = {"temperature": temperature, "bank": bank}
headers = {
"Content-Type": "application/json",
}
response = requests.post(url, json=requestBody, headers=headers)
```
通过将数据传递给 `json` 参数,`requests` 库将自动将其转换为 JSON 格式并设置正确的 Content-Type 头。这样应该可以解决请求被拒绝的问题。
另外,请确保你的本地程序正在运行,并监听在 `http://localhost:1235` 这个地址上。
相关问题
写个Python调用http://itsm.intest.cn/webservices/rest.php?version=1.3
好的,下面是一个简单的Python调用http://itsm.intest.cn/webservices/rest.php?version=1.3的示例代码:
```python
import requests
import json
# 定义API地址和请求参数
url = "http://itsm.intest.cn/webservices/rest.php?version=1.3"
payload = {
"operation": "core/get",
"class": "User",
"key": "SELECT User WHERE name='admin'"
}
# 发送HTTP请求并获取API响应
response = requests.post(url, data=json.dumps(payload))
# 解析API响应并输出结果
if response.status_code == 200:
result = response.json()
print(result)
else:
print("API调用失败,错误码:", response.status_code)
```
解释一下代码:
首先,使用requests库发送POST请求,其中url参数指定API地址,data参数指定API请求参数,使用json.dumps()方法将字典类型的payload参数转换为JSON格式字符串。
然后,判断API响应的状态码是否为200,如果是,则使用response.json()方法将API响应的JSON格式字符串转换为Python字典类型,然后输出结果;否则,输出API调用失败的错误码。
需要注意的是,这只是一个简单的示例,具体的API调用方法需要根据实际情况进行修改。同时,在使用API时需要遵守API提供方的使用规则,不要滥用API资源。
pycharm用代码实现与http://121.36.102.158:8000/jupyter/双向通信
您可以使用 Python 中的 requests 库与 WebSocket 进行通信来实现与 http://121.36.102.158:8000/jupyter/ 的双向通信。下面是一个简单示例代码:
```python
import websocket
import
import json
# 获取 token
response = requests.get('http://121.36.102.158:8000/jupyter/api/token')
token = response.json()['token']
# 创建 WebSocket 连接
ws = websocket.WebSocket()
ws.connect(f'ws://121.36.102.158:8000/jupyter/api/kernels?token={token}')
# 发送消息
message = {
'header': {
'msg_type': 'execute_request',
'msg_id': '1',
'username': '',
'session': '',
'version': '5.2'
},
'metadata': {},
'content': {
'code': 'print("Hello, world!")',
'silent': False,
'store_history': True,
'user_expressions': {},
'allow_stdin': False
},
'buffers': {}
}
ws.send(json.dumps(message))
# 接收消息
result = ws.recv()
print(result)
# 关闭连接
ws.close()
```
在上面的示例代码中,我们首先使用 requests 库获取了连接的 token,然后使用 websocket 库创建了一个 WebSocket 连接,并发送了一条执行代码的消息。接下来,我们接收了服务器返回的消息,并关闭了连接。
需要注意的是,上面的示例代码仅仅是一个简单的示例,实际实现中还需要处理连接失败、异常等情况。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)