【问题标题】:CherryPy 3.6 - reading Multipart Post http requestCherryPy 3.6 - 阅读 Multipart Post http 请求
【发布时间】:2015-04-24 05:03:17
【问题描述】:

我编写了一个 java 客户端,它通过一个多部分的 post http 请求向运行cherrypy 3.6 的服务器发送一串元信息和一个字节数组。

我需要提取这两个值,并在服务器端的 python3 中对此进行了编码,以了解如何操作结果,因为我在互联网上找不到任何相关文档来解释如何阅读此 html 部分

def controller(self, meta, data):

        print("meta", meta)
        print("data", type(data))

输出:

my meta information
<class 'cherrypy._cpreqbody.Part'>

注意: 数据部分包含原始二进制数据。

如何将 http 部分内容读入缓冲区或输出到磁盘文件?

感谢您的帮助。

【问题讨论】:

    标签: http post request cherrypy multipart


    【解决方案1】:

    感谢您的回答。 我已经阅读了这个文档,但不幸的是方法 read-into_file 和 make_file, read ...它对我不起作用。例如,当尝试读取从我的 java 客户端发送的 zip 文件时:

    假设data是Http post参数

    ma​​ke_file()

    fp = data.make_file()
    print("fp type", type(fp)) # _io.BufferedRandom
    zipFile =  fp.read()
    

    输出:

    AttributeError: 'bytes' object has no attribute 'seek'
    

    第 651 行,在 read_lines_to_boundary 中引发 EOFError("Illegal end of multipart body.")EOFError: Illegal end of multipart body.

    read_into_file()

        file = data.read_into_file()
        print("file type", type(file))
        zipFile =  io.BytesIO(file.read())
        # zipFile =  file.read() # => raises same error
    

    输出:

    line 651, in read_lines_to_boundary raise EOFError("Illegal end of multipart body.")EOFError: Illegal end of multipart body.
    

    我不明白会发生什么......

    实际上“数据”不是像对象这样的文件,而是cherrypy._cpreqbody.Part 1。它包含一个“文件”文件和一个 _io.BufferedRandom 类属性。

    它的 read() 方法以二进制形式(字节)返回整个正文内容。

    所以最简单的解决方案是:

    class BinReceiver(object):
    
        def index(self, data):
    
            zipFile =  io.BytesIO(data.file.read())
            path = "/tmp/data.zip"
            fp = open(path)
            fp.write(zipFile, 'wb')
            fp.close()
    
            print("saved data into", path, "size", len(zipFile))
    
        index.exposed = True
    

    这很好用......

    仅供参考:我正在运行 python3.2

    【讨论】:

      【解决方案2】:

      似乎data 是一个类似文件的对象,您可以在其上调用.read。另外CherryPy提供了一个方法read_into_file

      通过在您的 REPL 中输入 help(cherrypy._cpreqbody.Part) 查看完整文档。

      class Part(Entity)
       |  A MIME part entity, part of a multipart entity.
       |  
       |  Method resolution order:
       |      Part
       |      Entity
       |      __builtin__.object
       |  
       |  Methods defined here:
       |  
       |  __init__(self, fp, headers, boundary)
       |  
       |  default_proc(self)
       |      Called if a more-specific processor is not found for the
       |      ``Content-Type``.
       |  
       |  read_into_file(self, fp_out=None)
       |      Read the request body into fp_out (or make_file() if None).
       |      
       |      Return fp_out.
       |  
       |  read_lines_to_boundary(self, fp_out=None)
       |      Read bytes from self.fp and return or write them to a file.
       |      
       |      If the 'fp_out' argument is None (the default), all bytes read are
       |      returned in a single byte string.
       |      
       |      If the 'fp_out' argument is not None, it must be a file-like
       |      object that supports the 'write' method; all bytes read will be
       |      written to the fp, and that fp is returned.
       |  
       |  ----------------------------------------------------------------------
       |  Class methods defined here:
       |  
       |  from_fp(cls, fp, boundary) from __builtin__.type
       |  
       |  read_headers(cls, fp) from __builtin__.type
       |  
       |  ----------------------------------------------------------------------
       |  Data and other attributes defined here:
       |  
       |  attempt_charsets = ['us-ascii', 'utf-8']
       |  
       |  boundary = None
       |  
       |  default_content_type = 'text/plain'
       |  
       |  maxrambytes = 1000
       |  
       |  ----------------------------------------------------------------------
       |  Methods inherited from Entity:
       |  
       |  __iter__(self)
       |  
       |  __next__(self)
       |  
       |  fullvalue(self)
       |      Return this entity as a string, whether stored in a file or not.
       |  
       |  make_file(self)
       |      Return a file-like object into which the request body will be read.
       |      
       |      By default, this will return a TemporaryFile. Override as needed.
       |      See also :attr:`cherrypy._cpreqbody.Part.maxrambytes`.
       |  
       |  next(self)
       |  
       |  process(self)
       |      Execute the best-match processor for the given media type.
       |  
       |  read(self, size=None, fp_out=None)
       |  
       |  readline(self, size=None)
       |  
       |  readlines(self, sizehint=None)
       |  
       |  ----------------------------------------------------------------------
       |  Data descriptors inherited from Entity:
       |  
       |  __dict__
       |      dictionary for instance variables (if defined)
       |  
       |  __weakref__
       |      list of weak references to the object (if defined)
       |  
       |  type
       |      A deprecated alias for :attr:`content_type<cherrypy._cpreqbody.Entity.content_type>`.
       |  
       |  ----------------------------------------------------------------------
       |  Data and other attributes inherited from Entity:
       |  
       |  charset = None
       |  
       |  content_type = None
       |  
       |  filename = None
       |  
       |  fp = None
       |  
       |  headers = None
       |  
       |  length = None
       |  
       |  name = None
       |  
       |  params = None
       |  
       |  part_class = <class 'cherrypy._cpreqbody.Part'>
       |      A MIME part entity, part of a multipart entity.
       |  
       |  parts = None
       |  
       |  processors = {'application/x-www-form-urlencoded': <function process_u...
      

      【讨论】:

        猜你喜欢
        • 2012-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多