使用请求(Python)通过FieldStorage发送POST请求

时间:2015-04-14 15:02:39

标签: python http

基本上我正在做的是将文件提交到我的python服务器,然后我需要python服务器将同一文件转发到另一个API(第三方OCR)。我正在尝试使用请求库,但它似乎不喜欢它。它告诉我,目前,这个错误:

  

AttributeError:'set'对象没有属性'read'

所以这里是基本上运行它的代码。任何帮助或积分将不胜感激!

注意:self.request.post ['file']是fieldstorage数据类型。

import requests

files = {'file': {self.request.POST['fileName'], self.request.POST['file'].file, self.request.POST['type']}}
r = requests.post(url, files=files)

编辑:这是完整的追踪

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/usr/local/lib/python2.7/dist-packages/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/usr/local/lib/python2.7/dist-packages/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/usr/local/lib/python2.7/dist-packages/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/usr/local/lib/python2.7/dist-packages/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/usr/local/lib/python2.7/dist-packages/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "main.py", line 91, in post
    resp = self.save_ocr()
  File "main.py", line 143, in save_ocr
    r = requests.post(url, files=files)
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 88, in post
    return request('post', url, data=data, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 421, in request
    prep = self.prepare_request(req)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 359, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "/usr/lib/python2.7/dist-packages/requests/models.py", line 290, in prepare
    self.prepare_body(data, files)
  File "/usr/lib/python2.7/dist-packages/requests/models.py", line 427, in prepare_body
    (body, content_type) = self._encode_files(files, data)
  File "/usr/lib/python2.7/dist-packages/requests/models.py", line 140, in _encode_files
    rf = RequestField(name=k, data=fp.read(),
AttributeError: 'set' object has no attribute 'read'

0 个答案:

没有答案