<div>
<div>I have a newbie question. By the way, even though it is a brain-f**k, I love programming in this software. I just wish there was an easier/faster way to understand it. I got and read the twisted book, but unfortunately it does not have twisted web2 examples...
</div>
<div> </div>
<div>Anyway, I have written a web server using web2 that takes an HTTP request and sends it off to another server to process it. If the other server were to crash, how can I make the http request timeout after 30 seconds and redirect to a timeout page?
</div>
<div> </div>
<div>Following is my resource code. When the response is available, I take the response stream, write to it, and call finish().</div>
<div>
<p>class HTTPServer(resource.PostableResource):</p>
<p> def __init__(self, cache):<br> resource.Resource.__init__(self)<br> self.cache = cache</p>
<p> def _finished(self, result, httprequest, postdata):<br> convq = self.cache['conversations']<br> s=stream.ProducerStream() <br> self.cache['requests'][id(httprequest)] = (httprequest,postdata,s)
<br> if httprequest.args.has_key(CONVID):<br> conversationid = httprequest.args[CONVID][0]<br> try:<br> convq[conversationid].put(httprequest)<br> except:<br> convq[conversationid] =
Queue.Queue()<br> convq[conversationid].put(httprequest)<br> else:<br> convq[ANY].put(httprequest)<br> if log:<br> method = httprequest.method<br> raw_uri =
urllib.unquote(httprequest.uri)<br> print "%-4s" % method,<br> print "%-6s" % 'CLIENT',<br> print "%-4s" % raw_uri</p>
<p> return http.Response(stream=s)</p>
<p> def _failed(self, reason, httprequest, postdata):<br> result = str(reason)<br> result += str(httprequest.uri) + '\n'<br> result += '\n'.join(postdata) + '\n'<br> return
http.Response(500, stream="An internal server error has occurred.")</p>
<p> def http_POST(self, httprequest):<br> postdata = []<br> d = stream.readStream(httprequest.stream, postdata.append)<br> d.addCallbacks(<br> self._finished,<br> self._failed,
<br> callbackArgs=(httprequest, postdata,),<br> errbackArgs=(httprequest, postdata,)<br> )</p>
<p> # Catch any errors that occur in self._finished.<br> d.addErrback(self._failed, httprequest, postdata)</p>
<p> return d</p>
<p> http_PUT = http_POST<br> http_GET = http_POST<br></p>
<p> </p>
<p>~Paul</p></div></div>