# Collection of code snippets by Arne Vajhøj 
# (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import socket

HOST = 'localhost'
PORT = 12345
# open connection
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
wf = s.makefile('w')
rf = s.makefile('r')
# send message
outmsg = 'Hi from Jython!\n' 
wf.write(outmsg)
wf.flush()
# receive all messages
while True:
    inmsg = rf.readline().rstrip()
    if not inmsg: break
    print('|' + inmsg + '|')
# close connection
wf.close()
rf.close()
s.close()
