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 'Some data received:', bytes, 'no bytes', len(bytes)</div>
<div> print display</div><div> self.remaining -= len(display)</div><div><br></div><div> def connectionLost(self, reason):</div><div> print 'Finished receiving body:', reason.getErrorMessage()</div>
<div> self.finished.callback(None)</div><div><br></div><div>def do_cancel(d):</div><div> print 'got do cancel'</div><div> d._canceller(d)</div><div><br></div><div><br></div><div>from stringprod_resp import StringProducer</div>
<div>my_data = ['fred','ted','red','fred']</div><div>for c, a in enumerate(my_data):</div><div> if c > 2:</div><div> time.sleep(10)</div><div> agent = Agent(reactor)</div><div>
#postdata = urllib.urlencode({'Username' : 'nsp05682',</div><div> # 'Password' : 'rocs345',</div><div> # 'Action' : 'CheckCredits'})</div>
<div> postdata = urllib.urlencode({'the-field' : a})</div><div> body = StringProducer(postdata)</div><div> print 'body', body</div><div> d = agent.request(</div><div> 'POST',</div>
<div> '<a href="http://www.johna.com/test">http://www.johna.com/test</a>',</div><div> Headers({'User-Agent': ['Twisted Web Client'],</div><div> 'Accept-Encoding':['identity'],</div>
<div> 'Content-Type':['application/x-www-form-urlencoded']}),</div><div> body)</div><div> # 'Content-Length':[str(len(postdata))],</div><div><br></div><div><br>
</div><div><br></div><div> def cbRequest(response):</div><div> print 'Response version:', response.version</div><div> print 'Response code:', response.code</div><div> print 'Response phrase:', response.phrase</div>
<div> print 'Response headers:'</div><div> print pformat(list(response.headers.getAllRawHeaders()))</div><div> finished = Deferred()</div><div> print 'response deliverbody',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 'later', j</div><div> #print 'fff cancel', d.cancel</div><div> #print 'fff canceller', d._canceller</div><div> #k = dir(agent.request)</div><div> #print 'kkk', k</div>
<div> #print 'reacotcallater'</div><div> #if a == 'fred':</div><div> # print 'cancel ', 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 'got error', str(o)</div><div> reason = dir(o)</div><div> print 'got reason', o.getErrorMessage</div><div><br></div><div> def cbShutdown(ignored, no_response):</div>
<div> print 'got finish ignored', ignored</div><div> #print 'no rsposne', no_response</div><div> norep = dir(no_response)</div><div> #print 'kkk', norep</div><div> if no_response.cancelled:</div>
<div> print 'already cancelled'</div><div> else:</div><div> print 'not cancelled'</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>