I have some progress on the request timeout.<div><br></div><div>I have something that seems to work. But I am sure I have got something wrong..</div><div><br></div><div>This is a copy of the response.py example with some additions to deal with request timeouts.</div>
<div><br></div><div>Any chance that someone could take a look and let me know where I have gone wrong.</div><div><br></div><div><div>#!/usr/bin/env python</div><div># -*- coding: UTF-8 -*-</div><div>from pprint import pformat</div>
<div><br></div><div>from twisted.internet import reactor</div><div>from twisted.internet.defer import Deferred</div><div>from twisted.internet.protocol import Protocol</div><div>from twisted.web.client import Agent</div><div>
from twisted.web.http_headers import Headers</div><div>import urllib</div><div>import time</div><div><br></div><div>class BeginningPrinter(Protocol):</div><div>    def __init__(self, finished):</div><div>        self.finished = finished</div>
<div>        self.remaining = 1024 * 10</div><div><br></div><div>    def dataReceived(self, bytes):</div><div>        if self.remaining:</div><div>            display = bytes[:self.remaining]</div><div>            print &#39;Some data received:&#39;, bytes, &#39;no bytes&#39;, len(bytes)</div>
<div>            print display</div><div>            self.remaining -= len(display)</div><div><br></div><div>    def connectionLost(self, reason):</div><div>        print &#39;Finished receiving body:&#39;, reason.getErrorMessage()</div>
<div>        self.finished.callback(None)</div><div><br></div><div>def do_cancel(d):</div><div>    print &#39;got do cancel&#39;</div><div>    d._canceller(d)</div><div><br></div><div><br></div><div>from stringprod_resp import StringProducer</div>
<div>my_data = [&#39;fred&#39;,&#39;ted&#39;,&#39;red&#39;,&#39;fred&#39;]</div><div>for c, a in enumerate(my_data):</div><div>    if c &gt; 2:</div><div>        time.sleep(10)</div><div>    agent = Agent(reactor)</div><div>
    #postdata = urllib.urlencode({&#39;Username&#39; : &#39;nsp05682&#39;,</div><div>    #                             &#39;Password&#39; : &#39;rocs345&#39;,</div><div>    #                             &#39;Action&#39; : &#39;CheckCredits&#39;})</div>
<div>    postdata = urllib.urlencode({&#39;the-field&#39; : a})</div><div>    body = StringProducer(postdata)</div><div>    print &#39;body&#39;, body</div><div>    d = agent.request(</div><div>        &#39;POST&#39;,</div>
<div>        &#39;<a href="http://www.johna.com/test">http://www.johna.com/test</a>&#39;,</div><div>        Headers({&#39;User-Agent&#39;: [&#39;Twisted Web Client&#39;],</div><div>                 &#39;Accept-Encoding&#39;:[&#39;identity&#39;],</div>
<div>                 &#39;Content-Type&#39;:[&#39;application/x-www-form-urlencoded&#39;]}),</div><div>        body)</div><div>    #             &#39;Content-Length&#39;:[str(len(postdata))],</div><div><br></div><div><br>
</div><div><br></div><div>    def cbRequest(response):</div><div>        print &#39;Response version:&#39;, response.version</div><div>        print &#39;Response code:&#39;, response.code</div><div>        print &#39;Response phrase:&#39;, response.phrase</div>
<div>        print &#39;Response headers:&#39;</div><div>        print pformat(list(response.headers.getAllRawHeaders()))</div><div>        finished = Deferred()</div><div>        print &#39;response deliverbody&#39;,response.deliverBody</div>
<div>        response.deliverBody(BeginningPrinter(finished))</div><div>        return finished</div><div>    d.addCallback(cbRequest)</div><div>    #reactor.callLater(10, check_timeout,d)</div><div>    #j = dir(d)</div><div>
    #print &#39;later&#39;, j</div><div>    #print &#39;fff cancel&#39;, d.cancel</div><div>    #print &#39;fff canceller&#39;, d._canceller</div><div>    #k = dir(agent.request)</div><div>    #print &#39;kkk&#39;, k</div>
<div>    #print &#39;reacotcallater&#39;</div><div>    #if a == &#39;fred&#39;:</div><div>    #    print &#39;cancel &#39;, a</div><div>    no_response = reactor.callLater(40, do_cancel, d)</div><div>        #d.cancel()</div>
<div><br></div><div>    def got_error(o):</div><div>        print &#39;got error&#39;, str(o)</div><div>        reason = dir(o)</div><div>        print &#39;got reason&#39;, o.getErrorMessage</div><div><br></div><div>    def cbShutdown(ignored, no_response):</div>
<div>        print &#39;got finish ignored&#39;, ignored</div><div>        #print &#39;no rsposne&#39;, no_response</div><div>        norep = dir(no_response)</div><div>        #print &#39;kkk&#39;, norep</div><div>        if no_response.cancelled:</div>
<div>            print &#39;already cancelled&#39;</div><div>        else:</div><div>            print &#39;not cancelled&#39;</div><div>            no_response.cancel()</div><div>            no_response = None</div><div>
        #reactor.stop()</div><div>    d.addCallback(cbShutdown, no_response)</div><div>    d.addErrback(got_error)</div><div><br></div><div><br></div><div>reactor.run()</div></div><div><br></div>