Home > Net >  'socket' object has no attribute 'sendfile' while sending a file in flask guni
'socket' object has no attribute 'sendfile' while sending a file in flask guni

Time:11-12

Using flask, I'm trying to send a file to the user on clicking a button in UI using send_from_directory function. It used to work fine. I wanted to change the repo and since changing it, I'm no more able to download the file. On looking at the supervisor log, I see this:

[9617] [ERROR] Error handling request
Traceback (most recent call last):
  File "path_to_file/venv/lib/python3.4/site-packages/gunicorn/workers/sync.py", line 182, in handle_request
    resp.write_file(respiter)
  File "path_to_file/venv/lib/python3.4/site-packages/gunicorn/http/wsgi.py", line 385, in write_file
    if not self.sendfile(respiter):
  File "path_to_file/venv/lib/python3.4/site-packages/gunicorn/http/wsgi.py", line 375, in sendfile
    self.sock.sendfile(respiter.filelike, count=nbytes)
AttributeError: 'socket' object has no attribute 'sendfile'

In the same repo, this works fine locally. But when trying in remote server using the gunicorn supervisor nginx setup, I get the above error message. I do get 200 Ok response in the application log file. Spent a lot of time trying to fix but without success.

Also, the notable difference between the working app between the previous repo and the non-working current repo is the python version. Previous: python2.7, Current: python3.4

CodePudding user response:

For me, usually when a script works locally but not when hosted its one (or more) of these possibilities:

  • Location / path to the files is different
  • older version of python
  • older version of the library

CodePudding user response:

Pointing the virtual env to Python 3.6 and upgrading all the relevant libraries including Flask resolved the issue.

  • Related