connecting to server process via sockets

From: Randall Smith <randall(at)tnr(dot)cc>
To: pgsql-general(at)postgresql(dot)org
Subject: connecting to server process via sockets
Date: 2005-05-16 15:14:43
Message-ID: 4288B8E3.3090906@tnr.cc
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

Responses

Browse pgsql-general by date

  From Date Subject
Next Message John DeSoi 2005-05-16 15:32:19 Re: connecting to server process via sockets
Previous Message Michael Fuhr 2005-05-16 14:14:05 Re: Sequences question & problem