pymongo gridfs mongodb authentication fails - python

I am using gridfs to store and get files from my MongoDB database
fs = gridfs.GridFS(MongoClient('mongodb://username:password#XX.XX.XX.XXX:27017/').mydb)
and to get a file I am using
file = fs.get_last_version(file_name).read()
On this line I get Authentication Failed exception.
Traceback (most recent call last):
File "C:\Python36\lib\site-packages\flask\app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python36\lib\site-packages\flask\app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "C:\Python36\lib\site-packages\flask\app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python36\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Python36\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python36\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python36\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python36\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Python36\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python36\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "D:\git\EEG\ABM\BLabPythonService\app.py", line 1039, in start_processing
sys.stderr.flush()
File "D:\git\EEG\ABM\BLabPythonService\process_studies.py", line 271, in start_processing
overlay_artifacts(study)
File "D:\git\EEG\ABM\BLabPythonService\process_studies.py", line 49, in overlay_artifacts
edf_file = fs.get_last_version(file_name).read()
File "C:\Python36\lib\site-packages\gridfs\__init__.py", line 200, in get_last_version
return self.get_version(filename=filename, **kwargs)
File "C:\Python36\lib\site-packages\gridfs\__init__.py", line 184, in get_version
grid_file = next(cursor)
File "C:\Python36\lib\site-packages\pymongo\cursor.py", line 1132, in next
if len(self.__data) or self._refresh():
File "C:\Python36\lib\site-packages\pymongo\cursor.py", line 1055, in _refresh
self.__collation))
File "C:\Python36\lib\site-packages\pymongo\cursor.py", line 892, in __send_message
**kwargs)
File "C:\Python36\lib\site-packages\pymongo\mongo_client.py", line 950, in _send_message_with_response
exhaust)
File "C:\Python36\lib\site-packages\pymongo\mongo_client.py", line 961, in _reset_on_error
return func(*args, **kwargs)
File "C:\Python36\lib\site-packages\pymongo\server.py", line 99, in send_message_with_response
with self.get_socket(all_credentials, exhaust) as sock_info:
File "C:\Python36\lib\contextlib.py", line 81, in __enter__
return next(self.gen)
File "C:\Python36\lib\site-packages\pymongo\server.py", line 168, in get_socket
with self.pool.get_socket(all_credentials, checkout) as sock_info:
File "C:\Python36\lib\contextlib.py", line 81, in __enter__
return next(self.gen)
File "C:\Python36\lib\site-packages\pymongo\pool.py", line 852, in get_socket
sock_info.check_auth(all_credentials)
File "C:\Python36\lib\site-packages\pymongo\pool.py", line 570, in check_auth
auth.authenticate(credentials, self)
File "C:\Python36\lib\site-packages\pymongo\auth.py", line 486, in authenticate
auth_func(credentials, sock_info)
File "C:\Python36\lib\site-packages\pymongo\auth.py", line 466, in _authenticate_default
return _authenticate_scram_sha1(credentials, sock_info)
File "C:\Python36\lib\site-packages\pymongo\auth.py", line 237, in _authenticate_scram_sha1
res = sock_info.command(source, cmd)
File "C:\Python36\lib\site-packages\pymongo\pool.py", line 477, in command
collation=collation)
File "C:\Python36\lib\site-packages\pymongo\network.py", line 116, in command
parse_write_concern_error=parse_write_concern_error)
File "C:\Python36\lib\site-packages\pymongo\helpers.py", line 210, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: Authentication failed.
Everything worked fine when my mongo database didnt have authentication.
Other use of database is working fine. I have this python service that is connecting to database using flask_pymogno and a c# application that is also connecting to database using c#, and all connections are with the same credentials.

Solved it. The problem was actually in my password witch contained '+' character. It is a reserved URI character. Fixed it by replacing '+' with '%2B' and it now works.
Reference:
https://en.wikipedia.org/wiki/Percent-encoding

Related

SQLAlchemy and mysql throwing errors on concurrent requests

