tornado and postgresql - python

I'm using demo from facebook's tornado
But I don't want to use MySQL and trying to replace it with PG
So I went ahead and modified like this :
define("port", default=8888, help="run on the given port", type=int)
define("pgsql_host", default="127.0.0.1:5432", help="blog database host")
define("pgsql_database", default="pgdb", help="blog database name")
define("pgsql_user", default="admin", help="blog database user")
define("pgsql_password", default="pgpass", help="blog database password")
and
# Have one global connection to the blog DB across all handlers
self.db = tornado.database.Connection(
host=options.pgsql_host, database=options.pgsql_database,
user=options.pgsql_user, password=options.pgsql_password)
But app just hangs when I run it, how to properly migrate it to PG?
Actually I get error after some time:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/tornado-2.4.1-py2.7.egg/tornado/database.py", line 84, in __init__
self.reconnect()
File "/usr/local/lib/python2.7/dist-packages/tornado-2.4.1-py2.7.egg/tornado/database.py", line 101, in reconnect
self._db = MySQLdb.connect(**self._db_args)
File "/usr/lib/pymodules/python2.7/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/pymodules/python2.7/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2013, "Lost connection to MySQL server at 'reading initial communication packet', system error: 0")
Why is it connecting to MySQL?

Are you sure you can use it with different RDBMS including PostgreSQL?
It uses torndb package which is written to work with MySQL.
So i think that if you want to use it with PostgreSQL you should write your own torndb package for postgresql.
EDIT:
As you can see at Tornado Wiki you have to have postgresql wrapper (momoko or psycopg) there are some links for further study.
EDIT2: Momoko description:
"An asynchronous Psycopg2 wrapper for Tornado."

tornado.database (which will be broken out of Tornado in 3.0) is a lightweight MySQL wrapper. It doesn't support anything other than MySQL.
The good news is, as #sufleR noted, you have some options. You can use pyscopg directly, or you can use momoko, an async wrapper for pyscopg.

Related

psycopg2:OperationalError: Unknow host in Python Tkinter app

