This article will guide you in the creation of a simple webservice with Python. This is really simple, you just have to create a folder, and create the server.py and the client.py files and run it in the terminal.
This code is intended to run on Python 2.6+.
The first step is create the server. So create a folder with the name you want and create a file for the server. The server will use the library SimpleXMLRPCServer.
The following code refers to the server.py file.
#-*- encoding: iso-8859-1 -*- import SimpleXMLRPCServer db = 'users.txt' passwds = 'passwds.txt' def login(theUser, password): try: rowUser = open(db,'r').read() rowPasswds = open(passwds, 'r').read() except: # Return False in case it cant open the file return False # make the validation. for row in rowUser.split('\n'): for rowPasswd in rowPasswds.split('\n'): user = row passwd = rowPasswd if user == theUser and passwd == password: return True return False def register(theUser, password): userExists = False if login(theUser, password): return False try: rowUser = open(db,'r').read() # fetch for the username in the file for row in rowUser.split('\n'): if row == theUser: userExists = True break if not userExists: connection = open(db,'a') connection2 = open(passwds,'a') connection.write('%s\n'%theUser) connection2.write('\n%s'%password) connection.close() connection2.close() return True else: return 'user_exists' except IOError as e: # if we reach this part, it means the file doesn't exist yet, # so we may proceed with the registration connection = open(db,'a') connection2 = open(passwds,'a') connection.write('%s\n'%theUser) connection2.write('\n%s'%password) connection.close() connection2.close() server = SimpleXMLRPCServer.SimpleXMLRPCServer(('localhost',8082), allow_none=True) server.register_function(login) server.register_function(register) server.serve_forever()
This is the file for the server. In this case the data will be stored in a txt file. So the usernames will be stored in a file named users.txt and the passwords in a file named passwds.txt.
Now for the client, create a file name client.py. For the client, the library xmlrpclib will be used.
The following code refers to the client.py file.
#-*- encoding: iso-8859-1 -*- import xmlrpclib, getpass, hashlib client = xmlrpclib.ServerProxy('http://localhost:8082') user = raw_input('User: ') passwd = hashlib.sha256(getpass.getpass('Password: ')).hexdigest() if client.login(user, passwd): print 'You are logged.' else: print 'The user doesnt exists. Trying to register..' # make a call to try to register the user result = client.register(user, passwd) if result == 'user_exists': print 'Oops..username already exists.' else: print 'You are now registered. please login.'
So, in the client, it will assign two variables, user and passwd with data coming from an input, by using getpass.getpass(). getpass() is a very good function, since it prompts the user for a password without echoing. As you can see, we are also hashing the password to sha256.. And after that it just makes the authentication, if the login function from the server file returns true, the user is logged, else it calls the register method also from the server file.
In the register method it will open the respective files, check if the username already exists and if not it will write the data on it. And when the login function is called, it reads this files searching for the data that matches with the username and password.
A brief observation here. This validation method is just an example, it's not the best approach, since we are not using tables, and the user is not being referenced by id, no matter which password you type, you will always login, as long as the user exists. The main intention of this post is to show the communication between a server and client structure as a webservice, not the best authentication method.
Now, you should try running your application. First of all, open the terminal/command line, access the root of your directory and run the server.py file, after that, run the client.py file. You can run the files by typing:
Make sure to call server.py first.
Another issue you must pay attention is, when you create a server instance with
SimpleXMLRPCServer.SimpleXMLRPCServer(('localhost',8082) you set the address with a port which will be used by the client to try to establish the connection. This address must be the same in the
client.py file or you will get a
socket.error: [Errno 111] Connection refused error.
The .txt files presented here are merely illustrative for a database approach. Instead of using files to store the data, you could create a connection to a database and store these data on tables, and access it to retrieve the user information.
Here is an example of the application running in terminal. As you can see, the server is running in a terminal and the client application is running in another terminal. First, I try to login, but I’m not registered yet, so the application registers the user. And in the next try, the login happens.
Check your .txt files so you can see if the user and the password are being stored in these files.
Please, let me know if you have doubts. Feel free to comment and share. Also if you would like to contribute, click the Flattr button on top of this post.