Update of /cvsroot/pywin32/pywin32/isapi/samples
In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv4191/samples
Modified Files:
Tag: py3k
advanced.py redirector.py
Log Message:
first cut at 2to3-lovin'
Index: advanced.py
===================================================================
RCS file: /cvsroot/pywin32/pywin32/isapi/samples/advanced.py,v
retrieving revision 1.2.4.1
retrieving revision 1.2.4.2
diff -C2 -d -r1.2.4.1 -r1.2.4.2
*** advanced.py 26 Nov 2008 09:03:30 -0000 1.2.4.1
--- advanced.py 5 Jan 2009 12:49:52 -0000 1.2.4.2
***************
*** 5,8 ****
--- 5,16 ----
# .py implementation file changes.)
# * Custom command-line handling - both additional options and commands.
+ # * Using a query string - any part of the URL after a '?' is assumed to
+ # be "variable names" separated by '&' - we will print the values of
+ # these server variables.
+ # * If the tail portion of the URL is "ReportUnhealthy", IIS will be
+ # notified we are unhealthy via a HSE_REQ_REPORT_UNHEALTHY request.
+ # Whether this is acted upon depends on if the IIS health-checking
+ # tools are installed, but you should always see the reason written
+ # to the Windows event log - see the IIS documentation for more.
from isapi import isapicon
***************
*** 107,112 ****
--- 115,133 ----
raise InternalReloadException
+ url = ecb.GetServerVariable("URL")
+ if url.endswith("ReportUnhealthy"):
+ ecb.ReportUnhealthy("I'm a little sick")
+
ecb.SendResponseHeaders("200 OK", "Content-Type: text/html\r\n\r\n", 0)
print("<HTML><BODY>", file=ecb)
+
+ queries = ecb.GetServerVariable("QUERY_STRING").split("&")
+ if queries:
+ print("<PRE>", file=ecb)
+ for q in queries:
+ val = ecb.GetServerVariable(q)
+ print("%s=%r" % (q, val), file=ecb)
+ print("</PRE><P/>", file=ecb)
+
print("This module has been imported", file=ecb)
print("%d times" % (reload_counter,), file=ecb)
Index: redirector.py
===================================================================
RCS file: /cvsroot/pywin32/pywin32/isapi/samples/redirector.py,v
retrieving revision 1.2.4.1
retrieving revision 1.2.4.2
diff -C2 -d -r1.2.4.1 -r1.2.4.2
*** redirector.py 26 Nov 2008 09:03:30 -0000 1.2.4.1
--- redirector.py 5 Jan 2009 12:49:52 -0000 1.2.4.2
***************
*** 1,4 ****
! # This is a sample configuration file for an ISAPI filter and extension
! # written in Python.
#
# Please see README.txt in this directory, and specifically the
--- 1,3 ----
! # This is a sample ISAPI extension written in Python.
#
# Please see README.txt in this directory, and specifically the
***************
*** 10,36 ****
# this module and create your Extension and Filter objects.
! # This sample provides a very simple redirector.
! # It is implemented by a filter and an extension.
! # * The filter is installed globally, as all filters are.
! # * A Virtual Directory named "python" is setup. This dir has our ISAPI
! # extension as the only application, mapped to file-extension '*'. Thus, our
! # extension handles *all* requests in this directory.
! # The basic process is that the filter does URL rewriting, redirecting every
! # URL to our Virtual Directory. Our extension then handles this request,
! # forwarding the data from the proxied site.
! # So, the process is:
! # * URL of "index.html" comes in.
! # * Filter rewrites this to "/python/index.html"
! # * Our extension sees the full "/python/index.html", removes the leading
! # portion, and opens and forwards the remote URL.
!
! # This sample is very small - it avoid most error handling, etc. It is for
! # demonstration purposes only.
from isapi import isapicon, threaded_extension
- from isapi.simple import SimpleFilter
import sys
import traceback
! import urllib
# sys.isapidllhandle will exist when we are loaded by the IIS framework.
--- 9,25 ----
# this module and create your Extension and Filter objects.
! # This is the simplest possible redirector (or proxy) we can write. The
! # extension installs with a mask of '*' in the root of the site.
! # As an added bonus though, we optionally show how, on IIS6 and later, we
! # can use HSE_ERQ_EXEC_URL to ignore certain requests - in IIS5 and earlier
! # we can only do this with an ISAPI filter - see redirector_with_filter for
! # an example. If this sample is run on IIS5 or earlier it simply ignores
! # any excludes.
from isapi import isapicon, threaded_extension
import sys
import traceback
! import urllib.request, urllib.parse, urllib.error
! import win32api
# sys.isapidllhandle will exist when we are loaded by the IIS framework.
***************
*** 41,49 ****
# The site we are proxying.
proxy = "http://www.python.org"
- # The name of the virtual directory we install in, and redirect from.
- virtualdir = "/python"
! # The ISAPI extension - handles requests in our virtual dir, and sends the
! # response to the client.
class Extension(threaded_extension.ThreadPoolExtension):
"Python sample Extension"
--- 30,48 ----
# The site we are proxying.
proxy = "http://www.python.org"
! # Urls we exclude (ie, allow IIS to handle itself) - all are lowered,
! # and these entries exist by default on Vista...
! excludes = ["/iisstart.htm", "/welcome.png"]
!
! # An "io completion" function, called when ecb.ExecURL completes...
! def io_callback(ecb, url, cbIO, errcode):
! # Get the status of our ExecURL
! httpstatus, substatus, win32 = ecb.GetExecURLStatus()
! print("ExecURL of %r finished with http status %d.%d, win32 status %d (%s)" % (
! url, httpstatus, substatus, win32, win32api.FormatMessage(win32).strip()))
! # nothing more to do!
! ecb.DoneWithSession()
!
! # The ISAPI extension - handles all requests in the site.
class Extension(threaded_extension.ThreadPoolExtension):
"Python sample Extension"
***************
*** 54,122 ****
#print 'IIS dispatching "%s"' % (ecb.GetServerVariable("URL"),)
url = ecb.GetServerVariable("URL")
! if url.startswith(virtualdir):
! new_url = proxy + url[len(virtualdir):]
! print("Opening", new_url)
! fp = urllib.urlopen(new_url)
! headers = fp.info()
! ecb.SendResponseHeaders("200 OK", str(headers) + "\r\n", False)
! ecb.WriteClient(fp.read())
! ecb.DoneWithSession()
! print("Returned data from '%s'!" % (new_url,))
else:
! # this should never happen - we should only see requests that
! # start with our virtual directory name.
! print("Not proxying '%s'" % (url,))
!
!
! # The ISAPI filter.
! class Filter(SimpleFilter):
! "Sample Python Redirector"
! filter_flags = isapicon.SF_NOTIFY_PREPROC_HEADERS | \
! isapicon.SF_NOTIFY_ORDER_DEFAULT
!
! def HttpFilterProc(self, fc):
! #print "Filter Dispatch"
! nt = fc.NotificationType
! if nt != isapicon.SF_NOTIFY_PREPROC_HEADERS:
! return isapicon.SF_STATUS_REQ_NEXT_NOTIFICATION
! pp = fc.GetData()
! url = pp.GetHeader("url")
! #print "URL is '%s'" % (url,)
! prefix = virtualdir
! if not url.startswith(prefix):
! new_url = prefix + url
! print("New proxied URL is '%s'" % (new_url,))
! pp.SetHeader("url", new_url)
! # For the sake of demonstration, show how the FilterContext
! # attribute is used. It always starts out life as None, and
! # any assignments made are automatically decref'd by the
! # framework during a SF_NOTIFY_END_OF_NET_SESSION notification.
! if fc.FilterContext is None:
! fc.FilterContext = 0
! fc.FilterContext += 1
! print("This is request number", fc.FilterContext, "on this connection")
! return isapicon.SF_STATUS_REQ_HANDLED_NOTIFICATION
! else:
! print("Filter ignoring URL '%s'" % (url,))
!
! # Some older code that handled SF_NOTIFY_URL_MAP.
! #~ print "Have URL_MAP notify"
! #~ urlmap = fc.GetData()
! #~ print "URI is", urlmap.URL
! #~ print "Path is", urlmap.PhysicalPath
! #~ if urlmap.URL.startswith("/UC/"):
! #~ # Find the /UC/ in the physical path, and nuke it (except
! #~ # as the path is physical, it is \)
! #~ p = urlmap.PhysicalPath
! #~ pos = p.index("\\UC\\")
! #~ p = p[:pos] + p[pos+3:]
! #~ p = r"E:\src\pyisapi\webroot\PyTest\formTest.htm"
! #~ print "New path is", p
! #~ urlmap.PhysicalPath = p
# The entry points for the ISAPI extension.
- def __FilterFactory__():
- return Filter()
def __ExtensionFactory__():
return Extension()
--- 53,77 ----
#print 'IIS dispatching "%s"' % (ecb.GetServerVariable("URL"),)
url = ecb.GetServerVariable("URL")
! if ecb.Version < 0x60000:
! print("IIS5 or earlier - can't do 'excludes'")
else:
! for exclude in excludes:
! if url.lower().startswith(exclude):
! print("excluding %s" % url)
! ecb.IOCompletion(io_callback, url)
! ecb.ExecURL(None, None, None, None, None, isapicon.HSE_EXEC_URL_IGNORE_CURRENT_INTERCEPTOR)
! return isapicon.HSE_STATUS_PENDING
! new_url = proxy + url
! print("Opening %s" % new_url)
! fp = urllib.urlopen(new_url)
! headers = fp.info()
! ecb.SendResponseHeaders("200 OK", str(headers) + "\r\n", False)
! ecb.WriteClient(fp.read())
! ecb.DoneWithSession()
! print("Returned data from '%s'!" % (new_url,))
! return isapicon.HSE_STATUS_SUCCESS
# The entry points for the ISAPI extension.
def __ExtensionFactory__():
return Extension()
***************
*** 126,134 ****
from isapi.install import *
params = ISAPIParameters()
- # Setup all filters - these are global to the site.
- params.Filters = [
- FilterParameters(Name="PythonRedirector",
- Description=Filter.__doc__),
- ]
# Setup the virtual directories - this is a list of directories our
# extension uses - in this case only 1.
--- 81,84 ----
***************
*** 138,142 ****
ScriptMapParams(Extension="*", Flags=0)
]
! vd = VirtualDirParameters(Name=virtualdir[1:],
Description = Extension.__doc__,
ScriptMaps = sm,
--- 88,92 ----
ScriptMapParams(Extension="*", Flags=0)
]
! vd = VirtualDirParameters(Name="/",
Description = Extension.__doc__,
ScriptMaps = sm,
|