skip to Main Content

My site should scrape some data from another, everything worked on my computer. Once I put my app on a server and this error occurs. I changed permission to 777 for whole path folders and didn’t work. I’m using python3, apache 2.4 on ubuntu 18.10.

PermissionError at /scrape/ [Errno 13] Permission denied: FILENAME

Error log:

[Tue Sep 24 20:54:41.981186 2019] [wsgi:error] [pid 29114:tid 139877711529728] [remote 89.78.216.206:53391]   File "/home/matms/django_project/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
[Tue Sep 24 20:54:41.981190 2019] [wsgi:error] [pid 29114:tid 139877711529728] [remote 89.78.216.206:53391]     response = get_response(request)
[Tue Sep 24 20:54:41.981192 2019] [wsgi:error] [pid 29114:tid 139877711529728] [remote 89.78.216.206:53391]   File "/home/matms/django_project/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
[Tue Sep 24 20:54:41.981195 2019] [wsgi:error] [pid 29114:tid 139877711529728] [remote 89.78.216.206:53391]     response = self.process_exception_by_middleware(e, request)
[Tue Sep 24 20:54:41.981198 2019] [wsgi:error] [pid 29114:tid 139877711529728] [remote 89.78.216.206:53391]   File "/home/matms/django_project/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
[Tue Sep 24 20:54:41.981201 2019] [wsgi:error] [pid 29114:tid 139877711529728] [remote 89.78.216.206:53391]     response = wrapped_callback(request, *callback_args, **callback_kwargs)
[Tue Sep 24 20:54:41.981204 2019] [wsgi:error] [pid 29114:tid 139877711529728] [remote 89.78.216.206:53391]   File "/home/matms/django_project/news/views.py", line 96, in scrape
[Tue Sep 24 20:54:41.981207 2019] [wsgi:error] [pid 29114:tid 139877711529728] [remote 89.78.216.206:53391]     with open(local_filename, 'wb') as f:
[Tue Sep 24 20:54:41.981211 2019] [wsgi:error] [pid 29114:tid 139877711529728] [remote 89.78.216.206:53391] PermissionError: [Errno 13] Permission denied: '4MsktkpTURBXy9kOWRiYWQ4Yzk2ZDkyYjk2YjNiYmRhZjNhNDdiMWQ2NC5qcGeTlQMARc0EAM0CP5MFzQEUzJuVB9kyL3B1bHNjbXMvTURBXy83MWUxOGYwMDNhYWE1ODk3NTIwMmFmNTk0OGZmNmZjMS5wbmcAwgA.jpg'
[Tue Sep 24 20:54:41.981217 2019] [wsgi:error] [pid 29114:tid 139877711529728] [remote 89.78.216.206:53391]

Line causing error with:

  media_root = '/home/matms/django_project/media_root'
        if not image_source_solved.startswith(("data:image", "javascript")):
            #exists = os.path.isfile(media_root+image_source_solved)
            exists = 1
            if exists == 2:
                pass
            else:
                local_filename = image_source_solved.split('/')[-1].split("?")[0]+".jpg"
                r = session.get(image_source_solved, stream=True, verify=False)
                with open(local_filename, 'wb') as f:
                    for chunk in r.iter_content(chunk_size=1024):
                        f.write(chunk)

                current_image_absolute_path = os.path.abspath(local_filename)
                shutil.move(current_image_absolute_path, media_root)

2

Answers


  1. Chosen as BEST ANSWER
    with open(local_filename, 'wb') as f:
    

    This change solved the problem.

    from django.conf import settings                    
    with open(os.path.join(settings.MEDIA_ROOT, local_filename), 'wb') as f:
    

  2. It will throw error until it get the permission for the full path.
    Here is your directory structure string.

    '/home/matms/django_project/media_root'
    

    Even if you have changed the permission for media_root folder but not of its parent directories i.e django_project, matms or home, it will throw error.

    Vice versa also works:
    If the application has permission to modify /home/matms/django_project/ but not media_root folder, it will again throw error.

    There are chances that you have changed only the permission of media_root. You have to change permissions of its parent directories too.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search