Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to run an Icecast stream using a simple Python script to pick a random song from the list of songs on the server. I'm looking to add a voting/request interface, and my host allows use of python to serve webpages through CGI. However, I'm getting hung up on just how to get the GET arguments supplied by the user. I've tried the usual way with sys.argv:

#!/usr/bin/python
import sys
print "Content-type: text/html\n\n"
print sys.argv

But hitting up http://example.com/index.py?abc=123&xyz=987 only returns "['index.py']". Is there some other function Python has for this purpose, or is there something I need to change with CGI? Is what I'm trying to do even possible?

Thanks.

share|improve this question

1 Answer

up vote 9 down vote accepted

cgi.FieldStorage() should do the trick for you... it returns a dictionary with key as the field and value as it's value.

import cgi
import cgitb; cgitb.enable() # Optional; for debugging only

print "Content-Type: text/html"
print ""

arguments = cgi.FieldStorage()
for i in arguments.keys():
 print arguments[i].value
share|improve this answer
Thanks lalli, exactly what I needed. – James Aug 27 '10 at 9:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.