#!/usr/bin/env python # this script runs a client for megaserver and just chats with it # # CTRL+C is trapped and triggers: # * TCP socket closure # * megahal state staving # * client exit import mh_python import signal import socket import string import sys import time host = 'homeserver' port = 8112 size = 1024 greeting = 'buongiorno caro' def signal_handler(signal, frame): mh_python.cleanup() s.close() sys.exit(0) signal.signal(signal.SIGINT, signal_handler) def generate_sentence(reply): "Return a string containing a reply to input sentence." reply = mh_python.doreply(sentence) time.sleep(4) return reply mh_python.initbrain() first_talk = 1 while 1: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host,port)) if first_talk == 1: sentence = greeting first_talk = 0 print 'Io:', sentence s.send(sentence) reply = s.recv(size) s.close() print 'Lui:', reply sentence = generate_sentence(reply)