Network Protocol Python
<p>I am fairly new to Python and I am required to make a simple client and server which communicate through messages.</p>

<p>Currently, the system uses <code>.send</code> and <code>.recv</code> but I am supposed to update it so it only uses certain words like:</p>

<blockquote>
  <p><code>HELLO \n\r</code></p>
</blockquote>

<p>from client to server</p>

<p>then server replies back</p>

<blockquote>
  <p><code>GREETINGS \n\r</code></p>
</blockquote>

<p>Then a client is sending a number as 
GUESS </p>

<p>This is the info provided:</p>

<blockquote>
  <p>Your server and player clients will communicate using a standard protocol; a skeleton was introduced in lectures. Each message is terminated by \n\r, i.e., the bytes 0x0a and 0x0d</p>
</blockquote>

<p>Does anyone know how to do this?</p>

<p>As requested here is my present code, (please bear in mind that some variables are not needed and this is unfinished work)</p>

<p>Client side:</p>

<pre><code>gameCompleted=False
once = True

#Validate User Input
def valInput(userGuess):
    if(userGuess==''):
        print "Invalid Input. Please enter a number between 1 and 20."
        getGuess()
    elif userGuess.isdigit()==False:
        print "Invalid Input. Please enter a number between 1 and 20."
        getGuess()


#Get User Guess
def getGuess():
    global guess
    guess=raw_input("What is your guess?")
    valInput(guess)      



import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1", 8888))
while True:
    if once:
        print "Welcome to the Number Guessing Game!"
        once=False
    getGuess()
    s.send(guess)
    print s.recv(80)
</code></pre>

<p>SERVER side:</p>

<pre><code>import select
import socket
import random

#Global Variables
clientInfo={}
score=0

def storeClientInfo(client_socket,address):
    clientInfo[client_socket]=address

def genRand():
    global randomNo
    randomNo = random.randint(1, 20)


def processUserGuess(msg):
    guess=int(msg)
    global score
    score=score+1
    global randomNo
    if (within(guess,randomNo,0)):
        global gameCompleted
        gameCompleted=True
        return "You guessed correctly! \nYour score is: " + str(score)

    elif(within(guess,randomNo,2)):
        return "You are less than 3 away!"
    else:
        return "you are way off!"

def within(value, goal, n):
    proximity=abs(goal-value)
    if(proximity&lt;=n):
        return True
    else:
        return False



server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(('', 8888))
server_socket.listen(5)
print "Listening on port 8888"
genRand()
read_list = [server_socket]
while True:
    readable, writable, errored = select.select(read_list, [], [])
    for s in readable:
        if s is server_socket:
            client_socket, address = server_socket.accept()
            read_list.append(client_socket)
            print "Connection from", address
            storeClientInfo(client_socket,address)
        else:
            data = s.recv(1024)
            if data:
                print data, "from ", clientInfo[s]

                s.send(processUserGuess(data))
            else:
                s.close()
                read_list.remove(s)
</code></pre>
