python - 如何使用请求库发布到本地主机?
问题描述
我试图在 rpi 中使用 python 发布到以下 url (http://localhost:1880/notifData) 的帖子。为此,我使用瓶子没有问题:
from bottle import get, post, run, request
@post('/notifData')
def notifData():
print(request.body.getvalue())
run(host='localhost', port=1880, debug=True)
但是当我尝试使用请求库时,我得到了一个[Errno 111] Conection refused
错误:
import requests
r = requests.post('http://localhost:1880/notifData/')
print(r.text)
有人可以解释为什么它不起作用吗?
提前致谢!
解决方案
当发生与网络相关的问题(例如 DNS 故障或连接拒绝)时,请求会引发连接错误。查看您的代码和错误,我建议:
确保端口 (1880) 可用于新连接,
在 localhost 上禁用代理:
import os
os.environ['NO_PROXY'] = '127.0.0.1'
在发送发布请求之前,使用'127.0.0.1'而不是'localhost'来发送 post 请求。
推荐阅读
- mysql - 根据另一个表中的状态更新 MYSQL 表列
- php - 我想在我的 css 文件中添加一个带有 laravel URL 的背景图像。我可以通过仅包含路径来做到这一点?
- reactjs - 如何更改特定操作的反应时间范围选择器最小值或最大值
- javascript - Google Apps 脚本 - Gmail - 无法可靠地检测垃圾邮件中的特定字符串
- vb.net - VS2019,版本 16.9.4。无法加载文件或程序集 Microsoft.Win32.Primitives,可执行文件 (.exe) 或库 (.dll) 的格式无效
- python - 具有两种类型的单变量
- android - 在flutter中发布apk时如何解决错误?
- html - 在父活动上应用 CSS 样式但排除特定的子活动事件
- ajax - ASP.NET MVC Ajax 请求在大数据量期间因 net::ERR_HTTP2_PROTOCOL_ERROR 失败
- r - 用于计算R中重复序列的循环