SS 06: Simple chat client

DISCLAIMER: This is actually part of my uni labwork, but the deadline has passed, so I hope it’s alright to post this.

The task was to create a spectacularly simple chat client for two users who send can send one message and can only write another when they get a reply.

Here is my python code, if you have any suggestions for improvement, please leave a comment, this is only my second python program, so it’s unlikely this is the best way to do this.

"""

Server proxy module, for providing local functions that execute the three remote
functions of IMServer.php. That is: SET, GET and UNSET.

"""

import time

# Import URL library
import urllib2
from urllib import quote as enc

class IMServerProxy:

  def __init__(self, url):
    self.url = url

  def __getitem__(self, key):
    return urllib2.urlopen('%s?action=get&key=%s'%(self.url, enc(key))).read()

  def __setitem__(self, key, value):
    urllib2.urlopen('%s?action=set&key=%s&value=%s'%(self.url, enc(key), enc(value)))

  def __delitem__(self, key):
    urllib2.urlopen('%s?action=unset&key=%s'%(self.url, enc(key)))

server= IMServerProxy('http://soba.cs.man.ac.uk/~whelans1/IMServer.php') 

if server['FirstUserConnected'] != "True":
  server['FirstUserConnected'] = "True"
  user = 'WritingUser'
else:
  user = 'ReadingUser'

while True:

  if user == 'WritingUser':
    if server['UserWriting'] == 'False':
      if server['Message'] == '':
        message = raw_input('Type your message: ')
        server['Message'] = message
        server['UserWriting'] == 'True'
    time.sleep(3)
        user = 'ReadingUser'

  if user == 'ReadingUser':
    server['FirstUserConnected'] = "False"
    while server['Message'] != '':
      print server['Message']
      server['Message'] = ''
      server['UserWriting'] = "False"
      user = 'WritingUser'
Advertisements