Introduction
In this exercise you will learn how to use the Python language to webservices that can be used in your web sites. You will also learn how this can be used to make spatial data available as a geo-webservice. In particular you will:
- Define simple services on the webserver using Python CGI;
- Create an simple interface to this service, using the standard CGI parameters;
- Create a REST interface to the same service, using the rewriting capabilities of the server.
NOTE that this exercise is meant as a follow-up to the exercises Web Programming - Short Introduction and Introduction to server-side Python. This description is written supposing you have successfully finished those earlier exercises...!
You should remember from the theory as well as earlier exercises, that the HTTP
standard defines methods (or verbs) to indicate the
desired action to be performed on the identified resource. The HTTP/1.0 specification defined the GET, POST
and HEAD methods and the in later versions added other methods: PUT, PATCH, DELETE, TRACE,CONNECT and OPTIONS.
In our earlier exercise we used only the 2 most common verbs: GET and POST.
- GET is used to request data from a specified resource
- POST is used to submit data to be processed to a specified resource
GET request consists of query strings (name/value pairs) sent in the URL of a request. For instance:
https://gisedu.itc.utwente.nl/student/s1234567/helloworld.py?name=myName&age=23
These name/value pairs are sent in the URL, therefore GET requests should never be used to send sensitive data! GET requests have length restrictions and should be used only to retrieve data. Contrary to GET requests, the query strings (name/value pairs) of a POST request are sent in the HTTP message body. Also, POST requests have no restrictions on data length.
Python has a standard module called CGI
defining some utilities to be used by CGI scripts written
in Python. In particular, we can use the CGI module to read the query strings of GET and POST requests.
Parameters passed to the script via an HTTP POST or GET request are stored in the QUERY_STRING environment
variable before the script is called. The script can use the FieldStorage()
class to read these
environment variables and use them to prepare a response.
In the theory parts of this course, we have learned that "Webservices are loosely coupled, contracted components that communicate via standardised interfaces [after Schmelzer, 2002]." The service is made available on the WWW as a resource:
- Resources are identified by a URL (Uniform Resource Locator). A physical URL points at something that can actually be loaded or executed, for instance a web page or
a Python script:
../student/s1234567/create_geosjon.py
- Resources are returned in many formats, such as HTML, JSON, XML, SVG, etc. Clients can specify the format they wish.
- Client requests and server responses are self-descriptive messages. Each request/response cycle is stateless, that is, a new request/response is independent from, and ignorant of, previous ones.