cgiproxy.py :  » Blog » Frog » FrogComplete-1.8 » snakeserver » adapters » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Blog » Frog 
Frog » FrogComplete 1.8 » snakeserver » adapters » cgiproxy.py
#! /usr/bin/env python
#
#  CGI proxy script
#
#  Written by Irmen de Jong -- irmen@users.sourceforge.net
#
#  Usage:
#  This cgi script passes any GET or POST action that goes
#  trough it along to another URL (the proxied URL).
#  Results are passed back to the original user agent.
#  Tries hard to replicate HTTP headers, and also handles POST data.
#
#  You can configure the proxied location by editing
#  the PROXY_URL below.
#

import cgi,os,sys
import cgitb
import urllib
import shutil

# The proxied URL, all requests are passed to here:
# (including path info, query string, HTTP headers and POST data)
PROXY_URL = "http://isengard:9080/proxy.py"


cgitb.enable()

# determine the request URI (path including query args)
#requestURI = os.environ.get('REQUEST_URI')
#if not requestURI:
#scriptName = os.environ.get('SCRIPT_NAME')
pathInfo = os.environ.get('PATH_INFO')
queryString = os.environ.get('QUERY_STRING')
# requestURI=scriptName+pathInfo
requestURI=pathInfo
if queryString:
  requestURI+='?'+queryString

# extract HTTP headers
headers={}
headers['Accept']=os.environ.get("HTTP_ACCEPT")
headers['Range']=os.environ.get("HTTP_RANGE")
headers['Accept-Charset']=os.environ.get("HTTP_ACCEPT_CHARSET")
headers['Accept-Encoding']=os.environ.get("HTTP_ACCEPT_ENCODING")
headers['Accept-Language']=os.environ.get("HTTP_ACCEPT_LANGUAGE")
headers['User-Agent']=os.environ.get("HTTP_USER_AGENT")
headers['Cache-Control']=os.environ.get("HTTP_CACHE_CONTROL")
headers['Content-Type']=os.environ.get("CONTENT_TYPE")
headers['Pragma']=os.environ.get("HTTP_PRAGMA")
headers['Referer']=os.environ.get("HTTP_REFERER")
headers['Cookie']=os.environ.get("HTTP_COOKIE")
#headers['Content-Length']=os.environ.get("CONTENT_LENGTH")
#headers['Content-Disposition']=os.environ.get("CONTENT_DISPOSITION")

# read any POST data
if os.environ["REQUEST_METHOD"]=="POST":
  postdata=sys.stdin.read()
else:
  postdata=None

# open the target URL
urlopener=urllib.FancyURLopener()

# add original headers to target URL
for k in headers.keys():
  if headers[k] is not None:
    urlopener.addheader(k, headers[k])

# read the target URL, obtain HTTP reply headers
try:
  targetURL=PROXY_URL+requestURI
  if postdata:
    page=urlopener.open(targetURL, data=postdata)
  else:
    page=urlopener.open(targetURL)
except IOError,x:
  print "Content-Type: text/plain"
  print
  print "Something went wrong with getting the required page:",x[0]
  print "code:",x[1],x[2]
  print "target url:",targetURL
  print
  raise SystemExit

headers=dict(page.headers)

# find the content-type
contentType=headers.get('content-type') or headers.get('Content-Type') or headers.get('Content-type')
if not contentType:
  headers['Content-Type']='text/plain'

# output the result HTTP headers
for k in headers:
  sys.stdout.write( '%s: %s\n' % (k, headers[k]) )
sys.stdout.write('\n')

# copy the document body, chunk-wise
shutil.copyfileobj(page, sys.stdout)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.