Flask Deployment to AWS getting permission errno 13 - python-3.x

I have working app on the localhost but when I deployed to the AWS it shows several errors.It can receive my sent image but it cant save it. I tried change permissions with chmod,still its not working. This is first time i am deploying flask app,so I dont know much about i My code is as follow:
***modules***
ImageFile.LOAD_TRUNCATED_IMAGES = True
app = flask.Flask(__name__)
#app.route('/', methods = ['GET','POST'])
def hello():
return 'hey there!!!'
#app.route('/imageupload', methods = ['GET','POST'])
def handle_request():
try:
imagefile = flask.request.files['image']
filename = werkzeug.utils.secure_filename(imagefile.filename)
print("\nReceived image file name:" + imagefile.filename)
imagefile.save(filename)
print("hey")
for image in glob.glob('./frames/*.*'):
img_filter(imagefile,image)
json = flask.request.values['Id']
return {"status": "true","message": "Uploaded Successfully", "Id": json }
except:
return {"status" : "false"}
if __name__ == '__main__' :
app.run()
And these are the errors :
Received image file name:1.jpg
ERROR:image_flask:Exception on /imageupload [POST]
Traceback (most recent call last):
File "/home/ubuntu/pic-edit/env/lib/python3.6/site-packages/flask/app.py", line 2446, in ws$
response = self.full_dispatch_request()
File "/home/ubuntu/pic-edit/env/lib/python3.6/site-packages/flask/app.py", line 1951, in fu$
rv = self.handle_user_exception(e)
File "/home/ubuntu/pic-edit/env/lib/python3.6/site-packages/flask/app.py", line 1820, in ha$
reraise(exc_type, exc_value, tb)
File "/home/ubuntu/pic-edit/env/lib/python3.6/site-packages/flask/_compat.py", line 39, in $
raise value
File "/home/ubuntu/pic-edit/env/lib/python3.6/site-packages/flask/app.py", line 1949, in fu$
rv = self.dispatch_request()
File "/home/ubuntu/pic-edit/env/lib/python3.6/site-packages/flask/app.py", line 1935, in di$
return self.view_functions[rule.endpoint](**req.view_args)
File "/var/www/html/pic-edit/image_flask.py", line 26, in handle_request
imagefile.save(f'/home/ubuntu/pic-edit/{filename}')
File "/home/ubuntu/pic-edit/env/lib/python3.6/site-packages/werkzeug/datastructures.py", li$
dst = open(dst, "wb")
PermissionError: [Errno 13] Permission denied: '/home/ubuntu/pic-edit/1.jpg'

you don't have the right permission. Try to open first the folder with chmod -R 700 /home/ubuntu/pic-edit/
Please note open the permissions like this can be dangerous. Rather I would assign the permission to the "Flask" - Running user
Further for investigations under which user are you running the flask app? How do you start the app?
Also you might change the handle func to:
def handle_request():
try:
f = request.files['file']
f.save(secure_filename(f.filename))
json = flask.request.values['Id']
return {"status": "true","message": "Uploaded Successfully", "Id": json }
note you nead to import from flask import request

I got it where I went wrong, that is because of permissions to the folder. I already tried "chmod 755" to it but it did not worked. After that I tried
chmod a+rwx
And that work fluently.

Related

Retrieve data from Tuya sdk

