发送任何大小和格式的文件的最快方法是什么?

时间:2018-09-26 17:34:41

标签: python django python-3.x hadoop django-rest-framework

我使用Angular 6前端,Django 1.11后端和Hadoop 3.1构建Web应用程序。我需要以最快的方式发送任何大小和格式的文件。我在Django中的方法如下所示。各种格式的小文件似乎一切正常。但是,当我尝试上传较大的文件时,通常会在底部显示错误。您能推荐在python中以最快的方式发送所有格式和大小的文件的方法吗?如果您需要任何其他信息,请告诉我。预先感谢。

def post(self, request):

    key = request.META.get('HTTP_AUTHORIZATION').split()[1]

    user_id = Token.objects.get(key=key).user_id

    user_name = User.objects.get(id=user_id).username

    upload_file(request.FILES['file'], user_name)

    return Response(status=status.HTTP_201_CREATED)

    def upload_file(file, user_name):

        response = requests.put(url + ':9870/webhdfs/v1/user/' + str(user_name) + '/' + str(file) + '?op=CREATE&user.name=myuser&createflag=&createparent=true&overwrite=false' , data=file, headers={'content-type':'application/octet-stream'})

        return response

错误:

django_1  | Internal Server Error: /cloud/
django_1  | Traceback (most recent call last):
django_1  |   File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
django_1  |     chunked=chunked)
django_1  |   File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 354, in _make_request
django_1  |     conn.request(method, url, **httplib_request_kw)
django_1  |   File "/usr/local/lib/python3.6/http/client.py", line 1239, in request
django_1  |     self._send_request(method, url, body, headers, encode_chunked)
django_1  |   File "/usr/local/lib/python3.6/http/client.py", line 1285, in _send_request
django_1  |     self.endheaders(body, encode_chunked=encode_chunked)
django_1  |   File "/usr/local/lib/python3.6/http/client.py", line 1234, in endheaders
django_1  |     self._send_output(message_body, encode_chunked=encode_chunked)
django_1  |   File "/usr/local/lib/python3.6/http/client.py", line 1065, in _send_output
django_1  |     self.send(chunk)
django_1  |   File "/usr/local/lib/python3.6/http/client.py", line 986, in send
django_1  |     self.sock.sendall(data)
django_1  | BrokenPipeError: [Errno 32] Broken pipe
django_1  | 
django_1  | During handling of the above exception, another exception occurred:
django_1  | 
django_1  | Traceback (most recent call last):
django_1  |   File "/usr/local/lib/python3.6/site-packages/requests/adapters.py", line 445, in send
django_1  |     timeout=timeout
django_1  |   File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 638, in urlopen
django_1  |     _stacktrace=sys.exc_info()[2])
django_1  |   File "/usr/local/lib/python3.6/site-packages/urllib3/util/retry.py", line 367, in increment
django_1  |     raise six.reraise(type(error), error, _stacktrace)
django_1  |   File "/usr/local/lib/python3.6/site-packages/urllib3/packages/six.py", line 685, in reraise
django_1  |     raise value.with_traceback(tb)
django_1  |   File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
django_1  |     chunked=chunked)
django_1  |   File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 354, in _make_request
django_1  |     conn.request(method, url, **httplib_request_kw)
django_1  |   File "/usr/local/lib/python3.6/http/client.py", line 1239, in request
django_1  |     self._send_request(method, url, body, headers, encode_chunked)
django_1  |   File "/usr/local/lib/python3.6/http/client.py", line 1285, in _send_request
django_1  |     self.endheaders(body, encode_chunked=encode_chunked)
django_1  |   File "/usr/local/lib/python3.6/http/client.py", line 1234, in endheaders
django_1  |     self._send_output(message_body, encode_chunked=encode_chunked)
django_1  |   File "/usr/local/lib/python3.6/http/client.py", line 1065, in _send_output
django_1  |     self.send(chunk)
django_1  |   File "/usr/local/lib/python3.6/http/client.py", line 986, in send
django_1  |     self.sock.sendall(data)
django_1  | urllib3.exceptions.ProtocolError: ('Connection aborted.', BrokenPipeError(32, 'Broken pipe'))
django_1  | 
django_1  | During handling of the above exception, another exception occurred:
django_1  | 
django_1  | Traceback (most recent call last):
django_1  |   File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
django_1  |     response = get_response(request)
django_1  |   File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
django_1  |     response = self.process_exception_by_middleware(e, request)
django_1  |   File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
django_1  |     response = wrapped_callback(request, *callback_args, **callback_kwargs)
django_1  |   File "/usr/local/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
django_1  |     return view_func(*args, **kwargs)
django_1  |   File "/usr/local/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
django_1  |     return self.dispatch(request, *args, **kwargs)
django_1  |   File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 483, in dispatch
django_1  |     response = self.handle_exception(exc)
django_1  |   File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 443, in handle_exception
django_1  |     self.raise_uncaught_exception(exc)
django_1  |   File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 480, in dispatch
django_1  |     response = handler(request, *args, **kwargs)
django_1  |   File "/code/backend/views.py", line 37, in post
django_1  |     response = upload_file(request.FILES['file'], user_name)
django_1  |   File "/code/backend/hdfs.py", line 8, in upload_file
django_1  |     response = requests.put(url + ':9870/webhdfs/v1/user/' + str(user_name) + '/' + str(file) + '?op=CREATE&user.name=myuser&createflag=&createparent=true&overwrite=false' , data=file, headers={'content-type':'application/octet-stream'})
django_1  |   File "/usr/local/lib/python3.6/site-packages/requests/api.py", line 126, in put
django_1  |     return request('put', url, data=data, **kwargs)
django_1  |   File "/usr/local/lib/python3.6/site-packages/requests/api.py", line 58, in request
django_1  |     return session.request(method=method, url=url, **kwargs)
django_1  |   File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 512, in request
django_1  |     resp = self.send(prep, **send_kwargs)
django_1  |   File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 622, in send
django_1  |     r = adapter.send(request, **kwargs)
django_1  |   File "/usr/local/lib/python3.6/site-packages/requests/adapters.py", line 495, in send
django_1  |     raise ConnectionError(err, request=request)
django_1  | requests.exceptions.ConnectionError: ('Connection aborted.', BrokenPipeError(32, 'Broken pipe'))
django_1  | [20/Sep/2018 18:00:59] "POST /cloud/ HTTP/1.1" 500 17134
django_1  | [20/Sep/2018 18:00:59] "GET /cloud/ HTTP/1.1" 200 591

0 个答案:

没有答案