首页 > 解决方案 > 如何使用请求库发布到本地主机?

问题描述

我试图在 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)

有人可以解释为什么它不起作用吗?

提前致谢!

标签: pythonpost

解决方案


当发生与网络相关的问题(例如 DNS 故障或连接拒绝)时,请求会引发连接错误。查看您的代码和错误,我建议:

  1. 确保端口 (1880) 可用于新连接,

  2. 在 localhost 上禁用代理:

    import os
    os.environ['NO_PROXY'] = '127.0.0.1' 在发送发布请求之前,

  3. 使用'127.0.0.1'而不是'localhost'来发送 post 请求。


推荐阅读