title: Python socket server/client programming
body: <p>So I am just getting into python and trying out some stuff. To start, I am making a server that does simple stuff like "GET"s stored text, "STORE"s new text over the old stored text, and "TRANSLATE"s lowercase text into uppercase. But I have a few questions. Here is my code so far:</p>
  
  <pre><code>import socket
  
  HOST = ''   # Symbolic name meaning the local host
  PORT = 24069    # Arbitrary non-privileged port
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  print 'Socket created'
  try:
      s.bind((HOST, PORT))
  except socket.error , msg:
      print 'Bind failed. Error code: ' + str(msg[0]) + 'Error message: ' + msg[1]
      sys.exit()
  print 'Socket bind complete'
  s.listen(1)
  print 'Socket now listening'
  while 1:
      conn, addr = s.accept()
      print 'Connected with ' + addr[0] + ':' + str(addr[1])
      data = conn.recv(1024)
      reply = 'OK...' + data
      if not data: break
      conn.send(data)
  conn.close()
  s.close()
  </code></pre>
  
  <p>To start changing text from a client into uppercase, from my other programming knowledge, I assume I'd store the client's text in a variable and then run a function on it to change it to uppercase. Is there such a function in python? Could someone please give me a snippet of how this would look?</p>
  
  <p>And lastly, how would I do something like a GET or STORE in python? My best guess would be:</p>
  
  <pre><code>data = conn.recv(1024)
  if data == GET: print text
  if data == STORE: text = data #Not sure how to reference the text that the client has entered
  </code></pre>
  
  <p>Thank you so much for any help! :)</p>
  
  <p>Note to self:</p>
  
  <pre><code>import socket
  
  HOST = ''   # Symbolic name meaning the local host
  PORT = 24069    # Arbitrary non-privileged port
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  print 'Socket created'
  try:
      s.bind((HOST, PORT))
  except socket.error , msg:
      print 'Bind failed. Error code: ' + str(msg[0]) + 'Error message: ' + msg[1]
      sys.exit()
  print 'Socket bind complete'
  s.listen(1)
  print 'Socket now listening'
  
  # Accept the connection
  (conn, addr) = s.accept()
  print 'Server: got connection from client ' + addr[0] + ':' + str(addr[1])
  storedText = 'Hiya!'
  while 1:
      data = conn.recv(1024)
      tokens = data.split(' ', 1)
      command = tokens[0]
      if command == 'GET':
          print addr[0] + ':' + str(addr[1]) + ' sends GET'
          reply = storedText
      elif command == 'STORE':
          print addr[0] + ':' + str(addr[1]) + ' sends STORE'  
          storedText = tokens[0]
          reply = '200 OK\n' + storedText
      elif command == 'TRANSLATE':
          print addr[0] + ':' + str(addr[1]) + ' sends TRANSLATE'
          storedText = storedText.upper()
          reply = storedText
      elif command == 'EXIT':
          print addr[0] + ':' + str(addr[1]) + ' sends EXIT'
          conn.send('200 OK')
          break
      else:
          reply = '400 Command not valid.'
  
      # Send reply
      conn.send(reply)
  conn.close()
  s.close()
  </code></pre>