我正在尝试用烧瓶写一些代码,让另一台服务器可以向它发布一些数据,然后在烧瓶内,当我从外面接到电话时,我想再发一个帖子给另一台服务器。
以下是我的代码
from flask import Flask, jsonify, request
import json
import requests
app = Flask(__name__)
@app.route("/sendData.json", methods=['POST'])
def receiveImageData():
imageData = request.json
# save data to son file
with open(request.json['image_filename'].split('.')[0] + '.json', 'w+') as f:
json.dump(imageData, f, indent=4)
# post some data to this url
r = requests.post('http://example.com/example.json', son = {'image_filename':'test.jpg', 'image_url': "http://images.come"})
# return json data back for /sendData.json
return jsonify({"status": "success"})
问题是最后一行{'status':'success'}不会返回sendData.json。
我该如何解决它,或者应该有另一种方法来实现这一目标?
以下是烧瓶的错误日志
127.0.0.1 - - [03/Aug/2017 14:38:37] "POST /sendData.json HTTP/1.1" 500 -
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/bloomer/Documents/python/api_server/api.py", line 21, in receiveImageData
r = requests.post('http://example.com/example.json', son = {'image_filename':'test.jpg', 'image_url': "http://images.come"})
File "/usr/local/lib/python2.7/site-packages/requests/api.py", line 112, in post
return request('post', url, data=data, json=json, **kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/adapters.py", line 490, in send
raise ConnectionError(err, request=request)
ConnectionError: ('Connection aborted.', BadStatusLine("''",))
答案 0 :(得分:0)
在return Response(), 200
结束时使用receiveImageData()
进行试用。
修改强>
我研究了一下。实际上它应该工作。尝试类似:
return jsonify({"success": True}), 202
您收到的错误消息是什么?它是否与发布数据和状态的服务器/ API相同:成功?
答案 1 :(得分:0)
而不是session-config
使用:
jsonify({"status": "success"})