Python Web Service Example


I was testing a client software which communicates with a server software using JSON format. The behavior of the client depends on the instructions specified in the server response. In order to test the client I had to modify the server response and try different scenarios. One way to change the server response is to use the server web interface. This works but it is slow and inconvenient. Another way is to install a web proxy such as Charles and intercept HTTP requests, modify the request on the fly then pass back to client. This also works fine but it is not convenient as well. In this short article I will describe an easy and convenient way to achieve that goal.


1. Install module using the following command...

sudo easy_install

2. Below is the server code that uses

#!/usr/bin/env python
import web

# This is the mapping between the service URL end point 
# and the corresponding method that implements the request
urls = (
    '/api/v1/requestA', 'post_A',
    '/api/v1/requestB', 'post_B'

# Server app using
app = web.application(urls, globals())

# First request mock. We are simply reading the request 
# content from file. You can hard code it if you want
# or you can change it programmatically depending on
# your needs
class post_A:        
    def POST(self):
    	return open('A.json', 'r').read()
# Second mock
class post_B:        
    def POST(self):
        return open('B.json', 'r').read()

# Run forever
if __name__ == "__main__":

Leave a Reply

%d bloggers like this: