如何使用请求库POST烧瓶中的python对象?

时间:2018-02-05 23:09:01

标签: python flask python-requests

我的烧瓶终点定义如下;

@app.route('/ATGPollingEngineExceptions',methods=['POST'])
def PollingEngineExceptions(PollingException): 
    response = jsonify(statusCode=httplib.OK)                               
    response.status_code = httplib.OK
     ..
    return response

它接受python对象类 PollingException

PollingException

class PollingException(object):

    def __init__(self,errorcode,errordes,stacktrace,timestamp):
        self.ErrorCode=errorcode
        self.ErrorDescription=errordes
        self.StackTrace=stacktrace
        self.DateTimeStamp=timestamp

在我的测试用例中,我尝试POST PollingException

def test_atg_polling_exception(self):
    status=200
    jsonEncoder = CustomJSONEncoder()      
    a= ATGPollingException('System.OutOfMemoryException','System.OutOfMemoryException','System.OutOfMemoryException','2009-02-15T00:00:00Z')
    headers = {'Content-type': 'application/json'}
    jsonmsg=jsonEncoder.default(a)
    response = requests.post("http://localhost:5000/ATGPollingEngineExceptions",data=a)
    print response.status_code
    assert status not in response.status_code

但我得到了断管错误,

Exception happened during processing of request from ('127.0.0.1', 62948)
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 321, in process_request
    self.finish_request(request, client_address)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 657, in __init__
    self.finish()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 716, in finish
    self.wfile.close()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 283, in close
    self.flush()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 307, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe

在客户端,我得到TypeError: 'PollingException' object is not iterable

这是使用请求库发布python对象的正确方法吗?

0 个答案:

没有答案