Issue with Flask-Security under larger load - flask

I've got an issue with my Flask application. I have an order page that contains order products. When this page is opened for each product a request is sent to the server to get the product details. When the order contains 10-50 products no problems occur. But recently there came orders with 200-300 products. When such an order is opened I see following exceptions:
Feb 03 17:03:17 uwsgi[636157]: Traceback (most recent call last):
Feb 03 17:03:17 uwsgi[636157]: File "/usr/virtualfish/order/lib/python3.9/site-packages/flask/app.py", line 2077, in wsgi_app
Feb 03 17:03:17 uwsgi[636157]: response = self.full_dispatch_request()
Feb 03 17:03:17 uwsgi[636157]: File "/usr/virtualfish/order/lib/python3.9/site-packages/flask/app.py", line 1525, in full_dispatch_request
Feb 03 17:03:17 uwsgi[636157]: rv = self.handle_user_exception(e)
Feb 03 17:03:17 uwsgi[636157]: File "/usr/virtualfish/order/lib/python3.9/site-packages/flask/app.py", line 1521, in full_dispatch_request
Feb 03 17:03:17 uwsgi[636157]: rv = self.preprocess_request()
Feb 03 17:03:17 uwsgi[636157]: File "/usr/virtualfish/order/lib/python3.9/site-packages/flask/app.py", line 1861, in preprocess_request
Feb 03 17:03:17 uwsgi[636157]: rv = self.ensure_sync(before_func)()
Feb 03 17:03:17 uwsgi[636157]: File "/usr/virtualfish/order/lib/python3.9/site-packages/flask_principal.py", line 479, in _on_before_request
Feb 03 17:03:17 uwsgi[636157]: self.set_identity(identity)
Feb 03 17:03:17 uwsgi[636157]: File "/usr/virtualfish/order/lib/python3.9/site-packages/flask_principal.py", line 418, in set_identity
Feb 03 17:03:17 uwsgi[636157]: self._set_thread_identity(identity)
Feb 03 17:03:17 uwsgi[636157]: File "/usr/virtualfish/order/lib/python3.9/site-packages/flask_principal.py", line 462, in _set_thread_identity
Feb 03 17:03:17 uwsgi[636157]: identity_loaded.send(current_app._get_current_object(),
Feb 03 17:03:17 uwsgi[636157]: File "/usr/virtualfish/order/lib/python3.9/site-packages/blinker/base.py", line 266, in send
Feb 03 17:03:17 uwsgi[636157]: return [(receiver, receiver(sender, **kwargs))
Feb 03 17:03:17 uwsgi[636157]: File "/usr/virtualfish/order/lib/python3.9/site-packages/blinker/base.py", line 266, in <listcomp>
Feb 03 17:03:17 uwsgi[636157]: return [(receiver, receiver(sender, **kwargs))
Feb 03 17:03:17 uwsgi[636157]: File "/usr/virtualfish/order/lib/python3.9/site-packages/flask_security/core.py", line 254, in _on_identity_loaded
Feb 03 17:03:17 uwsgi[636157]: for role in getattr(current_user, 'roles', []):
Feb 03 17:03:17 uwsgi[636157]: File "/usr/virtualfish/order/lib/python3.9/site-packages/sqlalchemy/orm/attributes.py", line 287, in __get__
Feb 03 17:03:17 uwsgi[636157]: return self.impl.get(instance_state(instance), dict_)
Feb 03 17:03:17 uwsgi[636157]: File "/usr/virtualfish/order/lib/python3.9/site-packages/sqlalchemy/orm/attributes.py", line 723, in get
Feb 03 17:03:17 uwsgi[636157]: value = self.callable_(state, passive)
Feb 03 17:03:17 uwsgi[636157]: File "/usr/virtualfish/order/lib/python3.9/site-packages/sqlalchemy/orm/strategies.py", line 759, in _load_for_state
Feb 03 17:03:17 uwsgi[636157]: return self._emit_lazyload(
Feb 03 17:03:17 uwsgi[636157]: File "<string>", line 1, in <lambda>
Feb 03 17:03:17 uwsgi[636157]: File "/usr/virtualfish/order/lib/python3.9/site-packages/sqlalchemy/orm/strategies.py", line 900, in _emit_lazyload
Feb 03 17:03:17 uwsgi[636157]: q(session)
Feb 03 17:03:17 uwsgi[636157]: File "/usr/virtualfish/order/lib/python3.9/site-packages/sqlalchemy/ext/baked.py", line 544, in all
Feb 03 17:03:17 uwsgi[636157]: return list(self)
Feb 03 17:03:17 uwsgi[636157]: File "/usr/virtualfish/order/lib/python3.9/site-packages/sqlalchemy/orm/loading.py", line 100, in instances
Feb 03 17:03:17 uwsgi[636157]: cursor.close()
Feb 03 17:03:17 uwsgi[636157]: File "/usr/virtualfish/order/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
Feb 03 17:03:17 uwsgi[636157]: compat.raise_(
Feb 03 17:03:17 uwsgi[636157]: File "/usr/virtualfish/order/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
Feb 03 17:03:17 uwsgi[636157]: raise exception
Feb 03 17:03:17 uwsgi[636157]: File "/usr/virtualfish/order/lib/python3.9/site-packages/sqlalchemy/orm/loading.py", line 80, in instances
Feb 03 17:03:17 uwsgi[636157]: rows = [proc(row) for row in fetch]
Feb 03 17:03:17 uwsgi[636157]: File "/usr/virtualfish/order/lib/python3.9/site-packages/sqlalchemy/orm/loading.py", line 80, in <listcomp>
Feb 03 17:03:17 uwsgi[636157]: rows = [proc(row) for row in fetch]
Feb 03 17:03:17 uwsgi[636157]: File "/usr/virtualfish/order/lib/python3.9/site-packages/sqlalchemy/orm/loading.py", line 524, in _instance
Feb 03 17:03:17 uwsgi[636157]: tuple([row[column] for column in pk_cols]),
Feb 03 17:03:17 uwsgi[636157]: File "/usr/virtualfish/order/lib/python3.9/site-packages/sqlalchemy/orm/loading.py", line 524, in <listcomp>
Feb 03 17:03:17 uwsgi[636157]: tuple([row[column] for column in pk_cols]),
Feb 03 17:03:17 uwsgi[636157]: File "/usr/virtualfish/order/lib/python3.9/site-packages/sqlalchemy/engine/result.py", line 681, in _key_fallback
Feb 03 17:03:17 uwsgi[636157]: util.raise_(
Feb 03 17:03:17 uwsgi[636157]: File "/usr/virtualfish/order/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
Feb 03 17:03:17 uwsgi[636157]: raise exception
Feb 03 17:03:17 uwsgi[636157]: sqlalchemy.exc.NoSuchColumnError: "Could not locate column in row for column 'roles.id'"
I see the exception occurs in flask-security, not in my code.
When I tried to slow down requests for product details on the client side (entered delay in JavaScript), number of exceptions is decreased. So it seems to be caused by simultaneous requests number.
Otherwise other requests work fine. So there is nothing wrong in database structure.
What can be the cause?

Related

django.core.exceptions.ImproperlyConfigured: SQLite 3.9.0 or later is required (found 3.7.17) Error in Apache Logs

I'm getting below error in Apache error logs:
django.core.exceptions.ImproperlyConfigured: SQLite 3.9.0 or later is required (found 3.7.17)
I verified the sqlite3 version both in virtual environment and non virtual environment (standard). I can see the latest sqlite3 in both Python.
$ python3.7
Python 3.7.12 (default, Nov 8 2021, 09:02:58)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sqlite3 import dbapi2 as Database
>>> Database.sqlite_version_info
(3, 36, 0)
Error log from Apache web server:
[Mon Nov 08 15:02:33.698244 2021] [wsgi:error] [pid 1459] [remote 192.168.0.105:62615] backend = load_backend(db['ENGINE'])
[Mon Nov 08 15:02:33.698249 2021] [wsgi:error] [pid 1459] [remote 192.168.0.105:62615] File "/home/rafiq/myprojectenv/lib/python3.7/site-packages/django/db/utils.py", line 111, in load_backend
[Mon Nov 08 15:02:33.698252 2021] [wsgi:error] [pid 1459] [remote 192.168.0.105:62615] return import_module('%s.base' % backend_name)
[Mon Nov 08 15:02:33.698257 2021] [wsgi:error] [pid 1459] [remote 192.168.0.105:62615] File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
[Mon Nov 08 15:02:33.698260 2021] [wsgi:error] [pid 1459] [remote 192.168.0.105:62615] return _bootstrap._gcd_import(name[level:], package, level)
[Mon Nov 08 15:02:33.698265 2021] [wsgi:error] [pid 1459] [remote 192.168.0.105:62615] File "/home/rafiq/myprojectenv/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 73, in <module>
[Mon Nov 08 15:02:33.698267 2021] [wsgi:error] [pid 1459] [remote 192.168.0.105:62615] check_sqlite_version()
[Mon Nov 08 15:02:33.698273 2021] [wsgi:error] [pid 1459] [remote 192.168.0.105:62615] File "/home/rafiq/myprojectenv/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 69, in check_sqlite_version
[Mon Nov 08 15:02:33.698276 2021] [wsgi:error] [pid 1459] [remote 192.168.0.105:62615] 'SQLite 3.9.0 or later is required (found %s).' % Database.sqlite_version
[Mon Nov 08 15:02:33.698294 2021] [wsgi:error] [pid 1459] [remote 192.168.0.105:62615] django.core.exceptions.ImproperlyConfigured: SQLite 3.9.0 or later is required (found 3.7.17).
I see the latest version Python but why Apache reports 3.7.17?
I resolved the problem with the below steps.
mv /usr/bin/sqlite3 /usr/bin/sqlite3_3.7.9
ln -s /usr/local/bin/sqlite3 /usr/bin/sqlite3
The document which I followed doesn't contain the above-mentioned two commands and that helped to overcome the actual issue.
try this:
# Intall sqlite3 version 3.39.4
- wget https://www.sqlite.org/2022/sqlite-autoconf-3390400.tar.gz
- tar xvfz sqlite-autoconf-3390400.tar.gz
- cd sqlite-autoconf-3390400
- ./configure
- make
- make install
- PATH=$PATH:/usr/local/lib
- sqlite3 --version

Ubuntu / Django / Logging / PermissionError: [Errno 13] Permission denied

Ubuntu 18.04.1
using the Bitnami Django Stack: bitnami-djangostack-2.1.5-1
I get this error when starting apache service:
[16:21:20.028270 2019] [wsgi:error] Traceback (most recent call last):
[16:21:20.028373 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/python/lib/python3.7/logging/config.py", line 562, in configure
[16:21:20.028395 2019] [wsgi:error] handler = self.configure_handler(handlers[name])
[16:21:20.028421 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/python/lib/python3.7/logging/config.py", line 735, in configure_handler
[16:21:20.028438 2019] [wsgi:error] result = factory(**kwargs)
[16:21:20.028463 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/python/lib/python3.7/logging/handlers.py", line 147, in __init__
[16:21:20.028479 2019] [wsgi:error] BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
[16:21:20.028504 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/python/lib/python3.7/logging/handlers.py", line 54, in __init__
[16:21:20.028520 2019] [wsgi:error] logging.FileHandler.__init__(self, filename, mode, encoding, delay)
[16:21:20.028545 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/python/lib/python3.7/logging/__init__.py", line 1092, in __init__
[16:21:20.028562 2019] [wsgi:error] StreamHandler.__init__(self, self._open())
[16:21:20.028601 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/python/lib/python3.7/logging/__init__.py", line 1121, in _open
[16:21:20.028618 2019] [wsgi:error] return open(self.baseFilename, self.mode, encoding=self.encoding)
[16:21:20.028666 2019] [wsgi:error] PermissionError: [Errno 13] Permission denied: '/opt/djangostack-2.1.5-1/apps/django/django_projects/myproject/static/logs/ca.myproject.app.cleaner.log'
[16:21:20.028701 2019] [wsgi:error]
[16:21:20.028716 2019] [wsgi:error] The above exception was the direct cause of the following exception:
[16:21:20.028729 2019] [wsgi:error]
[16:21:20.028753 2019] [wsgi:error] Traceback (most recent call last):
[16:21:20.028832 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/apps/django/django_projects/myproject/myproject/wsgi.py", line 19, in <module>
[16:21:20.028851 2019] [wsgi:error] application = get_wsgi_application()
[16:21:20.028886 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/apps/django/lib/python3.7/site-packages/Django-2.1.5-py3.7.egg/django/core/wsgi.py", line 12, in get_wsgi_application
[16:21:20.028903 2019] [wsgi:error] django.setup(set_prefix=False)
[16:21:20.028928 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/apps/django/lib/python3.7/site-packages/Django-2.1.5-py3.7.egg/django/__init__.py", line 19, in setup
[16:21:20.028944 2019] [wsgi:error] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[16:21:20.028968 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/apps/django/lib/python3.7/site-packages/Django-2.1.5-py3.7.egg/django/utils/log.py", line 76, in configure_logging
[16:21:20.028984 2019] [wsgi:error] logging_config_func(logging_settings)
[16:21:20.029008 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/python/lib/python3.7/logging/config.py", line 799, in dictConfig
[16:21:20.029023 2019] [wsgi:error] dictConfigClass(config).configure()
[16:21:20.029046 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/python/lib/python3.7/logging/config.py", line 570, in configure
[16:21:20.029062 2019] [wsgi:error] '%r' % name) from e
[16:21:20.029102 2019] [wsgi:error] ValueError: Unable to configure handler 'cleaner_file'
How do I fix it?
Django is unable to create the log file in the directory /opt/djangostack-2.1.5-1/apps/django/django_projects/myproject/static/logs/
In my project settings.py the file path is specified as an absolute path, so Django should be able to write a log file to the folder.
Short fix:
chmod -R 777 $(pwd)/static/logs that will fix the issue, but after a reboot the error is back :(
Thanks for any help
Once the log hit the max size new file will be created with root permissions, see below:
-rw-rw-r-- 1 daemon daemon 0 Jun 5 13:54 my_project.debug.log
-rw-rw-r-- 1 daemon daemon 15319 Jun 12 17:52 my_project.app.log
**-rw-r--r-- 1 root root 159042 Jun 12 17:52 my_project.ws.log** --> HERE
-rw-rw-r-- 1 daemon daemon 15700891 Jun 12 17:52 my_project.ws.log.1
-rw-rw-r-- 1 daemon daemon 15711651 Jun 12 16:53 my_project.ws.log.2

Django with ModWsgi and Apache not a valid application

This ended up being a OSx MySQL Improperly Configured Reason: unsafe use of relative path issue but I have left original problem incase reader has same breadcrumb trail.
So this is an odd problem - I'm trying to deploy Django to Apache, ModWSGI on a Mac running OSx but am running into the following error message:
Target WSGI script '/Library/WebServer/local_biems/biems/wsgi.py' does not contain WSGI application
Now:
local_biems runs ok with python manage.py runserver with no silenced issues. So I believe my project is error free and ready for deployment.
a test application local_blog runs ok with runserver AND with my virtual host configuration - so I'm assuming from this that Apache and MODWSGI are playing happily together.
local_biems and local_blog are alongside each other in the /Library/WebServer/ and have identical permissions.
If I update the file paths in the virtual host, restart apache and do a hard reload of the site I get the error above.
WSGI files are identical.
What can I try? I've tried the try/catch statement around get_wsgi_application()
try:
application = get_wsgi_application()
print 'WSGI without exception'
except Exception:
print 'handling WSGI exception'
# Error loading applications
if 'mod_wsgi' in sys.modules:
traceback.print_exc()
os.kill(os.getpid(), signal.SIGINT)
time.sleep(2.5)
but same issue. If I remove this and stick with the standard...
Fri Dec 08 08:23:15.188440 2017] [wsgi:error] [pid 16734] [client 127.0.0.1:55348] Traceback (most recent call last):, referer: http://biems.local/
[Fri Dec 08 08:23:15.188471 2017] [wsgi:error] [pid 16734] [client 127.0.0.1:55348] File "/Library/WebServer/local_biems/biems/wsgi.py", line 16, in <module>, referer: http://biems.local/
[Fri Dec 08 08:23:15.188521 2017] [wsgi:error] [pid 16734] [client 127.0.0.1:55348] application = get_wsgi_application(), referer: http://biems.local/
[Fri Dec 08 08:23:15.188542 2017] [wsgi:error] [pid 16734] [client 127.0.0.1:55348] File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application, referer: http://biems.local/
[Fri Dec 08 08:23:15.188599 2017] [wsgi:error] [pid 16734] [client 127.0.0.1:55348] django.setup(set_prefix=False), referer: http://biems.local/
[Fri Dec 08 08:23:15.188615 2017] [wsgi:error] [pid 16734] [client 127.0.0.1:55348] File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/__init__.py", line 27, in setup, referer: http://biems.local/
[Fri Dec 08 08:23:15.188637 2017] [wsgi:error] [pid 16734] [client 127.0.0.1:55348] apps.populate(settings.INSTALLED_APPS), referer: http://biems.local/
[Fri Dec 08 08:23:15.188650 2017] [wsgi:error] [pid 16734] [client 127.0.0.1:55348] File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/apps/registry.py", line 78, in populate, referer: http://biems.local/
[Fri Dec 08 08:23:15.188682 2017] [wsgi:error] [pid 16734] [client 127.0.0.1:55348] raise RuntimeError("populate() isn't reentrant"), referer: http://biems.local/
[Fri Dec 08 08:23:15.188703 2017] [wsgi:error] [pid 16734] [client 127.0.0.1:55348] RuntimeError: populate() isn't reentrant, referer: http://biems.local/
Although I have just commented out all (but 1) of my apps. Strange thing is that this same site is working on a remove Ubuntu VPS no problem!
Ok, so I just copied the database settings from local_biems to the local_blog and I get MYSQL errors.
daviesc#orion:local_biems [0]$ tail -f /var/log/apache2/local_blog.error.log
[Fri Dec 08 08:42:57.704722 2017] [wsgi:error] [pid 16937] [client 127.0.0.1:55911] File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/utils.py", line 115, in load_backend, referer: http://biems.local/polls/
[Fri Dec 08 08:42:57.704740 2017] [wsgi:error] [pid 16937] [client 127.0.0.1:55911] return import_module('%s.base' % backend_name), referer: http://biems.local/polls/
[Fri Dec 08 08:42:57.704750 2017] [wsgi:error] [pid 16937] [client 127.0.0.1:55911] File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module, referer: http://biems.local/polls/
[Fri Dec 08 08:42:57.704766 2017] [wsgi:error] [pid 16937] [client 127.0.0.1:55911] __import__(name), referer: http://biems.local/polls/
[Fri Dec 08 08:42:57.704776 2017] [wsgi:error] [pid 16937] [client 127.0.0.1:55911] File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 30, in <module>, referer: http://biems.local/polls/
[Fri Dec 08 08:42:57.704913 2017] [wsgi:error] [pid 16937] [client 127.0.0.1:55911] 'Did you install mysqlclient or MySQL-python?' % e, referer: http://biems.local/polls/
[Fri Dec 08 08:42:57.704934 2017] [wsgi:error] [pid 16937] [client 127.0.0.1:55911] ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib, referer: http://biems.local/polls/
[Fri Dec 08 08:42:57.704941 2017] [wsgi:error] [pid 16937] [client 127.0.0.1:55911] Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/_mysql.so, referer: http://biems.local/polls/
[Fri Dec 08 08:42:57.704946 2017] [wsgi:error] [pid 16937] [client 127.0.0.1:55911] Reason: unsafe use of relative rpath libmysqlclient.18.dylib in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/_mysql.so with restricted binary., referer: http://biems.local/polls/
[Fri Dec 08 08:42:57.704951 2017] [wsgi:error] [pid 16937] [client 127.0.0.1:55911] Did you install mysqlclient or MySQL-python?, referer: http://biems.local/polls/
OK. I traced this to -
MySQL Improperly Configured Reason: unsafe use of relative path
Although needed to update the last arg to reflect the new path.
sudo install_name_tool -change libmysqlclient.18.dylib /
/usr/local/mysql/lib/libmysqlclient.18.dylib /
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/_mysql.so
It is probably a OSx specific issue - This was on 10.12.6. I'm not sure I would have found it easily because it the SQL error only seems to appear once and then the compiled files report a different error. I found using
find . -type f -name '*.pyc' -exec rm {} +
to remove all the compiled files helped.
Also have to set Debug to True not to get the less informative 500 Error.

Telegram-bot (telepot api): Is it possible to send an image directly from URL without saving it

Im writing a telegram bot using the python telepot api. I'm now stuck at the point where I want to send a picture which directly comes from an URL without storing it locally. Telepot provides the following instruction to send a photo:
>>> f = open('zzzzzzzz.jpg', 'rb') # some file on local disk
>>> response = bot.sendPhoto(chat_id, f)
Now im using
f = urllib2.urlopen('http://i.imgur.com/B1fzGoh.jpg')
bot.sendPhoto(chat_id, f)
The problem here is that urllib2.urlopen('url') provide me file-like object like:
<addinfourl at 140379102313792 whose fp = <socket._fileobject object at 0x7fac8e86d750>>
and not like open('myFile.jpg', 'rb') a file object like:
<open file 'app-root/runtime/repo/myImage.jpg', mode 'rb' at 0x7fac8f322540>
If I send the file-like object in sendPhoto() I get the following error:
Traceback (most recent call last):
[Wed Feb 10 06:21:09 2016] [error] File "/var/lib/openshift/56b8e2787628e1484a00013e/python/virtenv/lib/python2.7/site-packages/telepot/__init__.py", line 340, in handle
[Wed Feb 10 06:21:09 2016] [error] callback(update['message'])
[Wed Feb 10 06:21:09 2016] [error] File "/var/lib/openshift/56b8e2787628e1484a00013e/app-root/runtime/repo/moviequiz_main.py", line 35, in handle
[Wed Feb 10 06:21:09 2016] [error] response = bot.sendPhoto(chat_id, gif)
[Wed Feb 10 06:21:09 2016] [error] File "/var/lib/openshift/56b8e2787628e1484a00013e/python/virtenv/lib/python2.7/site-packages/telepot/__init__.py", line 230, in sendPhoto
[Wed Feb 10 06:21:09 2016] [error] return self._sendFile(photo, 'photo', p)
[Wed Feb 10 06:21:09 2016] [error] File "/var/lib/openshift/56b8e2787628e1484a00013e/python/virtenv/lib/python2.7/site-packages/telepot/__init__.py", line 226, in _sendFile
[Wed Feb 10 06:21:09 2016] [error] return self._parse(r)
[Wed Feb 10 06:21:09 2016] [error] File "/var/lib/openshift/56b8e2787628e1484a00013e/python/virtenv/lib/python2.7/site-packages/telepot/__init__.py", line 172, in _parse
[Wed Feb 10 06:21:09 2016] [error] raise BadHTTPResponse(response.status_code, response.text)
[Wed Feb 10 06:21:09 2016] [error] BadHTTPResponse: (414, u'<html>\\r\\n<head><title>414 Request-URI Too Large</title></head>\\r\\n<body bgcolor="white">\\r\\n<center><h1>414 Request-URI Too Large</h1></center>\\r\\n<hr><center>nginx/1.9.1</center>\\r\\n</body>\\r\\n</html>\\r\\n')
There is a solution for a different telegram-bot project provided here where they send the urllib2.urlopen('url').read() back to telegram but in my case this generates the same error as without .read() .
How could I get the file from the url as file object (best would be without saving it locally)?
Or how do I get the "file object" out of the "file-like object" provided by urlopen()?
Thanks for any help :)
In current Bot Api 2.3.1, You can simply send url of file to the server:
bot.sendPhoto(chat_id, "http://i.imgur.com/B1fzGoh.jpg");
That's it.
You don't even need to download it, Telegram would upload it by itself.
Yes.
You can make it async (or not):
async with aiohttp.get("http://i.imgur.com/B1fzGoh.jpg") as r:
result = r.read()
await self.sendPhoto(chat_id, ('image.jpg', result))

SQLite error attempt to write to a read-only database

I've been struggling to deploy my django project with Apache and mod_wsgi. I've had many problems that I managed to handle, but this one just seems not to be solvable.
I get the following error in my apache log when I enter the address setakshop.ir:8080 :
[Wed May 27 05:54:24 2015] [error] Internal Server Error: /en-gb/
[Wed May 27 05:54:24 2015] [error] Traceback (most recent call last):
[Wed May 27 05:54:24 2015] [error] File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 87, in get_response
[Wed May 27 05:54:24 2015] [error] response = middleware_method(request)
[Wed May 27 05:54:24 2015] [error] File "/usr/local/lib/python2.7/dist-packages/oscar/apps/basket/middleware.py", line 26, in process_request
[Wed May 27 05:54:24 2015] [error] strategy = selector.strategy(request=request, user=request.user)
[Wed May 27 05:54:24 2015] [error] File "/usr/local/lib/python2.7/dist-packages/oscar/apps/partner/strategy.py", line 39, in strategy
[Wed May 27 05:54:24 2015] [error] return Default(request)
[Wed May 27 05:54:24 2015] [error] File "/usr/local/lib/python2.7/dist-packages/oscar/apps/partner/strategy.py", line 57, in __init__
[Wed May 27 05:54:24 2015] [error] if request and request.user.is_authenticated():
[Wed May 27 05:54:24 2015] [error] File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 224, in inner
[Wed May 27 05:54:24 2015] [error] self._setup()
[Wed May 27 05:54:24 2015] [error] File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 357, in _setup
[Wed May 27 05:54:24 2015] [error] self._wrapped = self._setupfunc()
[Wed May 27 05:54:24 2015] [error] File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/middleware.py", line 22, in <lambda>
[Wed May 27 05:54:24 2015] [error] request.user = SimpleLazyObject(lambda: get_user(request))
[Wed May 27 05:54:24 2015] [error] File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/middleware.py", line 10, in get_user
[Wed May 27 05:54:24 2015] [error] request._cached_user = auth.get_user(request)
[Wed May 27 05:54:24 2015] [error] File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/__init__.py", line 152, in get_user
[Wed May 27 05:54:24 2015] [error] user_id = request.session[SESSION_KEY]
[Wed May 27 05:54:24 2015] [error] File "/usr/local/lib/python2.7/dist-packages/django/contrib/sessions/backends/base.py", line 49, in __getitem__
[Wed May 27 05:54:24 2015] [error] return self._session[key]
[Wed May 27 05:54:24 2015] [error] File "/usr/local/lib/python2.7/dist-packages/django/contrib/sessions/backends/base.py", line 175, in _get_session
[Wed May 27 05:54:24 2015] [error] self._session_cache = self.load()
[Wed May 27 05:54:24 2015] [error] File "/usr/local/lib/python2.7/dist-packages/django/contrib/sessions/backends/db.py", line 29, in load
[Wed May 27 05:54:24 2015] [error] self.create()
[Wed May 27 05:54:24 2015] [error] File "/usr/local/lib/python2.7/dist-packages/django/contrib/sessions/backends/db.py", line 41, in create
[Wed May 27 05:54:24 2015] [error] self.save(must_create=True)
[Wed May 27 05:54:24 2015] [error] File "/usr/local/lib/python2.7/dist-packages/django/contrib/sessions/backends/db.py", line 64, in save
[Wed May 27 05:54:24 2015] [error] obj.save(force_insert=must_create, using=using)
[Wed May 27 05:54:24 2015] [error] File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 589, in save
[Wed May 27 05:54:24 2015] [error] force_update=force_update, update_fields=update_fields)
[Wed May 27 05:54:24 2015] [error] File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 617, in save_base
[Wed May 27 05:54:24 2015] [error] updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
[Wed May 27 05:54:24 2015] [error] File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 698, in _save_table
[Wed May 27 05:54:24 2015] [error] result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
[Wed May 27 05:54:24 2015] [error] File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 731, in _do_insert
[Wed May 27 05:54:24 2015] [error] using=using, raw=raw)
[Wed May 27 05:54:24 2015] [error] File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 92, in manager_method
[Wed May 27 05:54:24 2015] [error] return getattr(self.get_queryset(), name)(*args, **kwargs)
[Wed May 27 05:54:24 2015] [error] File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 921, in _insert
[Wed May 27 05:54:24 2015] [error] return query.get_compiler(using=using).execute_sql(return_id)
[Wed May 27 05:54:24 2015] [error] File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 921, in execute_sql
[Wed May 27 05:54:24 2015] [error] cursor.execute(sql, params)
[Wed May 27 05:54:24 2015] [error] File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 65, in execute
[Wed May 27 05:54:24 2015] [error] return self.cursor.execute(sql, params)
[Wed May 27 05:54:24 2015] [error] File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 94, in __exit__
[Wed May 27 05:54:24 2015] [error] six.reraise(dj_exc_type, dj_exc_value, traceback)
[Wed May 27 05:54:24 2015] [error] File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 65, in execute
[Wed May 27 05:54:24 2015] [error] return self.cursor.execute(sql, params)
[Wed May 27 05:54:24 2015] [error] File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 485, in execute
[Wed May 27 05:54:24 2015] [error] return Database.Cursor.execute(self, query, params)
[Wed May 27 05:54:24 2015] [error] OperationalError: attempt to write a readonly database
Now I googled this error and searched a lot, I mean a lot! I know that the db.sqlite file must have write permissions and it should be owned by apache ( www-data ), also the directory containing it must have write permissions and be owned by www-data. and I have done these two things. I don't have selinux installed, I've ran ./manage.py migrate, and I don't know what else I can do to make this work, I even chmoded both the directory and the db.sqlite file to 777 to just see if it will work, but it didn't and I know that's not a safe thing to do.
now for your information this is the folder containing the db.sqlite file:
-rw-rw-r-- 1 ashkan ashkan 382 Jan 30 11:54 README.rst
-rw-rw-r-- 1 ashkan ashkan 0 Jan 30 11:54 __init__.py
drwxrwxr-x 4 ashkan ashkan 4096 Feb 6 15:15 apps
-rwxrwxrwx 1 www-data www-data 741376 May 27 09:11 db.sqlite
drwxrwxr-x 7 ashkan ashkan 4096 Jan 30 11:54 deploy
drwxrwxr-x 2 ashkan ashkan 4096 Jan 30 11:54 fixtures
drwxrwxr-x 2 ashkan ashkan 4096 Feb 22 00:14 i18n
drwxrwxr-x 3 ashkan ashkan 4096 Feb 23 21:45 locale
drwxr-xr-x 2 ashkan ashkan 4096 May 26 08:33 logs
-rwxrwxr-x 1 ashkan ashkan 242 May 26 00:22 manage.py
lrwxrwxrwx 1 ashkan ashkan 10 May 25 23:15 oscar -> i18n/oscar
drwxrwxr-x 4 ashkan ashkan 4096 Jan 30 21:53 public
-rw-rw-r-- 1 ashkan ashkan 14306 May 26 00:07 settings.py
-rw-r--r-- 1 ashkan ashkan 10230 May 26 00:23 settings.pyc
-rw-rw-r-- 1 ashkan ashkan 14876 Feb 6 14:26 settings.py~
-rw-rw-r-- 1 ashkan ashkan 293 Jan 30 11:54 settings_mysql.py
-rw-rw-r-- 1 ashkan ashkan 266 May 26 00:16 settings_postgres.py
-rw-rw-r-- 1 ashkan ashkan 162 Jan 30 11:54 settings_sphinx.py
drwxrwxr-x 2 ashkan ashkan 4096 Feb 23 21:45 static
drwxrwxr-x 3 ashkan ashkan 4096 Jan 30 11:54 templates
-rwxrwxr-x 1 ashkan ashkan 1114 Jan 30 11:54 test_migrations.sh
-rwxrwxr-x 1 ashkan ashkan 1138 Jan 30 11:54 update_latest.sh
-rw-rw-r-- 1 ashkan ashkan 1573 Jan 30 11:54 urls.py
-rw-rw-r-- 1 ashkan ashkan 1427 Jan 30 21:53 urls.pyc
drwxr-xr-x 2 ashkan ashkan 4096 Jan 30 20:58 whoosh_index
-rw-rw-r-- 1 ashkan ashkan 778 May 26 00:23 wsgi.py
and this is the directory containing the db.sqlite file:
-rw-rw-r-- 1 ashkan ashkan 866 Jan 30 11:54 README.rst
drwxrwxr-x 2 ashkan ashkan 4096 Jan 30 11:54 _fixtures
-rw-rw-r-- 1 ashkan ashkan 897149 Feb 6 15:34 alaki
-rw-rw-r-- 1 ashkan ashkan 1818857 Feb 6 15:37 alaki.txt
drwxrwxr-x 9 ashkan ashkan 4096 Jan 30 11:54 demo
-rw-rw-r-- 1 ashkan ashkan 443627 Feb 6 15:34 out
drwxrwxrwx 12 www-data www-data 4096 May 27 09:11 sandbox
drwxrwxr-x 5 ashkan ashkan 4096 Jan 30 11:54 us
any idea what's causing this problem? thank you.
update 1 :
here is my apache config :
Listen 8080
<VirtualHost *:8080>
WSGIDaemonProcess setak python-path=/home/ashkan/freshcopy/django-oscar/sites/sandbox:/usr/local/lib/python2.7/site-packages
WSGIProcessGroup setak
WSGIScriptAlias / /home/ashkan/freshcopy/django-oscar/sites/sandbox/wsgi.py
ServerAdmin admin#setakshop.ir
ServerName setakshop.ir:8000
DocumentRoot /var/www/
Alias /media/ /home/ashkan/freshcopy/django-oscar/sites/sandbox/public/media/
Alias /static/ /home/ashkan/freshcopy/django-oscar/sites/sandbox/public/static/
<Directory /home/ashkan/freshcopy/django-oscar/sites/sandbox>
<Files wsgi.py>
Order allow,deny
allow from all
</Files>
</Directory>
<Directory /home/ashkan/freshcopy/django-oscar/sites/sandbox/public/static>
Order allow,deny
allow from all
</Directory>
<Directory /home/ashkan/freshcopy/django-oscar/sites/sandbox/public/media>
Order allow,deny
allow from all
</Directory>
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel info
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
update 2 :
here is my wsgi.py :
import os
import sys
import site
import urllib
sys.stdout = sys.stderr
# Project root
root = '/home/ashkan/django-oscar/sites/sandbox'
sys.path.insert(0, root)
# Packages from virtualenv
activate_this = '/home/ashkan/django-oscar/oscar/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
# Set environmental variable for Django and fire WSGI handler
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from django.core.wsgi import get_wsgi_application
_application = get_wsgi_application()
def application(environ, start_response):
environ['PATH_INFO'] = urllib.unquote(environ['REQUEST_URI'].split('?')[0])
return _application(environ, start_response)