#Mini KPML client example in Python
#
# For variation, this is a command line generator that takes an SPL
# from the command line and then prints out the generated result,
# sending the SPL to a running KPML server.
#
# An example of use would be:
#
#  python kpml-mini-client.py "(s / sail :actor (p / ship))"
#
# Does not do anything about checking that input, etc. is OK, this
# should be done prior to sending any SPL at all... 
#
# The server can also be closed down by sending :stop ... otherwise
# remains open and waiting for SPLs.
#
import sys
import socket

# change the following for any particular use...
# for example, other machines, different ports for different languages, etc.
host, port = 'localhost', 10244

# Get the SPL from the command line ... would normally be created
# by a program of course...
spl = str(sys.argv[1])

# Make a normal stream socket ... (if there's an error just catch it and die)
try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.err, msg:
    sys.exit();

try:
    remote_ip = socket.gethostbyname( host )
except socket.gaierror:
    sys.exit()
     
s.connect((remote_ip , port))


# add a newline so that the input is complete and send it off
# to the hopefully waiting KPML server...
try:
    s.send(spl + "\n")
except socket.error:
    sys.exit()
    
# get the generated result back and print it out...
reply = s.recv(4096)

print (reply)
s.close()

