

import socket
import sys

# TCP/IP socket - there are lots of types here, let's not get lost atm
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

#set up my server to be on port 10000 on this "internet" adapter
#localhost referes to 127.0.0.1 or "HOME" or "this machine"
#it is an ip address
server_address = ('localhost', 10000)

#ask the OS if we're allowed to have this ip/port pair. 
sock.bind(server_address)

#wait for someone or something to connect!
# The 1 here says how many people are allowed to wait in line, like a queue
sock.listen(1)

while True:
    print ' (*) Waiting for a connection'
    
    #accept some incoming connection
    connection, client_address = sock.accept()
    try:
        print '    (*) Received connection from ', client_address
        #let's get up to 16 bytes from the connector
        data = connection.recv(16)
        print '    (*) Received message "%s"' % data
        if data:
            print '    (*) Sending message back to the client '
            if data == "WORD":
                connection.sendall("DIFFERENT")
            elif data == "AXO":
                connection.sendall("BURGUNDY")               
            elif data[:3] == "ADD":
                connection.sendall(data[3:])
             # ADD 1 2
            else:
                connection.sendall("WRONG")
        else:
            print '    (*) No more data from ', client_address
            break
    finally:
        connection.close()
