I have this decorator in Python, which has the purpose to get a file named 'api.key' in the root directory:
def require_appkey(view_function):
@wraps(view_function)
# the new, post-decoration function. Note *args and **kwargs here.
def decorated_function(*args, **kwargs):
with open('api.key', 'r') as apikey:
key=apikey.read().replace('\n', '')
#if request.args.get('key') and request.args.get('key') == key:
if request.headers.get('token_') and request.headers.get('token_') == key:
return view_function(*args, **kwargs)
else:
return {'ERROR' : 'UNAUTHORIZED ACTION!'}
return decorated_function
When executing in localhost:5000, no problems are encountered with POSTMAN but when the files are put in the AWS Ubuntu server, it returns FileNotFoundError: [Errno 2] No such file or directory: 'api.key'. Here is the full log:
[Fri Jun 08 17:31:39.648898 2018] [wsgi:error] [pid 16373] ERROR:flask.app:Exception on /salestracking/AuthenticateUser [POST]
[Fri Jun 08 17:31:39.648925 2018] [wsgi:error] [pid 16373] Traceback (most recent call last):
[Fri Jun 08 17:31:39.648928 2018] [wsgi:error] [pid 16373] File "/home/ubuntu/salesappserver/lib/python3.5/site-packages/flask/app.py", l$
[Fri Jun 08 17:31:39.648931 2018] [wsgi:error] [pid 16373] rv = self.dispatch_request()
[Fri Jun 08 17:31:39.648934 2018] [wsgi:error] [pid 16373] File "/home/ubuntu/salesappserver/lib/python3.5/site-packages/flask/app.py", l$
[Fri Jun 08 17:31:39.648936 2018] [wsgi:error] [pid 16373] return self.view_functions[rule.endpoint](**req.view_args)
[Fri Jun 08 17:31:39.648939 2018] [wsgi:error] [pid 16373] File "/home/ubuntu/salesappserver/lib/python3.5/site-packages/flask_restful/__$
[Fri Jun 08 17:31:39.648941 2018] [wsgi:error] [pid 16373] resp = resource(*args, **kwargs)
[Fri Jun 08 17:31:39.648943 2018] [wsgi:error] [pid 16373] File "/home/ubuntu/salesappserver/lib/python3.5/site-packages/flask/views.py",$
[Fri Jun 08 17:31:39.648946 2018] [wsgi:error] [pid 16373] return self.dispatch_request(*args, **kwargs)
[Fri Jun 08 17:31:39.648948 2018] [wsgi:error] [pid 16373] File "/home/ubuntu/salesappserver/lib/python3.5/site-packages/flask_restful/__$
[Fri Jun 08 17:31:39.648951 2018] [wsgi:error] [pid 16373] resp = meth(*args, **kwargs)
[Fri Jun 08 17:31:39.648953 2018] [wsgi:error] [pid 16373] File "/var/www/html/salesappserver/api.py", line 27, in decorated_function
[Fri Jun 08 17:31:39.648956 2018] [wsgi:error] [pid 16373] with open('api.key', 'r') as apikey:
[Fri Jun 08 17:31:39.648959 2018] [wsgi:error] [pid 16373] FileNotFoundError: [Errno 2] No such file or directory: 'api.key'
I already check my directory and it seems that 'api.key' exists.
(salesappserver) ubuntu@ip-172-31-3-35:~/salesappserver$ ls
api.key api.py app.wsgi bin include index.html lib pip-selfcheck.json README.md share
(salesappserver) ubuntu@ip-172-31-3-35:~/salesappserver$
Please help me to fix this.