Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 10th, 2012  |  syntax: Python  |  size: 2.36 KB  |  hits: 83  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. from time import strftime
  2. from twisted.internet import reactor
  3. from twisted.internet.defer import Deferred, DeferredList
  4. from twisted.internet.protocol import Protocol
  5. from twisted.web.client import Agent, HTTPConnectionPool
  6. from twisted.web.http_headers import Headers
  7. from twisted.internet.error import AlreadyCalled, AlreadyCancelled
  8.  
  9. class BeginningPrinter(Protocol):
  10.     def __init__(self,handler):
  11.         self.handler=handler
  12.     def dataReceived(self, received):
  13.         handler(received)
  14.     def connectionLost(self, reason):
  15.         pass
  16.  
  17. class Site():
  18.     def __init__(self,url,rest,wait,pool,md5=None,ind=None,name=None):
  19.         self.url=url
  20.         self.rest=rest
  21.         self.wait=wait
  22.         self.pool=pool
  23.         self.md5=md5
  24.         self.ind=ind if ind else 0
  25.         self.name=name if name else self.url
  26.         self.last_success=0
  27.         self.last_received=0
  28.         self.agent = Agent(reactor, pool=pool)
  29.         self.deferred=None
  30.         self.delayedCall=None
  31.     def __request(self):
  32.         self.deferred = self.agent.request(
  33.             'GET',
  34.             self.url,
  35.             Headers({'User-Agent': ['Mozilla/5.0']})
  36.         )
  37.     def again(self):
  38.         print strftime('%X'),'again'
  39.         try:
  40.             self.deferred.cancel()
  41.         except (AlreadyCalled, AlreadyCancelled) :
  42.             pass
  43.         try:
  44.             self.delayedCall.reset(self.wait)
  45.         except (AlreadyCalled, AlreadyCancelled) :
  46.             self.delayedCall = reactor.callLater(self.wait, self.timeIsOff)
  47.  
  48.         self.__request()
  49.         self.deferred.addCallback(self.gotRequest)
  50.     def timeIsOff(self):
  51.         print strftime('%X'),'time is off'
  52.         self.again()
  53.     def handleReceived(self, received):
  54.         print strftime('%X'),'received: ',received
  55.     def gotRequest(self, response):
  56.         print strftime('%X'),'gotRequest'
  57.         self.again()
  58.         print 'Response code:', response.code
  59.         response.deliverBody(BeginningPrinter(self.handleReceived))
  60.     def startLive(self):
  61.         print strftime('%X'),'startLive'
  62.         self.__request()
  63.         self.deferred.addCallback(self.gotRequest)
  64.         self.delayedCall = reactor.callLater(self.wait, self.timeIsOff)
  65.  
  66. pool = HTTPConnectionPool(reactor)
  67. site1 = Site(url='http://wool/cgi-bin/wait.cgi',rest=3,wait=2,pool=pool)
  68. site1.startLive()
  69. reactor.run()