I use that guide to retrieve data from my Fingerbot: https://www.home-assistant.io/integrations/tuya/
I also use that repo to extract the data:
https://github.com/redphx/tuya-local-key-extractor
When I use the extract.py file I get that error:
Exception in thread Thread-1:
Traceback (most recent call last):
openapi <tuya_iot.openapi.TuyaOpenAPI object at 0x7f52fff924c0>
File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
openmq <TuyaOpenMQ(Thread-1, started 139994399807232)>
self.run()
File "/home/xavier/.local/lib/python3.8/site-packages/tuya_iot/openmq.py", line 158, in run
self.__run_mqtt()
File "/home/xavier/.local/lib/python3.8/site-packages/tuya_iot/openmq.py", line 164, in __run_mqtt
mq_config = self._get_mqtt_config()
File "/home/xavier/.local/lib/python3.8/site-packages/tuya_iot/openmq.py", line 67, in _get_mqtt_config
"uid": self.api.token_info.uid,
AttributeError: 'NoneType' object has no attribute 'uid'
Traceback (most recent call last):
File "./extract.py", line 41, in <module>
device_manager.update_device_list_in_smart_home()
File "/home/xavier/.local/lib/python3.8/site-packages/tuya_iot/device.py", line 239, in update_device_list_in_smart_home
response = self.api.get(f"/v1.0/users/{self.api.token_info.uid}/devices")
AttributeError: 'NoneType' object has no attribute 'uid'
Here is the code I use:
import json
import logging
import os
from config import (
ENDPOINT,
APP,
EMAIL,
PASSWORD,
ACCESS_ID,
ACCESS_KEY,
)
from tuya_iot import (
TuyaOpenAPI,
AuthType,
TuyaOpenMQ,
TuyaDeviceManager,
)
openapi = TuyaOpenAPI(ENDPOINT, ACCESS_ID, ACCESS_KEY, AuthType.SMART_HOME)
openapi.connect(EMAIL, PASSWORD, country_code=84, schema=APP.value)
openmq = TuyaOpenMQ(openapi)
openmq.start()
print('openapi {}'.format(openapi))
print('openmq {}'.format(openmq))
device_manager = TuyaDeviceManager(openapi, openmq)
device_manager.update_device_list_in_smart_home()
devices = []
for tuya_device in device_manager.device_map.values():
device = {
'device_id': tuya_device.id,
'device_name': tuya_device.name,
'product_id': tuya_device.product_id,
'product_name': tuya_device.product_name,
'category': tuya_device.category,
'uuid': tuya_device.uuid,
'local_key': tuya_device.local_key,
}
try:
resp = openapi.get('/v1.0/iot-03/devices/factory-infos?device_ids={}'.format(tuya_device.id))
factory_info = resp['result'][0]
if 'mac' in factory_info:
mac = ':'.join(factory_info['mac'][i:i + 2] for i in range(0, 12, 2))
device['mac_address'] = mac
except Exception as e:
print(e)
devices.append(device)
print(json.dumps(devices, indent=2))
os._exit(0)
I am on Ubuntu 20.04 and I install the python sdk with tha t command:
pip3 install tuya-iot-py-sdk

Unable to render template with flask wsgi [duplicate]

This question already has answers here:
Flask raises TemplateNotFound error even though template file exists
(13 answers)
Closed 2 years ago.
I have deployed a flask application, using mod_wsgi with apache2
Now, checking if everything works as intended, it looks like my endpoints which render templates are provoking some 500 status code errors.
Here's an approximative tree of my project:
main_folder
requirements.txt
mainfile.wsgi
app_folder
controllers
views.py
models
repository
services
static
templates
terms
en
terms.html
fr
terms.html
uploads
__init__.py
config.cfg
webapp.py
I am calling an endpoint which is supposed to render the templates/terms/en/terms.html and I get the following in the logs:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.7/dist-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/var/www/main_folder/app_folder/controllers/views.py", line 33, in gts
return render_template('terms/' + lang + '/terms.html')
File "/usr/local/lib/python3.7/dist-packages/flask/templating.py", line 138, in render_template
ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "/usr/local/lib/python3.7/dist-packages/jinja2/environment.py", line 869, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "/usr/local/lib/python3.7/dist-packages/jinja2/environment.py", line 830, in get_template
return self._load_template(name, self.make_globals(globals))
File "/usr/local/lib/python3.7/dist-packages/jinja2/environment.py", line 804, in _load_template
template = self.loader.load(self, name, globals)
File "/usr/local/lib/python3.7/dist-packages/jinja2/loaders.py", line 113, in load
source, filename, uptodate = self.get_source(environment, name)
File "/usr/local/lib/python3.7/dist-packages/flask/templating.py", line 60, in get_source
return self._get_source_fast(environment, template)
File "/usr/local/lib/python3.7/dist-packages/flask/templating.py", line 89, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: terms/en/terms.html
Here's an excerp of the code where I try to render the template in app_folder/controllers/views.py:
from flask import render_template, request, send_file
from app_folder.controllers import Controllers as controllers
def gts(lang='fr'):
return render_template('terms/' + lang + '/terms.html')
controllers().register(
'/cgu/<string:lang>',
'cgu_lang',
gts
)
I want to specify that endpoints with JSON body results are functionning, uploading ressources are working as well, and getting an uploaded ressource is working.
Only endpoints which should render templates are not working, which is getting me worried as well for endpoints which should send emails using an email template
Is there any fix I should apply when serving the application with wsgi?
Edit: FYI, this is the __init__.py in the controllers folder
import pkgutil
METHODS = set([
'GET',
'POST',
'PUT',
'PATCH',
'DELETE',
'COPY',
'HEAD',
'OPTIONS',
'LINK',
'UNLINK',
'PURGE',
'LOCK',
'UNLOCK',
'PROPFIND',
'VIEW'
])
class Controllers:
class __OnlyOne:
# ======================================================
# the code goes here
# =======================================================
def __init__(self):
self.val = None
self.rules = []
def __str__(self):
return 'self' + self.val
def register(self, rule, view_name, view_func):
r = {}
r["rule"] = rule
r["view_name"] = view_name
r["view_func"] = view_func
self.rules.append(r)
def register_methods(self, rule, view_name, view_func, methods=METHODS):
r = {}
r["rule"] = rule
r["view_name"] = view_name
r["view_func"] = view_func
r["methods"] = methods
self.rules.append(r)
def grab(self, app):
for r in self.rules:
if "methods" not in r.keys():
app.add_url_rule(r["rule"], r["view_name"], r["view_func"])
else:
app.add_url_rule(r["rule"], r["view_name"],
r["view_func"], methods=r["methods"])
# =================================================================
# the code goes up there
# ==================================================================
instance = None
def __new__(cls): # __new__ always a classmethod
if not Controllers.instance:
Controllers.instance = Controllers.__OnlyOne()
return Controllers.instance
def __getattr__(self, name):
return getattr(self.instance, name)
def __setattr__(self, name):
return setattr(self.instance, name)
# import all the modules in folder
__all__ = []
for loader, module_name, is_pkg in pkgutil.walk_packages(__path__):
__all__.append(module_name)
_module = loader.find_module(module_name).load_module(module_name)
globals()[module_name] = _module
I symlinked my app_folder/templates into my main_folder and it's now working

