Python Lover(8)Twisted Basic - async - logging

PythonLover(8)TwistedBasic-async-logging

1.AsynchronousResponse

http://twistedmatrix.com/documents/current/web/howto/web-in-60/asynchronous.html

BasicDifferences

Wedonotdirectlyresponsestring,wecallRequest.write.Wecancallrequest.writemanytimes,aftertheentireresponsebodyhasbeenpassedtoRequest.write,theapplicationmustcallRequest.finish.

fromtwisted.web.resourceimportResource

fromtwisted.web.serverimportNOT_DONE_YET

fromtwisted.internetimportreactor

classDeplayedResource(Resource):

def_deplayedRender(self,request):

request.write("""

<html>

<body>

Sorrytokeepyouwaiting.

</body>

</html>

""")

defrender_GET(self,request):

reactor.callLater(5,self._deplayedRender,request)#delay5seconds

returnNOT_DONE_YET

resource=DeplayedResource()

Commandtostart

>twistd-nweb--path.

Visitthispagetoverify

http://localhost:8080/RestAPI.rpy

2.DeferredinAsyncResponse

http://twistedmatrix.com/documents/current/web/howto/web-in-60/asynchronous-deferred.html

Deferredprovidescallbacksanderrbacks.

Deferred.addCallbackDeferred.callback

Deferred.addErrbackDeferred.errback

fromtwisted.internet.taskimportdeferLater

fromtwisted.web.resourceimportResource

fromtwisted.web.serverimportNOT_DONE_YET

fromtwisted.internetimportreactor

classDelayedResource(Resource):

def_delayedRender(self,request):

request.write("""

<html>

<body>

Sorrytokeepyouwaiting.

</body>

</html>

""")

request.finish()

defrender_GET(self,request):

d=deferLater(reactor,5,lambda:request)

d.addCallback(self._delayedRender)

returnNOT_DONE_YET

resource=DelayedResource()

3.InterruptedResponse

http://twistedmatrix.com/documents/current/web/howto/web-in-60/interrupted.html

Sincewedoasync,sometimes,wehavenotfinishedgeneratetheresult,buttheclientconnectionsareclose,soweneedtoabandontheresponsegenerationentirely.

CanceltheGenerationProcess

fromtwisted.web.resourceimportResource

fromtwisted.web.serverimportNOT_DONE_YET

fromtwisted.internetimportreactor

classDelayedResource(Resource):

def_delayedRender(self,request):

request.write("""

<html>

<body>

Sorrytokeepyouwaiting.

</body>

</html>

""")

request.finish()

def_responseFailed(self,err,call):

call.cancel()

defrender_GET(self,request):

call=reactor.callLater(5,self._delayedRender,request)

request.notifyFinish().addErrback(self._responseFailed,call)

returnNOT_DONE_YET

resource=DelayedResource()

4.LoggingError

http://twistedmatrix.com/documents/current/web/howto/web-in-60/logging-errors.html

fromtwisted.web.resourceimportResource

fromtwisted.web.serverimportNOT_DONE_YET

fromtwisted.internetimportreactor

fromtwisted.python.logimporterr

classDelayedResource(Resource):

def_delayedRender(self,request):

request.write("""

<html>

<body>

Sorrytokeepyouwaiting.

</body>

</html>

""")

request.finish()

def_responseFailed(self,failure,call):

call.cancel()

err(failure,"Asyncresponsedemointerruptedresponse")

defrender_GET(self,request):

call=reactor.callLater(5,self._delayedRender,request)

request.notifyFinish().addErrback(self._responseFailed,call)

returnNOT_DONE_YET

resource=DelayedResource()

Thesemessagewilldisplaywhenwecanceltheclientrequest.

2015-09-1614:00:36-0500[HTTPChannel,0,127.0.0.1]Asyncresponsedemointerruptedresponse

Traceback(mostrecentcalllast):

Failure:twisted.internet.error.ConnectionDone:Connectionwasclosedcleanly.

5.AccessLogging

http://twistedmatrix.com/documents/current/web/howto/web-in-60/access-logging.html

Wecanconfiguretheloggingin2differentways.

factory=Site(root,logPath=b”/tmp/access-logging-demo.log”)

or

>twistd-nweb--logfile/tmp/access-logging-demo.log

>twistd-nweb--path.--logfile./RestAPI.access.log

References:

http://twistedmatrix.com/documents/15.0.0/core/howto/index.html

http://twistedmatrix.com/documents/current/web/howto/web-in-60/

deployment

http://twistedmatrix.com/trac/wiki/TwistedWeb

https://github.com/todddeluca/daemoncmd

https://bitbucket.org/tildeslash/monit/

相关推荐