Quantcast
submit to reddit       
       

Sending data to an HTTP server - get and post methods

There are two different methods for passing variables to a CGI script, the get method and the post method.

The Get Method

The get method is used for ordinary requests to get pages from a server, and it can also have arguments appended to it. The usual format is

somedomain.com/somepage?arg1=value1&arg2=value2

The data that's being passed to the script is placed held in an environment variable named QUERY_STRING. The question mark separates the url from the data.

Clicking on this link will generate a get request with several arguments attached:

http://raspberrywebserver.com/cgi-bin/examples/getmethod.py?os=linux&cpu=arm11&server=apache2

This python code reads the arguments from the query string and prints them out:

#!/usr/bin/env python import os # print header print "Content-type: text/html\n\n" query_string=os.environ["QUERY_STRING"] print "<h2>Query string</h2>" print "query_string: "+query_string+"<br>" print "<h2>Argument list</h2>" arg_list=query_string.split('&') #print arg_list i=1 for arg in arg_list: key, value=arg.split('=') print "key "+str(i)+": "+key+"<br>" print"value "+str(i)+": "+value+"<br>" i +=1

Don't pass sensitive data like passwords using the get method.

The Post Method

When the post method is used, the browser sends a URL request first and then the data is sent seperately. CGI scripts running on the server process the URL request, and then the data is read from stdin. The post method is often use with forms.

The following form uses several hidden fields and a submit button:

<form action="/cgi-bin/examples/postmethod.py" method="POST"> <input type="hidden" name="os" value="linux"> <input type="hidden" name="cpu" value="ARM11"> <input type="hidden" name="server" value="apache2"> <input type="submit" value="Submit"> </form>

When the submit button is clicked on, this python code executes:

#!/usr/bin/env python import sys args=sys.stdin.read() # print header print "Content-type: text/html\n\n" print "<h2>Arguments</h2>" arg_list=args.split('&') i=1 for arg in arg_list: key, value=arg.split('=') print "key "+str(i)+": "+key+"<br>" print"value "+str(i)+": "+value+"<br>" i +=1

Using the Python CGI module

Python has a CGI API which you can use to access arguments in the same way regardless of whether the request is a post or get. The following script is a simple example of how to use it:

#!/usr/bin/env python import cgi # print header print "Content-type: text/html\n\n" print "<h2>Arguments</h2>" form = cgi.FieldStorage() arg1 = form.getvalue('os') print "OS: " +arg1+"<br>" arg2 = form.getvalue('cpu') print "CPU: "+arg2+"<br>" arg3 = form.getvalue('server') print "Server: "+arg3+"<br>"

The call to cgi.FieldStorage() determines what type of request needs to be processed, and then converts the arguments into an easily accessible form. This script can be used to process get and post requests. Click on the link below to invoke the script using a get request, and click on the submit button to invoke the script using a post request.

http://raspberrywebserver.com/cgi-bin/examples/cgiform.py?os=linux&cpu=arm11&server=apache2
<form action="/cgi-bin/examples/cgiform.py" method="POST"> <input type="hidden" name="os" value="linux"> <input type="hidden" name="cpu" value="ARM11"> <input type="hidden" name="server" value="apache2"> <input type="submit" value="Submit"> </form>

Comments

Comments

comments powered by Disqus



Follow me


This site is powered by Pyplate, a lightweight Python CMS for the Raspberry Pi.