Developing a desktop application with tkinter, I had an error connecting the database to it.
I'm new to this, I was working locally but I don't know what would happen if I want to share my app with other people. The app would not have access to this database.
I tried to create a database in render.com, once created I made the connection to it through psycopg2 passing the data (hostname, port, db_name, username, and password), but it throws me the error psycopg2:OperationalError.
I know that I am doing something wrong and I have been investigating but I have not been able to find the solution to this. So, when deploying a tkinter app, what database could it use? so that everyone can use the app correctly
I would appreciate any help you can give me, thanks in advance.
Here are the connection settings, which are the same settings from the database created on render.com (I've hidden the password and host):
conn = psycopg2.connect(
dbname='tkinter_app',
user='tkinter_app_user',
password='#############',
host='dpg-cfkimi9mbjsn9eclo3dg-a',
port='5432',
)
And here is the error log:
Traceback (most recent call last):
File "c:\Users\luigi\Desktop\Programacion\tkinter-postgresql-desktop\src\student.py", line 134, in <module>
display_students()
File "c:\Users\luigi\Desktop\Programacion\tkinter-postgresql-desktop\src\student.py", line 27, in display_students
conn = psycopg2.connect(
File "C:\Users\luigi\Desktop\Programacion\tkinter-postgresql-desktop\venv\lib\site-packages\psycopg2\__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not translate host name "dpg-cfkimi9mbjsn9eclo3dg-a" to address: Unknown host
When I created the database, I copied all the configurations as they were, investigating I read that the host should be the External Database URL showed in the rander database configuration, but it didn't work either and it also throws me the error: Unknown Server error
I also try to connect the database in pgAdmin but throws me the same error Unknown host

Cannot acquire connection to Neo4j database

I am trying to connect to my Neo4j graph database server from a new machine. I can successfully connect from an older machine but do not wish to use the older one anymore.
I have reduced the problem to a simple script that returns an exception:
from neo4j.v1 import GraphDatabase, basic_auth
auth = basic_auth("username","password")
session = GraphDatabase.driver("bolt://remote.server:7687",auth=auth).session()
statement = """MATCH (a:Protein)
WHERE a.name={name}
RETURN a.Accession"""
tx = session.begin_transaction()
record = tx.run(statement,{'name':"ARCH_HUMAN"}).single()
print record['a.Accession']
session.close()
And the error message is:
File "Test.py", line 10, in <module>
tx = session.begin_transaction()
File "/home/username/anaconda2/lib/python2.7/site-packages/neo4j/v1/api.py", line 432, in begin_transaction
self._connect()
File "/home/username/anaconda2/lib/python2.7/site-packages/neo4j/v1/api.py", line 269, in _connect
self._connection = self._acquirer(access_mode)
File "/home/username/anaconda2/lib/python2.7/site-packages/neo4j/v1/direct.py", line 52, in acquire
raise ServiceUnavailable("Cannot acquire connection to {!r}".format(self.address))
neo4j.exceptions.ServiceUnavailable: Cannot acquire connection to Address(host='remote.server', port=7687)
Port 7687 is open (confirmed via netstat -tulpn and iptables -L), and neo4j is configured to listen to 0.0.0.0:7687. In addition, .neo4j/known_hosts contains an entry for host 0.0.0.0
What's strange is that I get a different error message (neo4j.exceptions.AuthError) if I break the authentication by using an incorrect password. So the connection is being made to check the password, but still I cannot connect with the correct auth.
What's going on?
I too had the same issue and turns out the driver was the issue.
I did some experiments and found out that the last driver that it works for is neo4j-driver==v1.1.0 but the next version neo4j-driver==v1.2.0 it stops working for some reason.
Try uncomment dbms.connectors.default_listen_address=0.0.0.0 And check this
# Bolt connector
dbms.connector.bolt.enabled=true
dbms.connector.bolt.tls_level=OPTIONAL
dbms.connector.bolt.listen_address=:7687
# HTTP Connector. There must be exactly one HTTP connector.
dbms.connector.http.enabled=true
dbms.connector.http.listen_address=:7474
# HTTPS Connector. There can be zero or one HTTPS connectors.
dbms.connector.https.enabled=true
dbms.connector.https.listen_address=:7473

Issue to connect into a mysql database with MySQLdb module (python)

I have a database in mysql and I want to connect to it. I am trying to use this module from Python called MySQLdb. I created an user (called abc) and password (abc) for this database (abc) that has one table and it is connecting ok (when I connect by mysql command line).
But when I run my python script there is an error in the connection.
My script is:
#!/usr/bin/python
import MySQLdb
# Open database connection
db = MySQLdb.connect("localhost","abc","abc","abc")
# prepare a cursor object using cursor() method
cursor = db.cursor()
# execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")
# Fetch a single row using fetchone() method.
data = cursor.fetchone()
print "Database version : %s " % data
# disconnect from server
db.close()
My error is:
Traceback (most recent call last):
File "./test.py", line 8, in <module>
db = MySQLdb.connect("localhost","abc","abc","abc")
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py", line 193, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (1044, "Access denied for user 'abc'#'localhost' to database 'abc'")
What is wrong? My script or something in my mysql?
I changed my localhost to 127.0.0.1 (as suggested in another post, but did not solve my issue.
I also checked my permissions for this mysql user:
SHOW GRANTS;
+------------------------------------------------------------------------------+
| Grants for abc#localhost |
+------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'abc'#'localhost' |
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, CREATE VIEW ON `abc`.* TO 'abc'#'localhost' |
+------------------------------------------------------------------------------+
You might need to restart the mysql daemon for the privileges to take affect. Or use the flush privileges command. https://dev.mysql.com/doc/refman/5.7/en/privilege-changes.html

Python pymssql - Connecting to Named Instance

I'm trying to connect to a SQL Server named instance from python 3.4 on a remote server, and get an error.
File "C:\Scripts\Backups Integrity Report\Backup Integrity Reports.py", line 269, in
conn = pymssql.connect(host=r'hwcvcs01\HDPS', user='My-office\romano', password='PASS', database='CommServ')
File "pymssql.pyx", line 636, in pymssql.connect (pymssql.c:10178)
pymssql.OperationalError: (20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed\n')
Other SQLs are connected without a problem. Also I manage to connect to the SQL using the Management Studio, from the same remote server.
Tried different ports, tried to connect to the host itself rather than the instance, and also tried pypyodbc.
What might be the problem?
For a named instance that looks like this:
myhost\myinstance,1435
You can connect via pymssql with the following:
pymssql.connect(host='myhost', server='myinstance', port='1435', user='user', password='pw')
According to the pymssql documentation on the pymssql Connection class, for a named instance containing database theDatabase, looking like this:
myhost\myinstance
You could connect as follows:
pymssql.connect(host=r'myhost\myinstance', database='theDatabase', user='user', password='pw')
The r-string is a so-called raw string that does not treat the '' as an escape.

python MySQLdb godaddy.com hosting

import MySQLdb
conn = MySQLdb.connect('localhost', 'user', 'pwd', 'sampledb')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/pymodules/python2.7/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/pymodules/python2.7/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (110)")
How do I connect and what is the issue here? Thank you!
You do not connect to a database on localhost on Go Daddy shared hosting. You will need to locate the correct database host.
Log in to your Account Manager.
Click Web Hosting.
Next to the hosting account you want to use, click Launch.
In the Databases section of the Hosting Control Center, click the MySQL icon.
Click the pencil icon next to the database you would like to get connection strings for.
Your database host name displays in the Host Name field. It will end with "hostedresource.com".
See godaddy's help pages:
http://support.godaddy.com/help/39
import MySQLdb
conn = MySQLdb.connect("host", "user", "password", "database")
host: Get in cPanel "Web sites IP".
user: This user must to have permissions to access (You can add this permission in MySQL Databases menu in cPanel).
database: Name database.
Note: Add permission for your IP in Remote MySQL Menu in cPanel
You can use sqlalchemy
from sqlalchemy import create_engine
engine = create_engine('mysql+pymysql://user:password#host/database')
conn = engine.connect()

Categories

Resources