Created
May 2, 2012 21:14
-
-
Save Drvanon/2580575 to your computer and use it in GitHub Desktop.
classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Traceback (most recent call last): | |
File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 84, in callWithLogger | |
return callWithContext({"system": lp}, func, *args, **kw) | |
File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 69, in callWithContext | |
return context.call({ILogContext: newCtx}, func, *args, **kw) | |
File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 118, in callWithContext | |
return self.currentContext().callWithContext(ctx, func, *args, **kw) | |
File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 81, in callWithContext | |
return func(*args,**kw) | |
--- <exception caught here> --- | |
File "/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.py", line 586, in _doReadOrWrite | |
why = selectable.doRead() | |
File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 419, in doConnect | |
self._connectDone() | |
File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 422, in _connectDone | |
self.protocol = self.connector.buildProtocol(self.getPeer()) | |
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1049, in buildProtocol | |
return self.factory.buildProtocol(addr) | |
File "/usr/lib/python2.7/dist-packages/twisted/internet/protocol.py", line 104, in buildProtocol | |
p = self.protocol() | |
exceptions.AttributeError: PoetryProtocol instance has no __call__ method | |
Failed to connect to: IPv4Address(TCP, '127.0.0.1', 10000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is the Twisted Get Poetry Now! client, version 2.1. | |
# NOTE: This should not be used as the basis for production code. | |
import optparse | |
from twisted.internet.protocol import Protocol, ClientFactory | |
def parse_args(): | |
usage = """usage: %prog [options] [hostname]:port ... | |
This is the Get Poetry Now! client, Twisted version 2.1. | |
Run it like this: | |
python get-poetry-simple.py port1 port2 port3 ... | |
If you are in the base directory of the twisted-intro package, | |
you could run it like this: | |
python twisted-client-2/get-poetry-simple.py 10001 10002 10003 | |
to grab poetry from servers on ports 10001, 10002, and 10003. | |
Of course, there need to be servers listening on those ports | |
for that to work. | |
""" | |
parser = optparse.OptionParser(usage) | |
_, addresses = parser.parse_args() | |
if not addresses: | |
print parser.format_help() | |
parser.exit() | |
def parse_address(addr): | |
if ':' not in addr: | |
host = '127.0.0.1' | |
port = addr | |
else: | |
host, port = addr.split(':', 1) | |
if not port.isdigit(): | |
parser.error('Ports must be integers.') | |
return host, int(port) | |
return map(parse_address, addresses) | |
class CountDown(object): | |
counter = 2 | |
kill = False | |
def count(self): | |
print 'got there' | |
if self.counter == 0: | |
self.kill = True | |
print 'killed' | |
else: | |
self.counter -= 1 | |
reactor.callLater(1, self.count) | |
class PoetryProtocol(Protocol): | |
poem = '' | |
timers = [] | |
def countdowners(self): | |
for poe in self.factory.poems: | |
timers.append(CountDown) | |
def checktime(self): | |
print 'checked the time' | |
for i in self.timers: | |
i.count() | |
if i.kill == True: | |
loseConnection() | |
def dataReceived(self, data): | |
self.poem += data | |
def connectionLost(self, reason): | |
self.poemReceived(self.poem) | |
def connectionMade(self): | |
self.checktime() | |
self.countdowners() | |
def poemReceived(self, poem): | |
self.factory.poem_finished(poem) | |
class PoetryClientFactory(ClientFactory): | |
protocol = PoetryProtocol() # tell base class what proto to build | |
def __init__(self, poetry_count): | |
self.poetry_count = poetry_count | |
self.poems = [] | |
def poem_finished(self, poem=None): | |
if poem is not None: | |
self.poems.append(poem) | |
self.poetry_count -= 1 | |
self.poetry_loaded = True | |
if self.poetry_count == 0: | |
self.report() | |
from twisted.internet import reactor | |
reactor.stop() | |
def report(self): | |
for poem in self.poems: | |
print poem | |
def clientConnectionFailed(self, connector, reason): | |
print 'Failed to connect to:', connector.getDestination() | |
self.poem_finished() | |
def poetry_main(): | |
addresses = parse_args() | |
factory = PoetryClientFactory(len(addresses)) | |
from twisted.internet import reactor | |
for address in addresses: | |
host, port = address | |
reactor.connectTCP(host, port, factory) | |
reactor.run() | |
if __name__ == '__main__': | |
poetry_main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment