python怎么提取raw的通道_Cherrypy:如何从上传的文件中获取rawdata?

本文档探讨了在使用CherryPy 3.2.4版本时,如何从上传的文件中获取原始数据。作者展示了尝试获取上传文件长度、文件名、MIME类型及读取文件内容的代码,并提供了通过读取Content-Length头信息来保存上传文件到本地的解决方案。
摘要由CSDN通过智能技术生成

我在使用cherrypy3.2.4上传文件时遇到问题。(Python 2.7)

我无法获取上传文件的原始数据。我试图调试如何从响应中获取数据值,但没有成功。有人知道怎么解决这个问题吗?在

皮奥特

这是我使用的代码:def index(self):

return """

Upload a file

filename:

Download a file

This one

"""

index.exposed = True

def upload(self, myFile):

out = """

myFile length: %s

myFile filename: %s

myFile mime-type: %s

"""

cherrypy.log('-'*60)

cherrypy.log('0: ' + str(dir(myFile)))

cherrypy.log('-'*60)

cherrypy.log('2: ' + str(dir(cherrypy.request.headers)))

cherrypy.log('-'*60)

cherrypy.log('4: ' + str(dir(cherrypy.request.rfile)))

cherrypy.log('-'*60)

cherrypy.log('6: ' + str(cherrypy.request.rfile.readline))

cherrypy.log('-'*60)

cherrypy.log('8: ' + str(cherrypy.request.rfile.read()))

cherrypy.log('-'*60)

cherrypy.log('-'*60)

cherrypy.log('-'*60)

# Although this just counts the file length, it demonstrates

# how to read large files in chunks instead of all at once.

# CherryPy reads the uploaded file into a temporary file;

# myFile.file.read reads from that.

size = 0

while True:

data = myFile.file.read(8192)

if not data:

break

size += len(data)

#cherrypy.log('myFile: %s' % str(dir(myFile)))

#cherrypy.log('File: %s' % str(myFile.read_into_file))

#myFile.read_into_file('a.txt')

return out % (size, myFile.filename, myFile.content_type)

upload.exposed = True

这是输出:

^{pr2}$

编辑:

我试着找到了一个简单的解决方案:def upload(self, myFile):

lcHDRS = {}

for key, val in cherrypy.request.headers.iteritems():

lcHDRS[key.lower()] = val

incomingBytes = int(lcHDRS['content-length'])

content = myFile.file.read(incomingBytes)

file = open ('./upload/' + file.filename,"wb")

file.write(content)

file.close()

return 'File was uploaded...'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值