How do I use a Custom Provider [keycloak] for OAuth2.0 in Flask-Appbuilder?

I am trying to implement keycloak as a OAuth2.0 provider. I have all the necessary OAUTH_PROVIDER information and I have declared the AUTH_TYPE, AUTH_USER_REGISTRATION, AUTH_USER_REGISTRATION_ROLE (See code below)
AUTH_TYPE = AUTH_OAUTH
AUTH_USER_REGISTRATION = True
AUTH_USER_REGISTRATION_ROLE = "Public"
OAUTH_PROVIDERS = [
{
‘name’: ‘provider_name’,
‘icon’: ‘fa-icon’,
’token_key’: ‘access_token’,
‘remote_app’: {
‘base_url’: ‘https://www.base_url.com‘,
‘request_token_params’: {
‘scope’: ‘email profile’
},
‘request_token_url’: None,
‘access_token_url’: ‘https://www.access_token_url.com’,
‘authorize_url': ‘https://www.authorize_url.com',
‘consumer_key’: ‘PROVIDER_OAUTH_KEY’,
‘consumer_secret’: ‘PROVIDER_OAUTH_SECRET’
}
}
]
Because this is a custom provider (apart from the usual google, facebook, ... providers), in my security manager I have overridden the get_oauth_user_info function that is declared in the flask-appbuilder manager.py
def get_oauth_user_info(self, provider, response=None):
logging.debug("Oauth2 provider: {0}.".format(provider))
me = self.appbuilder.sm.oauth_remotes[provider].get("user")
logging.error(me)
return {
"preferred_username": me.data.get("preferred_username",""),
"first_name": me.data.get("given_name", ""),
"last_name": me.data.get("family_name", ""),
"email": me.data.get("email", "")
I expect results the application to be redirected to the keycloak login page, however the following error is thrown on the post request:
DEBUG:flask_oauthlib:Request 'https://...' with 'POST' method
ERROR:flask_appbuilder.security.manager:User info does not have username or email {}
[2019-05-23 12:52:10,130] ERROR in app: Exception on /oauth-authorized/login [GET]
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python3.6/site-packages/flask_appbuilder/security/views.py", line 519, in oauth_authorized
resp = self.appbuilder.sm.oauth_remotes[provider].authorized_response()
If anyone has some pointers as to what I am doing wrong please could you assist me.
You should add annotation #appbuilder.sm.oauth_user_info_getter to the get_oauth_user_info func like in the docs https://flask-appbuilder.readthedocs.io/en/latest/security.html#authentication-oauth
Also change
me = self.appbuilder.sm.oauth_remotes[provider].get("user")
to
me = self.appbuilder.sm.oauth_remotes[provider].get("userinfo")
in case if base_url is like https://{keycloak-domain}/auth/realms/{realm}/protocol/openid-connect/

Scrapy error catching in scrapy/middleware.py file: TypeError: __init__() missing 1 required positional argument: 'uri'

I am catching this error while starting a crawl. I have searched for an answer in several forums, and looked at the code in scrapy/middleware.py (came standard with scrapy and I have not altered it) and cannot figure out why I am getting an error.
The scraper is using both an ImagesPipeline and S3FilesStore pipeline for storing a json file and downloaded images directly into different S3 folders. I am using Python 3.6.
Any help is appreciated. The error message and my scraper settings are below, please let me know if anything else would be useful.
Traceback (most recent call last):
File "/Users/user/anaconda/envs/python3/lib/python3.6/site-
packages/twisted/internet/defer.py", line 1386, in _inlineCallbacks
result = g.send(result)
File "/Users/user/anaconda/envs/python3/lib/python3.6/site-
packages/scrapy/crawler.py", line 77, in crawl
self.engine = self._create_engine()
File "/Users/user/anaconda/envs/python3/lib/python3.6/site-
packages/scrapy/crawler.py", line 102, in _create_engine
return ExecutionEngine(self, lambda _: self.stop())
File "/Users/user/anaconda/envs/python3/lib/python3.6/site-
packages/scrapy/core/engine.py", line 70, in __init__
self.scraper = Scraper(crawler)
File "/Users/user/anaconda/envs/python3/lib/python3.6/site-
packages/scrapy/core/scraper.py", line 71, in __init__
self.itemproc = itemproc_cls.from_crawler(crawler)
File "/Users/user/anaconda/envs/python3/lib/python3.6/site-
packages/scrapy/middleware.py", line 58, in from_crawler
return cls.from_settings(crawler.settings, crawler)
File "/Users/user/anaconda/envs/python3/lib/python3.6/site-
packages/scrapy/middleware.py", line 40, in from_settings
mw = mwcls()
TypeError: __init__() missing 1 required positional argument: 'uri'
ITEM_PIPELINES = {
'scrapy.pipelines.files.S3FilesStore': 1,
'scrapy.pipelines.images.ImagesPipeline': 1
}
AWS_ACCESS_KEY_ID = 'xxxxxx'
AWS_SECRET_ACCESS_KEY= 'xxxxxx'
IMAGES_STORE = 's3 path'
FEED_URI = 's3 path'
FEED_FORMAT = 'jsonlines'
FEED_EXPORT_FIELDS = None
FEED_STORE_EMPTY = False
FEED_STORAGES = {}
FEED_STORAGES_BASE = {
'': 'scrapy.extensions.feedexport.FileFeedStorage',
'file': 'scrapy.extensions.feedexport.FileFeedStorage',
'stdout': 'scrapy.extensions.feedexport.StdoutFeedStorage',
's3': 'scrapy.extensions.feedexport.S3FeedStorage',
'ftp': 'scrapy.extensions.feedexport.FTPFeedStorage',
}
FEED_EXPORTERS = {}
FEED_EXPORTERS_BASE = {
'json': 'scrapy.exporters.JsonItemExporter',
'jsonlines': 'scrapy.exporters.JsonLinesItemExporter',
'jl': None,
'csv': None,
'xml': None,
'marshal': None,
'pickle': None,
}

Google Calendar API FileNotFoundError:

I am trying to create events for Google Calendar but am getting a FileNotFoundError for 'client_secret.json'.
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
SCOPES = 'https://www.googleapis.com/auth/calendar'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store, flags) \
if flags else tools.run(flow, store)
CAL = build('calendar', 'v3', http=creds.authorize(Http()))
GMT_OFF = '-07:00' # PDT/MST/GMT-7
EVENT = {
'summary': 'Dinner with friends',
'start': {'dateTime': '2015-09-15T19:00:00%s' % GMT_OFF},
'end': {'dateTime': '2015-09-15T22:00:00%s' % GMT_OFF},
'attendees': [
{'email': 'friend1#example.com'},
{'email': 'friend2#example.com'},
],
}
e = CAL.events().insert(calendarId='primary',
sendNotifications=True, body=EVENT).execute()
print('''*** %r event added:
Start: %s
End: %s''' % (e['summary'].encode('utf-8'),
e['start']['dateTime'], e['end']['dateTime']))
Here is the error:
Warning (from warnings module):
File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\site-packages\oauth2client_helpers.py", line 255
warnings.warn(_MISSING_FILE_MESSAGE.format(filename))
UserWarning: Cannot access storage.json: No such file or directory
Traceback (most recent call last):
File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\site-packages\oauth2client\clientsecrets.py", line 121, in _loadfile
with open(filename, 'r') as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'client_secret.json'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/bakat/AppData/Local/Programs/Python/Python35-32/Diet Buddy/Google_Calendar.py", line 16, in
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\site-packages\oauth2client_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\site-packages\oauth2client\client.py", line 2125, in flow_from_clientsecrets
cache=cache)
File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\site-packages\oauth2client\clientsecrets.py", line 165, in loadfile
return _loadfile(filename)
File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\site-packages\oauth2client\clientsecrets.py", line 125, in _loadfile
exc.strerror, exc.errno)
oauth2client.clientsecrets.InvalidClientSecretsError: ('Error opening file', 'client_secret.json', 'No such file or directory', 2)
I found the problem - you must go to https://developers.google.com/gmail/api/quickstart/python and follow the instructions to download your client_secrets.json.

Resources