I am writing an application and whenever I do one request at a time, everything is fine. but when my vuejs frontend does a call to my flask backend, using 2 requests at the same time, I get a big error, see below. This is presumably because flask is still processing the previous request and has a lock on the database. Is there a way to make my MySQL either asynchronous or to make them queue up? Even changing to a better database solution is OK. As long as I can have multiple requests in flask at the same time.
app.py
session = classes.Session()
# configuration
DEBUG = True
# instantiate the app
app = Flask(__name__)
app.config.from_object(__name__)
classes.py
ssl_args = {'ssl_ca': './ca.crt'}
engine = create_engine("URL", connect_args=ssl_args)
Session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
Base.query = Session.query_property()
The error:
Traceback (most recent call last):
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\pymysql\connections.py", line 756, in _write_bytes
self._sock.sendall(data)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1264.0_x64__qbz5n2kfra8p0\Lib\ssl.py", line 1204, in sendall
v = self.send(byte_view[count:])
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1264.0_x64__qbz5n2kfra8p0\Lib\ssl.py", line 1173, in send
return self._sslobj.write(data)
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:2477)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\sqlalchemy\engine\base.py", line 1706, in _execute_context
self.dialect.do_execute(
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\sqlalchemy\engine\default.py", line 716, in do_execute
cursor.execute(statement, parameters)
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\pymysql\cursors.py", line 148, in execute
result = self._query(query)
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\pymysql\cursors.py", line 310, in _query
conn.query(q)
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\pymysql\connections.py", line 547, in query
self._execute_command(COMMAND.COM_QUERY, sql)
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\pymysql\connections.py", line 814, in _execute_command
self._write_bytes(packet)
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\pymysql\connections.py", line 759, in _write_bytes
raise err.OperationalError(
pymysql.err.OperationalError: (2006, "MySQL server has gone away (SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:2477)'))")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\flask\app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\flask_cors\extension.py", line 165, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\flask\app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\flask_cors\extension.py", line 165, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "E:\Documents\GitHub\thought4food\app\app.py", line 160, in recipeById
return json.loads(str(recipe))
File "E:\Documents\GitHub\thought4food\app\classes\recipe.py", line 21, in __repr__
ingredients = json.loads(str(self.recipe_ingredients))
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\sqlalchemy\orm\attributes.py", line 465, in __get__
return self.impl.get(state, dict_)
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\sqlalchemy\orm\attributes.py", line 911, in get
value = self.callable_(state, passive)
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\sqlalchemy\orm\strategies.py", line 878, in _load_for_state
return self._emit_lazyload(
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\sqlalchemy\orm\strategies.py", line 1041, in _emit_lazyload
result = session.execute(
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\sqlalchemy\orm\session.py", line 1670, in execute
result = conn._execute_20(statement, params or {}, execution_options)
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\sqlalchemy\engine\base.py", line 1521, in _execute_20
return meth(self, args_10style, kwargs_10style, execution_options)
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\sqlalchemy\sql\lambdas.py", line 479, in _execute_on_connection
return connection._execute_clauseelement(
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\sqlalchemy\engine\base.py", line 1390, in _execute_clauseelement
ret = self._execute_context(
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\sqlalchemy\engine\base.py", line 1749, in _execute_context
self._handle_dbapi_exception(
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\sqlalchemy\engine\base.py", line 1930, in _handle_dbapi_exception
util.raise_(
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\sqlalchemy\util\compat.py", line 211, in raise_
raise exception
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\sqlalchemy\engine\base.py", line 1706, in _execute_context
self.dialect.do_execute(
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\sqlalchemy\engine\default.py", line 716, in do_execute
cursor.execute(statement, parameters)
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\pymysql\cursors.py", line 148, in execute
result = self._query(query)
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\pymysql\cursors.py", line 310, in _query
conn.query(q)
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\pymysql\connections.py", line 547, in query
self._execute_command(COMMAND.COM_QUERY, sql)
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\pymysql\connections.py", line 814, in _execute_command
self._write_bytes(packet)
File "E:\Documents\GitHub\thought4food\app\env\Lib\site-packages\pymysql\connections.py", line 759, in _write_bytes
raise err.OperationalError(
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2006, "MySQL server has gone away (SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:2477)'))")
[SQL: SELECT ingredients.id AS ingredients_id, ingredients.name AS ingredients_name, ingredients.amount AS ingredients_amount, ingredients.unit AS ingredients_unit
FROM ingredients, recipe_ingredients
WHERE %(param_1)s = recipe_ingredients.recipe_id AND ingredients.id = recipe_ingredients.ingredient_id]
[parameters: {'param_1': 0}]
(Background on this error at: http://sqlalche.me/e/14/e3q8)
I found the solution.
I changed session = classes.Session() to session = classes.Session and I added:
#app.teardown_request
def remove_session(ex=None):
session.remove()
This made sure that the session was no longer in use and gave control of the session to flask.

How to solve 'no module named mysqldb'?

I am new to python. I am following a tutorial series tk make a website using flask. But when i fill up the contact form and then click on send i get this error
ModuleNotFoundError: No module named 'MySQLdb'
Traceback (most recent call last)
File "C:\Users\acer\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\sqlalchemy\util\_collections.py", line 1020, in __call__
return self.registry[key]
During handling of the above exception, another exception occurred:
File "C:\Users\acer\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\acer\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\acer\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\acer\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\acer\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\acer\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\acer\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\acer\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\acer\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\acer\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\acer\PycharmProjects\webdev.flask\first.1.py", line 46, in contact
db.session.add(entry)
File "C:\Users\acer\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\sqlalchemy\orm\scoping.py", line 163, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "C:\Users\acer\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\sqlalchemy\util\_collections.py", line 1022, in __call__
return self.registry.setdefault(key, self.createfunc())
File "C:\Users\acer\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\sqlalchemy\orm\session.py", line 3286, in __call__
return self.class_(**local_kw)
File "C:\Users\acer\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask_sqlalchemy\__init__.py", line 138, in __init__
bind = options.pop('bind', None) or db.engine
File "C:\Users\acer\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask_sqlalchemy\__init__.py", line 943, in engine
return self.get_engine()
File "C:\Users\acer\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask_sqlalchemy\__init__.py", line 962, in get_engine
return connector.get_engine()
File "C:\Users\acer\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask_sqlalchemy\__init__.py", line 556, in get_engine
self._engine = rv = self._sa.create_engine(sa_url, options)
File "C:\Users\acer\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask_sqlalchemy\__init__.py", line 972, in create_engine
return sqlalchemy.create_engine(sa_url, **engine_opts)
File "C:\Users\acer\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\sqlalchemy\engine\__init__.py", line 500, in create_engine
return strategy.create(*args, **kwargs)
File "C:\Users\acer\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\sqlalchemy\engine\strategies.py", line 87, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "C:\Users\acer\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\sqlalchemy\dialects\mysql\mysqldb.py", line 118, in dbapi
return __import__("MySQLdb")
ModuleNotFoundError: No module named 'MySQLdb'
I tried to install mysqlient but it also gives ' cant build the wheel ' error.
I am using python 3.8
How to solve this issue?
if you use pycharm then file - > settings -> interpreter - > + (right). there you install wheel and mysql client (not always the latest version works)
if manually I assume that you are using a virtual environment, then you need to activate it and it already has "pip install wheel" also mysqlclient

Unable to send POST request using Flask test client

I'm following the TestDriven.io course, after setting up restful routes I encounter this error even after adding the route handler in project/api/users.py:
test_app_is_development (test_config.TestDevelopmentConfig) ... ok
test_app_is_production (test_config.TestProductionConfig) ... ok
test_app_is_testing (test_config.TestTestingConfig) ... ok
test_add_user (test_users.TestUserService)
Ensure a new user can be added to the database ... ERROR
test_users (test_users.TestUserService)
Ensure the /ping route behaves correctly. ... ok
======================================================================
ERROR: test_add_user (test_users.TestUserService)
Ensure a new user can be added to the database
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1283, in _execute_context
self.dialect.do_execute(
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 590, in do_execute
cursor.execute(statement, parameters)
psycopg2.ProgrammingError: can't adapt type 'now'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/src/app/project/tests/test_users.py", line 22, in test_add_user
response = self.client.post(
File "/usr/local/lib/python3.8/site-packages/werkzeug/test.py", line 1016, in post
return self.open(*args, **kw)
File "/usr/local/lib/python3.8/site-packages/flask/testing.py", line 222, in open
return Client.open(
File "/usr/local/lib/python3.8/site-packages/werkzeug/test.py", line 970, in open
response = self.run_wsgi_app(environ.copy(), buffered=buffered)
File "/usr/local/lib/python3.8/site-packages/werkzeug/test.py", line 861, in run_wsgi_app
rv = run_wsgi_app(self.application, environ, buffered=buffered)
File "/usr/local/lib/python3.8/site-packages/werkzeug/test.py", line 1096, in run_wsgi_app
app_rv = app(environ, start_response)
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python3.8/site-packages/flask_restful/__init__.py", line 272, in error_router
return original_handler(e)
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.8/site-packages/flask/_compat.py", line 38, in reraise
raise value.with_traceback(tb)
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.8/site-packages/flask_restful/__init__.py", line 272, in error_router
return original_handler(e)
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.8/site-packages/flask/_compat.py", line 38, in reraise
raise value.with_traceback(tb)
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python3.8/site-packages/flask_restful/__init__.py", line 468, in wrapper
resp = resource(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/flask/views.py", line 89, in view
return self.dispatch_request(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/flask_restful/__init__.py", line 583, in dispatch_request
resp = meth(*args, **kwargs)
File "/usr/src/app/project/api/users.py", line 28, in post
db.session.commit()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/scoping.py", line 163, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 1042, in commit
self.transaction.commit()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 504, in commit
self._prepare_impl()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 483, in _prepare_impl
self.session.flush()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 2523, in flush
self._flush(objects)
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 2664, in _flush
transaction.rollback(_capture_exception=True)
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
compat.raise_(
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
raise exception
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 2624, in _flush
flush_context.execute()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/unitofwork.py", line 422, in execute
rec.execute(self)
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/unitofwork.py", line 586, in execute
persistence.save_obj(
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py", line 239, in save_obj
_emit_insert_statements(
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py", line 1135, in _emit_insert_statements
result = cached_connections[connection].execute(
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1020, in execute
return meth(self, multiparams, params)
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/sql/elements.py", line 298, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1133, in _execute_clauseelement
ret = self._execute_context(
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1323, in _execute_context
self._handle_dbapi_exception(
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1517, in _handle_dbapi_exception
util.raise_(
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
raise exception
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1283, in _execute_context
self.dialect.do_execute(
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 590, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'now'
[SQL: INSERT INTO users (username, email, active, created_date) VALUES (%(username)s, %(email)s, %(active)s, %(created_date)s) RETURNING users.id]
[parameters: {'username': 'michael', 'email': 'michael#mherman.org', 'active': True, 'created_date': <sqlalchemy.sql.functions.now at 0x7f5369329f40; now>}]
(Background on this error at: http://sqlalche.me/e/f405)
----------------------------------------------------------------------
Ran 5 tests in 0.307s
FAILED (errors=1)
<unittest.runner.TextTestResult run=5 errors=1 failures=0>
As you can see the error has to do with the self.client.post request done in this function:
def test_add_user(self):
"""Ensure a new user can be added to the database"""
with self.client:
response = self.client.post(
'/users',
data=json.dumps({
'username': 'michael',
'email': 'michael#mherman.org'
}),
content_type='application/json',
)
print (response)
data = json.loads(response.data.decode())
self.assertEqual(response.status_code, 201)
self.assertIn('michael#mherman.org was added!', data['message'])
self.assertIn('success', data['status'])
Everything seems correct in the logic and syntax of the code but I can't figure out why the test isn't passing?
Judging by the error log, the problem is in the INSERT INTO statement. The created_date parameter is not a date but a function sqlalchemy.sql.functions.now. It looks like you forgot to call it.

Connect via py2neo to a neo4j with password secured http protocol

Hello I am trying to connect to a neo4j database with py2neo.
This code works so far:
graph = Graph(bolt=True, host='***',
bolt_port=***,
http_port=***,
user='***',
password='***')
But when I protect my IP address with a password over HTTP then I can't connect and I don't know how to authorize py2neo to connect.
Does anyone know how I can solve this? :)
Edit :
When I open the ip in a web browser i need to input a user and a password before i can see the neo4j browser - and I do not now how to input these credentias with py2neo (because it seems that this is my problem)
Stacktrace:
Traceback (most recent call last):
File "WebApp35\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "WebApp35\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "WebApp35\lib\site-packages\flask_restful\__init__.py", line 271, in error_router
return original_handler(e)
File "WebApp35\lib\site-packages\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "WebApp35\lib\site-packages\flask\_compat.py", line 32, in reraise
raise value.with_traceback(tb)
File "WebApp35\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "WebApp35\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "WebApp35\lib\site-packages\flask_restful\__init__.py", line 271, in error_router
return original_handler(e)
File "WebApp35\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "WebApp35\lib\site-packages\flask\_compat.py", line 32, in reraise
raise value.with_traceback(tb)
File "WebApp35\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "WebApp35\lib\site-packages\flask\app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "WebApp35\lib\site-packages\flask_restful\__init__.py", line 477, in wrapper
resp = resource(*args, **kwargs)
File "WebApp35\lib\site-packages\flask\views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "WebApp35\lib\site-packages\flask_restful\__init__.py", line 587, in dispatch_request
resp = meth(*args, **kwargs)
File "app\handlers\nodeHandlers.py", line 70, in get
return DataManager.get_suggestion(suggestion_string), 201, {
File "app\adapter\dataManager.py", line 44, in get_suggestion
return cls.adapter.get_suggestion(suggestion_string)
File "app\adapter\neoAdapter.py", line 337, in get_suggestion
for node in cls.cypher.run(query):
File "WebApp35\lib\site-packages\py2neo\database\__init__.py", line 676, in run
return self.begin(autocommit=True).run(statement, parameters, **kwparameters)
File "WebApp35\lib\site-packages\py2neo\database\__init__.py", line 351, in begin
return self.transaction_class(self, autocommit)
File "WebApp35\lib\site-packages\py2neo\database\__init__.py", line 1171, in __init__
self.session = driver.session()
File "WebApp35\lib\site-packages\py2neo\packages\neo4j\v1\session.py", line 148, in session
session = Session(self)
File "WebApp35\lib\site-packages\py2neo\packages\neo4j\v1\session.py", line 461, in __init__
self.connection = connect(driver.host, driver.port, driver.ssl_context, **driver.config)
File "WebApp35\lib\site-packages\py2neo\packages\neo4j\v1\connection.py", line 399, in connect
raise error
py2neo.packages.neo4j.v1.exceptions.ProtocolError: Cannot establish secure connection; [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:720)

Python flask and restless - how to return gz file of json object

I I am using flask-restless. I am trying to respond of a get request of a gz file of a json object from s3 and do not understand how to send a gz file.
class report_download(Resource):
def get(self,report_name):
conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
bucket = conn.get_bucket(bucketname)
key = Key(bucket, report_name)
key.get_contents_to_filename('/tmp/%s' % report_name)
os.system('gzip /tmp/%s' % report_name)
data = open('/tmp/%s.gz' % report_name).readlines()[0]
return data
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/SocketServer.py", line 651, in __init__
self.finish()
File "/usr/lib/python2.7/SocketServer.py", line 710, in finish
self.wfile.close()
File "/usr/lib/python2.7/socket.py", line 279, in close
self.flush()
File "/usr/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
File "/usr/local/lib/python2.7/dist-packages/gevent-1.0b2-py2.7-linux-x86_64.egg/gevent/socket.py", line 468, in sendall
data_sent += self.send(_get_memory(data, data_sent), flags)
File "/usr/local/lib/python2.7/dist-packages/gevent-1.0b2-py2.7-linux-x86_64.egg/gevent/socket.py", line 439, in send
return sock.send(data, flags)
error: [Errno 32] Broken pipe
127.0.0.1 - - [26/May/2016 03:29:03] "GET /api/driver/report/download/7000_upload_.json HTTP/1.1" 500 -
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 268, in error_router
return self.handle_error(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 268, in error_router
return self.handle_error(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 481, in wrapper
return self.make_response(data, code, headers=headers)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 510, in make_response
resp = self.representations[mediatype](data, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/representations/json.py", line 20, in output_json
dumped = dumps(data, **settings) + "\n"
File "/usr/lib/python2.7/json/__init__.py", line 250, in dumps
sort_keys=sort_keys, **kw).encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 201, in encode
return encode_basestring_ascii(o)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x8b in position 1: invalid start byte
Zipped data is binary, so, readlines probably only reads corrupted data.
You can use python module gzip, without the need of any external program:
import gzip
class report_download(Resource):
def get(self,report_name):
conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
bucket = conn.get_bucket(bucketname)
key = Key(bucket, report_name)
contents = key.get_contents_as_string()
data = StringIO.StringIO()
zip = gzip.GzipFile(fileobj=data, mode='wb')
zip.write(contents)
zip.close()
return Response(data.getvalue(), mimetype='application/x-gzip')

Categories

Resources