gpt4 book ai didi

python - 如何使用请求模块将数据发布到 flask 应用程序?

转载 作者:行者123 更新时间:2023-11-28 19:20:49 24 4
gpt4 key购买 nike

我正在尝试使用 flask 和 requests 模块开发一个小型应用程序。我试图将一些数据发布到 flask 网络应用程序。但我坚持这个错误。

flask .py

@app.route('/add/', methods=['POST'])
def add_paths():
paths = request.form['paths']
tags = 'others'
for path in paths:
g.db.execute('insert into entries (path, tags) values(?, ?)',
[path, tags])
g.db.commit()
message = 'New paths are posted'
return jsonify(result = message)

发布数据的命令行文件

import json
import glob2
import requests

list = glob2.glob('/home/sheeshmohsin/*/**')
post_data = {'paths' : list }
headers = {'content-type': 'application/json'}
post_response = requests.post(url='http://localhost:9696/add/', data=json.dumps(post_data), headers=headers)
print post_response
print post_response.text

我得到的错误是:-

File "commandline.py", line 8, in <module>
post_response = requests.post(url='http://127.0.0.1:9696/add/', data=json.dumps(post_data), headers=headers)
File "/usr/lib/python2.7/site-packages/requests/api.py", line 88, in post
return request('post', url, data=data, **kwargs)
File "/usr/lib/python2.7/site-packages/requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 335, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 438, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python2.7/site-packages/requests/adapters.py", line 327, in send
raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=9696): Max retries exceeded with url: /add/ (Caused by <class 'socket.error'>: [Errno 111] Connection refused)

最佳答案

连接被拒绝的错误消息表明它可能是您的防火墙或环回:

  • 检查防火墙是否阻止端口 9696。
  • 我的环回 (127.0.0.1) 导致了类似的问题,因此将 localhost 替换为实际 IP 地址,这样 Python 就不会使用环回接口(interface) (requests.post(url ='http://localhost:9696/add/',...). 你可能需要对 Flask 做同样的事情 (app..run(host='192.168...', port =9696)).

关于python - 如何使用请求模块将数据发布到 flask 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25614089/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com