#!/usr/bin/env python """ By Mike Urbach, 2011. A TCP client specifically designed for communicating with the DND. Special thanks to David Gelhar and his 1998 document, "Protocol Specification: DND Server Interface," located at: http://basement.dartmouth.edu/protocols/DND-protocol.pdf. Syntax example for a morning after name and phone number lookup: LOOKUP jessica w 14, name phone Full examples and protocol specifications in David Gelhar's documentation""" from socket import * # connection info TCP_IP = "129.170.208.9" TCP_PORT = 902 BUFFER_SIZE = 4096 # create a socket and connect, printing initial response socket = socket(AF_INET, SOCK_STREAM) socket.connect((TCP_IP, TCP_PORT)) data = socket.recv(BUFFER_SIZE) print "<<< " + data # main loop for query/response exchange message = "" while(message != "QUIT\r\n"): message += raw_input(">>> ") message += "\r\n" # have to append this for protocol to work socket.send(message) data = socket.recv(BUFFER_SIZE) # this is a little hack to make sure the last line # doesn't start with a 1 -- that would indicate it is not # the last response, so we keep receiving. simply # increasing the buffer size didn't seem to solve this while(data.splitlines()[-1].startswith("1")): data += s.recv(BUFFER_SIZE) print data message = ""