I am a noob in Python. Just installed it, and spent 2 hours googleing how to get to a simple parameter sent in the URL to a Python script
Found this
Very helpful, except I cannot for anything in the world to figure out how to replace
import urlparse
url = 'http://foo.appspot.com/abc?def=ghi'
parsed = urlparse.urlparse(url)
print urlparse.parse_qs(parsed.query)['def']
With what do I replace url = ‘string’ to make it work?
I just want to access http://site.com/test/test.py?param=abc and see abc printed.
Final code after Alex’s answer:
url = os.environ["REQUEST_URI"]
parsed = urlparse.urlparse(url)
print urlparse.parse_qs(parsed.query)['param']
If you don’t have any libraries to do this for you, you can construct your current URL from the HTTP request that gets sent to your script via the browser.
The headers that interest you are
Hostand whatever’s after the HTTP method (probablyGET, in your case). Here are some more explanations (first link that seemed ok, you’re free to Google some more :).This answer shows you how to get the headers in your CGI script:
If you’re doing this as an exercise, then it’s fine because you’ll get to understand what’s behind the scenes. If you’re building anything reusable, I recommend you use libraries or a framework so you don’t reinvent the wheel every time you need something.