[Twisted-Python] web/client.py add parameter to get raw redirects

Clark C. Evans cce at clarkevans.com
Mon Nov 24 01:52:13 MST 2003


Hello.  I'm working on some testing harness for my application
(in twisted, of course) and I found I needed to record when 
the app is expected to send back a redirect.  So, this needed
a few changes in client.py ; if this seems resonable, I can
commit it.  - Clark

...

--- client.py.bc	Sun Nov 23 23:45:10 2003
+++ client.py	Mon Nov 24 03:46:55 2003
@@ -28,10 +28,19 @@
 from twisted.web import error
 import urlparse, os, types
 
+from twisted.internet.error import ConnectionDone
+class PageRedirect(ConnectionDone):
+    """A request resulted in a redirect (followRedirect = 0) """
+    def __init__(self, status, location):
+        Exception.__init__(self, 
+          "%s redirection to: %s" % (status, location))
+        self.status = status
+        self.location = location
+
 class HTTPPageGetter(http.HTTPClient):
 
     quietLoss = 0
-    
+    followRedirect = 1 
     failed = 0
 
     def connectionMade(self):
@@ -79,6 +88,10 @@
         if not l:
             self.handleStatusDefault()
         url = l[0]
+        if not self.followRedirect:
+            self.handleStatusDefault()
+            self.factory.noPage(PageRedirect(self.status,url))
+            return
         scheme, host, port, path = _parse(url, defaultPort=self.transport.addr[1])
         self.factory.setURL(url)
 
@@ -173,7 +186,9 @@
     path = None
 
     def __init__(self, url, method='GET', postdata=None, headers=None,
-                 agent="Twisted PageGetter", timeout=0, cookies=None):
+                 agent="Twisted PageGetter", timeout=0, cookies=None,
+                 followRedirect=1):
+        self.protocol.followRedirect = followRedirect
         self.timeout = timeout
         self.agent = agent
 




More information about the Twisted-Python mailing list