Search Postgresql Archives

connecting to server process via sockets

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



For fun and learning, I would like to connect to the Postgresql backend and issue queries using sockets. I'm using Python's socket module. I'm new to socket programming, but I'm experienced with Python and Postgresql. I've been using the JDBC driver and the online documentation as a guide, but I'm afraid my ignorance has led me to failure thus far.

This is what I think I understand.

1.  Send the startup message as such.
	a.  length of message.
	b.  protocol major (3)
	c.  protocol minor (0) (don't know what this is).
	d.  message
	e.  send 0 (Don't know why?)

O.K.  Here I show my ignorance.

#!/usr/bin/python

import socket

# Connection string
cnstring = 'user=randall, database=dws, client_encoding=UNICODE, DateStyle=ISO' # This just wrapped in my email.
msg_len = str(len(cnstring))
protocol_major = '3'
protocol_minor = '0'


pgsocket = socket.socket()
pgsocket.connect(('localhost', 5432))
pgsocket.send(msg_len)
pgsocket.send(protocol_major)
pgsocket.send(protocol_minor)
pgsocket.send(cnstring)
pgsocket.send('0')
pgsocket.close()


When I run this, this is what shows up in the logs.

2005-05-16 10:11:34 [2638] LOG: connection received: host=127.0.0.1 port=42607
2005-05-16 10:11:34 [2638] LOG: invalid length of startup packet


Please do not recommend that I use an existing API. I'm doing this for fun and maybe to come up with a simple pure python database driver.

Randall

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux