Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x CherryPy上传文件_Python 3.x_Upload_Cherrypy - Fatal编程技术网

Python 3.x CherryPy上传文件

Python 3.x CherryPy上传文件,python-3.x,upload,cherrypy,Python 3.x,Upload,Cherrypy,我想将一个文件从python3客户端发布到cherrypy。我正在使用请求库。 我的客户代码: import requests url = 'http://127.0.0.1:8080/upload' files = {'file.zip': open('file.zip', 'rb')} r = requests.post(url, files=files) 我的服务器代码: import os import tempfile import shutil import cherrypy

我想将一个文件从python3客户端发布到cherrypy。我正在使用请求库。 我的客户代码:

import requests

url = 'http://127.0.0.1:8080/upload'
files = {'file.zip': open('file.zip', 'rb')}

r = requests.post(url, files=files)
我的服务器代码:

import os
import tempfile
import shutil

import cherrypy


config = {
    'global' : {
        'server.socket_host' : '127.0.0.1',
        'server.socket_port' : 8080,
        'server.thread_pool' : 8,
        'server.max_request_body_size' : 0,
        'server.socket_timeout' : 60
  }
}


class App:
    @cherrypy.config(**{'response.timeout': 3600})
    @cherrypy.expose()
    def upload(self):
        '''Handle non-multipart upload'''

        destination = os.path.join('/home/uvv/upload')
        with open(destination, 'wb') as f:
            shutil.copyfileobj(cherrypy.request.body, f)

        return 'Okay'


if __name__ == '__main__':
        cherrypy.quickstart(App(), '/', config)
服务器返回一个错误:

127.0.0.1 - - [17/Aug/2016:11:38:49] "POST /upload HTTP/1.1" 400 2083 "" "python-requests/2.10.0"

从回复中获取信息是有用的。当您发送请求时,您将收到响应。从这个响应中,您可以获得有关HTTP代码的信息,其中200表示OK,400表示bad请求。这是您可以在cherrypy日志中看到的文本:
POST/upload HTTP/1.1“400
。要获得更多信息,请使用
print(r.text)

如果您将上面的代码与下面的代码一起使用,那么这就是将文件上载到cherrypy服务器的工作示例

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os
import cherrypy

config = {
    'global' : {
        'server.socket_host' : '127.0.0.1',
        'server.socket_port' : 9090,
        'server.thread_pool' : 8,
        'server.max_request_body_size' : 0,
        'server.socket_timeout' : 60
    }
}


class App:

    @cherrypy.expose
    def upload(self, ufile):
        upload_path = os.path.normpath('/path/to/project/data/')
        upload_file = os.path.join(upload_path, ufile.filename)
        size = 0
        with open(upload_file, 'wb') as out:
            while True:
                data = ufile.file.read(8192)
                if not data:
                    break
                out.write(data)
                size += len(data)
        out = '''
length: {}
filename: {}
mime-type: {}
''' .format(size, ufile.filename, ufile.content_type, data)
        return out


if __name__ == '__main__':
    cherrypy.quickstart(App(), '/', config)

将路径
/path/to/project/data/
替换为适合您的项目的路径。

这不是您发布的错误,而是一个日志条目。HTTP响应主体是什么?p.s.尝试在上载处理程序的开头添加日志记录,以查看它是否被调用。p.p.s.首先尝试上载小文件,并检查您是否拥有适当的权限要从cherrypy appPrermission:a+rwx写入
/home/uvv/upload
文件,请执行以下操作。该文件不会上载。那么响应主体是什么?最好提供参考:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os
import cherrypy

config = {
    'global' : {
        'server.socket_host' : '127.0.0.1',
        'server.socket_port' : 9090,
        'server.thread_pool' : 8,
        'server.max_request_body_size' : 0,
        'server.socket_timeout' : 60
    }
}


class App:

    @cherrypy.expose
    def upload(self, ufile):
        upload_path = os.path.normpath('/path/to/project/data/')
        upload_file = os.path.join(upload_path, ufile.filename)
        size = 0
        with open(upload_file, 'wb') as out:
            while True:
                data = ufile.file.read(8192)
                if not data:
                    break
                out.write(data)
                size += len(data)
        out = '''
length: {}
filename: {}
mime-type: {}
''' .format(size, ufile.filename, ufile.content_type, data)
        return out


if __name__ == '__main__':
    cherrypy.quickstart(App(), '/', config)