Skip to content

Rhaptos Software Development

Personal tools
You are here: Home » Development » Tuesday Python Challenge » Challenge #7: Take a REST

Challenge #7: Take a REST

Document Actions
REST is "Representational State Transfer" and is sort of an evolved way of creating web services. It doesn't have all the defining overhead of things like SOAP or even XML-RPC, but builds an API much like those found already on the web. We can easily access RESTful resources in Python.
REST essentially asks us to get data from the application via URLs, returning it in a machine-readable (and useful) format. We already use URLs to access information: http://cnx.org/content/m10000 will get you information about module 10000, and  will get info about http://cnx.org/content/m10001 will get you information about module 10001. If we had a template for metadata about the object we might ask, as a web service, for http://cnx.org/content/m10000/meta and get some XML or a flat file or something to read.

RSS and OAI are examples of a RESTful interface as well.

This is easy to do in Python. I have created a file challenge7.csv that you can fetch from this web server. Write a Python function that will grab this resource (a comma-separated-value file) and turn it into a list of lists. You may use any of the standard Python libraries.

See discussions of REST at http://www.xfront.com/REST-Web-Services.html and elsewhere.
Created by jccooper
Last modified 2006-08-28 19:13

Solutions here

Posted by jccooper at 2006-08-28 19:10
Comment in reply to this to avoid spoilers.

Re: Solutions here

Posted by cbearden at 2006-08-30 09:24
#-- No indentation, so I can put my answer here without format loss

import urllib
import csv
uri = 'http://rhaptos.org/development/monday/challenge7.csv'

f = urllib.urlopen(uri)
r = csv.reader(f)
## print [ [ i.strip() for i in l ] for l in r ]
print map(lambda l: map(lambda i: i.strip(), l), r)

f.close()

Re: Solutions here

Posted by jenn at 2006-08-30 17:35
http://rhaptos.org/Members/jenn/pychallenges/pychallenge7

Though here's the text since, as Chuck points out, the formatting isn't crucial on this one.

----------------------------------

So, I've never really grokked REST. Whenever I read about it, it seems like the same stuff I've been doing since, oh, the mid-nineties. Is it just a way to describe the web so that client-server programmers will get it, or what? To describe the way the web already works, so we make sure to keep doing things that way even as the tools get shinier and more complex? I'm certainly not against naming things retroactively, if it helps to clarify them, but it always makes me worry that I'm missing some major point.

Anyhoo, here's my answer to the challenge. It's done in the "Jenn Using Python to Slurp Stuff" style of architecture, which I employ frequently in my non-Selenium-based automated testing. If there's anything in it that's *anti*-REST, I'd like to know -- despite my flippancy, I don't claim to know everything about how the web should work, and certainly not from a formal CS perspective!

#!/usr/bin/python
import httplib

conn = httplib.HTTPConnection('rhaptos.org')
conn.request("GET","/development/monday/challenge7.csv")
respfile = conn.getresponse().read()
conn.close()

result=[y.split(', ') for y in respfile.split('\n')]

This does result in some slightly overescaped strings, since the single ticks were already in the csv file. If I were less busy with other stuff I would throw an extra loop or two into the result to get rid of the extra quotes. But I think this handles the letter of the request, to generate a list of lists from the given file.

A final note: when I first started doing this sort of thing in Python, I had to learn it from the ground up from the module documentation, and noticed that help(httplib) gives you quite a nice overview of the HTTP request and response process. That's something that I still need occasionally, despite *using* HTTP for a long time and even sometimes doing hackish things to it.

A final final note, now that I've seen Chuck's solution: I didn't even think about urllib, though of course that's the right answer. I usually need httplib since I'm more interested in return codes and other http-level things than in the actual content of the retrieved object!