Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
110 lines (68 sloc) 2.45 KB
# 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 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/ 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()
def parse_address(addr):
if ':' not in addr:
host = ''
port = addr
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 PoetryProtocol(Protocol):
poem = ''
def dataReceived(self, data):
self.poem += data
def connectionLost(self, reason):
def poemReceived(self, 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.poetry_count -= 1
if self.poetry_count == 0:
from twisted.internet import reactor
def report(self):
for poem in self.poems:
print poem
def clientConnectionFailed(self, connector, reason):
print 'Failed to connect to:', connector.getDestination()
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)
if __name__ == '__main__':