The generic way to do stuff like this is to
use CGI. That module contains everything you need to read and parse CGI parameters. It also contains the function which returns the details of the server and the URL of the script.
Look for url and self_url in the CGI docs.