我一直在尝试向使用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
函数是不推荐使用的,并且引入了一个新的函数,或者我的代码有什么问题吗。在
似乎您正在一个名为
requests.py
的文件中编写代码,因此当您尝试导入请求模块时,它确实将您自己的文件作为模块导入。尝试重命名文件。。。在相关问题 更多 >
编程相关推荐