aboutsummaryrefslogtreecommitdiffstats
path: root/toys/brhute-py/twisted_http_persistent.py
diff options
context:
space:
mode:
Diffstat (limited to 'toys/brhute-py/twisted_http_persistent.py')
-rw-r--r--toys/brhute-py/twisted_http_persistent.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/toys/brhute-py/twisted_http_persistent.py b/toys/brhute-py/twisted_http_persistent.py
new file mode 100644
index 0000000..333bcb3
--- /dev/null
+++ b/toys/brhute-py/twisted_http_persistent.py
@@ -0,0 +1,39 @@
+from twisted.internet import reactor
+from twisted.internet.defer import Deferred, DeferredList
+from twisted.internet.protocol import Protocol
+from twisted.web.client import Agent, HTTPConnectionPool
+
+class IgnoreBody(Protocol):
+ def __init__(self, deferred):
+ self.deferred = deferred
+
+ def dataReceived(self, bytes):
+ pass
+
+ def connectionLost(self, reason):
+ self.deferred.callback(None)
+
+
+def cbRequest(response):
+ print 'Response code:', response.code
+ finished = Deferred()
+ response.deliverBody(IgnoreBody(finished))
+ return finished
+
+pool = HTTPConnectionPool(reactor)
+agent = Agent(reactor, pool=pool)
+
+def requestGet(url):
+ d = agent.request('GET', url)
+ d.addCallback(cbRequest)
+ return d
+
+# Two requests to the same host:
+d = requestGet('http://google.com/titi').addCallback(
+ lambda ign: requestGet("http://google.com/tata"))
+def cbShutdown(ignored):
+ reactor.stop()
+d.addCallback(cbShutdown)
+
+reactor.run()
+