1

I need to fetch a file from an API and then save it in an Survey object. It seems to be working fine, i.e. I can fetch the file and save it where I need to, but a ConnectionAbortedError is raised and the file is uploaded twice. The one saved in the model being the last one.

  • Here is the views.py:
def send_survey(request):

    ## Using convertApi to merge PDFs
    convertapi.api_secret = settings.CONVERTAPI_SECRET
    result = convertapi.convert('merge', {'Files': [files_url_list]}) 

    ## Getting the file from the url
    r = requests.get(result.file.url)

    ## Saving the file to the Survey object
    if r.status_code == 200:
        contentFile = ContentFile(r.content)
        s = Survey()
        s.file.save('Last survey', contentFile, save=True)
        messages.success(request, "The literature survey saved successfully!")
    else:
        messages.error(request, r.status_code)
    return redirect('homepage')
  • And the models.py:
class Survey(models.Model):

    date = models.DateField('Date', default=date.today)
    file = models.FileField('Survey', upload_to='literature_survey/previous_surveys', default=None)
    sent = models.BooleanField('Sent?', default=False)

    def __str__(self):
        return f"Survey #{self.pk}"

And I get the following error:

----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 62615)
Traceback (most recent call last):
  File "C:\Python\Python38\lib\socketserver.py", line 650, in process_request_thread
    self.finish_request(request, client_address)
  File "C:\Python\Python38\lib\socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Python\Python38\lib\socketserver.py", line 720, in __init__
    self.handle()
  File "C:\Python\Python38\lib\site-packages\django\core\servers\basehttp.py", line 174, in handle
    self.handle_one_request()
  File "C:\Python\Python38\lib\site-packages\django\core\servers\basehttp.py", line 182, in handle_one_request
    self.raw_requestline = self.rfile.readline(65537)
  File "C:\Python\Python38\lib\socket.py", line 669, in readinto
    return self._sock.recv_into(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
----------------------------------------
  • Does this answer your question? [ConnectionAbortedError: \[WinError 10053\] An established connection was aborted by the software in your host machine](https://stackoverflow.com/questions/51562067/connectionabortederror-winerror-10053-an-established-connection-was-aborted-b) – DARK_C0D3R May 23 '20 at 17:35
  • @DARK_C0D3R I don't think it does since in that case he is using a webdriver and the problem seems to come from it. Here I'm just doing a GET request. Also, in my case, the file is uploaded once, then the error is raised and finally the file is uploaded a second time and save to the model with no error. – Benjamin Hennequart May 23 '20 at 17:44

0 Answers0