17

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']
3
  • Are you using a framework in particular? The frameworks will help abstract your problem Commented Jan 22, 2013 at 22:03
  • No framework, just simple .py scripts run in browser. Commented Jan 22, 2013 at 22:04
  • Final code after Alex's answer: code url = os.environ["REQUEST_URI"] parsed = urlparse.urlparse(url) print urlparse.parse_qs(parsed.query)['param'] code Commented Jan 22, 2013 at 22:50

2 Answers 2

12

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 Host and whatever's after the HTTP method (probably GET, 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 are running as a CGI, you can't read the HTTP header directly, but the web server put much of that information into environment variables for you. You can just pick it out of os.environ[].

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks so much, and you were right. Found what I was looking in os.environ["REQUEST_URI"].
You're welcome, glad to help. :) I've pasted your working solution from the comment in the question to make it more readable.
I would upvote this if there was an actual code example.
3

This is how I capture in Python 3 from CGI (A) URL, (B) GET parameters and (C) POST data:

=======================================================

import sys, os, io

CAPTURE URL

myDomainSelf = os.environ.get('SERVER_NAME')

myPathSelf = os.environ.get('PATH_INFO')

myURLSelf = myDomainSelf + myPathSelf

CAPTURE GET DATA

myQuerySelf = os.environ.get('QUERY_STRING')

CAPTURE POST DATA

myTotalBytesStr=(os.environ.get('HTTP_CONTENT_LENGTH'))

if (myTotalBytesStr == None):

myJSONStr = '{"error": {"value": true, "message": "No (post) data received"}}'

else:

myTotalBytes=int(os.environ.get('HTTP_CONTENT_LENGTH'))

myPostDataRaw = io.open(sys.stdin.fileno(),"rb").read(myTotalBytes)

myPostData = myPostDataRaw.decode("utf-8")

Write RAW to FILE

mySpy = "myURLSelf: [" + str(myURLSelf) + "]\n"

mySpy = mySpy + "myQuerySelf: [" + str(myQuerySelf) + "]\n"

mySpy = mySpy + "myPostData: [" + str(myPostData) + "]\n"

You need to define your own myPath here

myFilename = "spy.txt"

myFilePath = myPath + "\" + myFilename

myFile = open(myFilePath, "w")

myFile.write(mySpy)

myFile.close()

=======================================================

Here are some other useful CGI environment vars:

AUTH_TYPE

CONTENT_LENGTH

CONTENT_TYPE

GATEWAY_INTERFACE

PATH_INFO

PATH_TRANSLATED

QUERY_STRING

REMOTE_ADDR

REMOTE_HOST

REMOTE_IDENT

REMOTE_USER

REQUEST_METHOD

SCRIPT_NAME

SERVER_NAME

SERVER_PORT

SERVER_PROTOCOL

SERVER_SOFTWARE

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.