#!/usr/bin/python3

from socket import *
from sys import argv

default_host = "localhost"
portnum = 5432

def talk():
	rhost = default_host
	if len(argv) > 1:
		rhost = argv[1]
	print("Looking up address of " + rhost + "...", end="")
	try:
	    dest = gethostbyname(rhost)
	except gaierror as mesg:
	    errno,errstr=mesg.args
	    print("\n   ", errstr);
	    return;
	except herror as mesg:
	    errno,errstr=mesg.args
	    print("\n HERROR: ", errstr);
	    return;
	print("got it: " + dest)
	addr=(dest, portnum)
	s = socket()
	res=s.connect_ex(addr)
	if res!=0: 
		print("connect to port ", portnum, " failed")
		exit
	s.settimeout(1.1)
	while True:
	    buf = input("> ")
	    if len(buf) == 0: return
	    buf = buf + "\n"
	    # s.send(bytes(buf, 'iso-8859-1'))
	    s.send(bytes(buf, 'ascii'))
	

		
talk()
