# L-37 MCS 275 Wed 14 Apr 2010 : pass_server.py # The server receives login name. namespass = {'A':'1','B':'2'} from socket import * hostname = '' # blank for any address number = 11267 # number for the port buffer = 80 # size of the buffer def connect(): """ Connects and returns server socket. """ server_address = (hostname, number) server = socket(AF_INET, SOCK_STREAM) server.bind(server_address) server.listen(1) return server def serve(server): """ Accepts connection from a client and receives a login name. Checks if the name is a key in namespass and sends a reply to the client. If the name is a key, then the password sent by the client is checked and a reply is send back. """ client, client_address = server.accept() name = client.recv(buffer) print 'received \"' + name + '\"' if not namespass.has_key(name): print 'access is denied' client.send('access denied') else: client.send('send password') pasw = client.recv(buffer) print 'received \"' + pasw + '\"' if namespass[name] == pasw: print 'access is granted' client.send('access granted') else: print 'access is denied' client.send('access denied') def main(): """ Simulates a simple password interaction. """ server = connect() print 'waiting for connections' serve(server) print 'closing the server' server.close() if __name__=="__main__": main()