AttributeError:模块“requests”没有属性“post”。它是否已弃用并引入了新功能?

2024-04-19 16:33:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试向使用flask构建的本地服务器发送请求。 请求使用python的requests模块发送。在

我不知道这个requests.post函数是否已经被弃用,是否引入了新的函数,或者我的代码真的有什么问题。我完全按照this article所说的做了。
我的代码是:

import requests

host_url = "http://127.0.0.1:5000"

# get the data
data_for_prediction = [int(input()) for _ in range(10)]
r = requests.post(url=host_url,json={data_for_prediction})
print(r.json())

上面代码的错误是:

^{pr2}$

我的服务器代码是:

flask_server_app = Flask(__name__)

# let's make the server now
@flask_server_app.route("/api/predict", methods=["GET", "POST"])
# my prediction function goes here
def predict():
    # Get the data from the POST request & reads the received json
    json_data = request.get_json(force=True)

    # making prediction
    # Make prediction using model loaded from disk as per the data.
    prediction = ml_model.predict([[json_data]])

    # return json version of the prediction
    return jsonify(prediction[0])

# run the app now
if __name__ == '__main__':
    flask_server_app.run(port=5000, debug=True)

我尝试过查看文档,在线查看了许多文章,还重新编写了整个代码。但没有帮助。在

那么,这个requests.post函数是不推荐使用的,并且引入了一个新的函数,或者我的代码有什么问题吗。在


Tags: the函数代码jsonapphosturlflask
1条回答
网友
1楼 · 发布于 2024-04-19 16:33:19

似乎您正在一个名为requests.py的文件中编写代码,因此当您尝试导入请求模块时,它确实将您自己的文件作为模块导入。尝试重命名文件。。。在

相关问题 更多 >