Unable to open a connection to PostgreSQL from Python3 - python

I'm running into a weird problem where I'm unable to connect to PostgreSQL from a Python 3.2 install. I'm running Fedora 15 and have installed Python3 and PostgerSQL9 from the Fedora repositories using yum. Does anyone have any ideas on why I'm seeing this problem and how to correct it? Google searches haven't been turning up anything.
I've changed the username, password, and database, but my pg_hba.conf file is correct.
import postgresql
t = postgresql.open(user='validuser', password='secret', database='some_database')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python3.2/site-packages/postgresql/__init__.py", line 88, in open
c.connect()
File "/usr/lib64/python3.2/site-packages/postgresql/driver/pq3.py", line 2419, in connect
pq = Connection3(sf, startup, password = password,)
File "/usr/lib64/python3.2/site-packages/postgresql/protocol/client3.py", line 514, in __init__
element.Startup(**startup), password
TypeError: keyword arguments must be strings
As a side note, I get this same error if I try to connect using different user, password, database combinations, and also if I use a pq://user:password#host/database connection string instead of keywords, both to localhost and to remote hosts.

I think that probably is some bug in python3-postgresql package, but it looks like it works after little change. Edit this file (probably /usr/lib64 for 64 bit installations):
/usr/lib/python3.2/site-packages/postgresql/protocol/client3.py
Change (line 514):
element.Startup(**startup), password
to:
element.Startup(startup), password
After that I made simple connection (I changed pg_hba.conf host methods to md5) and it looks ok:
[grzegorz#localhost Desktop]$ python3
Python 3.2 (r32:88445, Feb 21 2011, 21:12:33)
[GCC 4.6.0 20110212 (Red Hat 4.6.0-0.7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import postgresql
>>> db = postgresql.open("pq://grzegorz:12345#localhost/grzegorz")
>>> ps = db.prepare("SELECT version()")
>>> ps()
[('PostgreSQL 9.0.4 on i386-redhat-linux-gnu, compiled by GCC gcc (GCC) 4.6.0 20110530 (Red Hat 4.6.0-9), 32-bit',)]
>>> ps = db.prepare("TABLE t")
>>> ps()
[(1, 'aaa'), (2, 'bbb'), (3, 'ccc')]
>>>

Just a guess, but python is probably passing unicode to postgres and it's expecting strings.

You can also use another module - psycopg2 to connect to postgresql
http://initd.org/psycopg/download/

Related

How do i upload tinkergraph into python/gremlin?

I am trying to use gremlin in python. I imported the following:
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
from gremlin_python.structure.graph import Graph
from gremlin_python import statics
from gremlin_python.process.graph_traversal import __
from gremlin_python.process.strategies import *
from gremlin_python.process.traversal import *
import asyncio
statics.load_statics(globals())
When i run this:
graph = TinkerGraph.open()
graph.io(graphml()).readGraph('air-routes.graphml')
i get the following error:
NameError: name 'TinkerGraph' is not defined
How do i resolve this?
There is no TinkerGraph in Python. In gremlin-python you only get a reference to a graph remotely on a server and that might be a TinkerGraph or something else. If you want to load data that way, you must issue that command as a script through a Client instance:
client = Client('ws://localhost:45940/gremlin', 'g')
client.submit("graph.io(graphml()).readGraph('air-routes.graphml');[]").all().result()
where "graph" in that script is a Graph instance that already exists on the server (and is likely empty). If you're using Gremlin Server, you might consider doing that loading separately as part of Gremlin Server startup as well and then just using gremlin-python to query that data. That would probably be best in this example as the data would just be present when the server is started.
Note that in 3.4.0, we introduce the io() step which will be part of gremlin-python directly at which point you will be able to directly do:
g.io('air-routes.xml').read()
in native python and it will just work (again, the Graph instance must be defined remotely) though the file must be readable by the server.
Here's my working example in the Python shell for submitting a script, first with the tornado error and then without:
$ env/bin/python
Python 3.4.3 (default, Nov 28 2017, 16:41:13)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from gremlin_python.driver.client import Client
>>> client = Client('ws://localhost:8182/gremlin', 'g')
>>> client.submit("g.V()").all().result()
Traceback (most recent call last):
File "/home/smallette/git/apache/incubator-tinkerpop/gremlin-python/target/python3/gremlin_python/driver/client.py", line 51, in __init__
from gremlin_python.driver.tornado.transport import (
File "/home/smallette/git/apache/incubator-tinkerpop/gremlin-python/target/python3/gremlin_python/driver/tornado/transport.py", line 19, in <module>
from tornado import ioloop, websocket
ImportError: No module named 'tornado'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/smallette/git/apache/incubator-tinkerpop/gremlin-python/target/python3/gremlin_python/driver/driver_remote_connection.py", line 45, in __init__
password=password)
File "/home/smallette/git/apache/incubator-tinkerpop/gremlin-python/target/python3/gremlin_python/driver/client.py", line 54, in __init__
raise Exception("Please install Tornado or pass"
Exception: Please install Tornado or passcustom transport factory
>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
>>> exit()
$ env/bin/pip install tornado
Collecting tornado
Collecting backports-abc>=0.4 (from tornado)
Using cached https://files.pythonhosted.org/packages/7d/56/6f3ac1b816d0cd8994e83d0c4e55bc64567532f7dc543378bd87f81cebc7/backports_abc-0.5-py2.py3-none-any.whl
Installing collected packages: backports-abc, tornado
Successfully installed backports-abc-0.5 tornado-5.1.1
smallette#ubuntu:~/git/apache/incubator-tinkerpop/gremlin-python/target/python3$ env/bin/python
Python 3.4.3 (default, Nov 28 2017, 16:41:13)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from gremlin_python import statics
>>> client = Client('ws://localhost:8182/gremlin', 'g')
>>> client.submit("g.V()").all().result()
[v[0]]

Problems in pexpect (python3.3)

Running 3.3 python on CentOS 7.
Tryin' to write simple script but can't get pexpect module to work as I want
if I use interpreter python 3.3, I can write this commands correctly
[root#localhost expect]# python3.3
Python 3.3.3 (default, Apr 7 2015, 02:31:24)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pexpect
>>> child = pexpect.spawn('telnet 10.1.1.1')
but If I run file pexpect.py with exactly same commands, I get
[root#localhost expect]# python3.3 /usr/etc/pexpect.py
Traceback (most recent call last):
File "/usr/etc/pexpect.py", line 1, in <module>
import pexpect
File "/usr/etc/pexpect.py", line 3, in <module>
child = pexpect.spawn('telnet 10.1.1.1');
AttributeError: 'module' object has no attribute 'spawn'
I found some similar info in the google, advice was to move .py file to another folder.
It didn't work for me.
Another advice was to delete " pycache" folder (I've got same in my pexpect.py location), but it didn't work aswell. Errors are still the same, this folder are still created after running the script (trying, I mean).
Any ideas?
You have called your file pexpect.py. You need to rename it to something else as you are importing from your file not the pexpect module. You also need to delete any .pyc in the same folder. It does not matter where you move your script, the current folder is still going to be in the path before where the actual pexpect module is.

pattern.web URL dowload error -- IOError: [Errno 13] Permission denied

I am having trouble using the URL object from the web package of the pattern library. When I try to download text from a URL object, I get a permission error.
Python 2.7.3 (default, Dec 18 2014, 19:10:20)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pattern.web
>>> urlObject=pattern.web.URL("http://google.com")
>>> urlResponse = urlObject.download()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/pattern/web/__init__.py", line 426, in download
cache[id] = data
File "/usr/local/lib/python2.7/dist-packages/pattern/web/cache/__init__.py", line 96, in __setitem__
f = open(self._hash(k), "wb")
IOError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/pattern/web/cache/tmp/c7b920f57e553df2bb68272f61570210'
I had no problem with this when running python with sudo, but I would like to understand what is going on here and how I can avoid granting root permissions to python just to download a URL. Does anyone have any insight on this issue?
Running Ubuntu 12.04, python 2.7.3, pattern version 2.6
You need to run it in a directory where you have write access because it is attempting to create a cache file.
(Probably want to clean up cache files from when you ran it with sudo because it seems to have polluted your system directories with chaff.)
It looks like you could try
urlResponse = urlObject.download(cached=False)
to disable the caching functionality.
Changing the owner/group of /usr/local/lib/python2.7/dist-packages/pattern/web/ to my user seems to have resolved the issue. I installed pattern via pip, nothing fancy, so I am assuming this is a bug and the permissions were too tight when pattern was installed.

Undefined symbol "afinfo" when importing python-iptables package "iptc"

I am trying to utilize the python-iptables package to list iptables rules in a web app. When I add the iptc package to my environment, I get the below error. I used yum 'provides' to find where the libxtables.so.4 file comes from and found that the iptables and iptables-devel packages were the appropriate choice in CentOS 6.4 x64. I upgraded those packages but it did not change the error.
Does anyone have any suggestions about how I can resolve this?
pgrace#ny-misc01:~/repos/python-iptables/libxtwrapper$ python
Python 2.6.6 (r266:84292, Sep 11 2012, 08:34:23)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import iptc
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib64/python2.6/site-packages/iptc/__init__.py", line 10, in
from ip4tc import Table, Chain, Rule, Match, Target, Policy, IPTCError
File "/usr/lib64/python2.6/site-packages/iptc/ip4tc.py", line 11, in
from xtables import (XT_INV_PROTO, NFPROTO_IPV4, XTablesError, xtables,
File "/usr/lib64/python2.6/site-packages/iptc/xtables.py", line 744, in
class xtables(object):
File "/usr/lib64/python2.6/site-packages/iptc/xtables.py", line 757, in xtables
_xtables_afinfo = ct.c_void_p.in_dll(_lib_xtables, "afinfo")
ValueError: /lib64/libxtables.so.4: undefined symbol: afinfo
>>>
See this: https://github.com/ldx/python-iptables/issues/25
This is a known problem, old versions of libxtables declared afinfo as static, thus it is not accessible for python-iptables. There is a possible workaround, though - please keep an eye on the ticket, it will be updated as soon as there has been progress.
Another solution is to update iptables on your machine.
Disclaimer: I am the maintainer of python-iptables.
Update: this should be fixed now.

os.tmpfile() doesn't work under windows?

I'm trying to use generateDS under windows, which uses os.tmpfile. Unfortunately, os.tmpfile doesn't work for me:
(oneclickcos) C:\Users\Marcin\Documents\oneclickcos\xsd>python
Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.tmpfile()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 13] Permission denied
>>>
I've got all my temp directories set with full control for everyone, so that shouldn't be the problem.
What could be causing this?
Run the script as administrator (right click on the script and select 'run as administrator'), the script lacks the permissions to execute os.tmpfile().
Edit:
As I see you're using the interpreter, simply run the interpreter as administrator. If you're accessing it though a terminal, running the terminal as administrator should be sufficient.
As Griffin pointed out the problem is that the os.tmpfile() tries to create a file in the root directory. If you don't like to run the script as administrator you can use os.tmpnam() and handle the file yourself.
Warning: Use of tmpnam() is vulnerable to symlink attacks

Categories