Handling URLs within a CGI script

Submitted by:Jhon Smith

Date added:25 June, 2012

Category:Python

The following are some useful functions to build URLs within a CGI script, for example to send a HTTP redirection header

Tags: http redirection , cgi script

Code Snippet:

import os, string

def isSSL():
""" Return true if we are on a SSL (https) connection. """
return os.environ.get('SSL_PROTOCOL', '') != ''

def getScriptname():
""" Return the scriptname part of the URL ("/path/to/my.cgi"). """
return os.environ.get('SCRIPT_NAME', '')

def getPathinfo():
""" Return the remaining part of the URL. """
pathinfo = os.environ.get('PATH_INFO', '')

# Fix for bug in IIS/4.0
if os.name == 'nt':
scriptname = getScriptname()
if string.find(pathinfo, scriptname) == 0:
pathinfo = pathinfo[len(scriptname):]

return pathinfo

def getQualifiedURL(uri = None):
""" Return a full URL starting with schema, servername and port.

*uri* -- append this server-rooted uri (must start with a slash)
"""
schema, stdport = (('http', '80'), ('https', '443'))[isSSL()]
host = getEnvValue('HTTP_HOST')
if not host:
host = getEnvValue('SERVER_NAME')
port = getEnvValue('SERVER_PORT', '80')
if port != stdport: host = host + ":" + port

result = "%s://%s" % (schema, host)
if uri: result = result + uri

return result

def getBaseURL():
""" Return a fully qualified URL to this script. """
return getQualifiedURL(getScriptname())

# example (redirect)
print "Location:", getQualifiedURL("/go/here")
 
 

Comments