1 from TwistedQuotes import quoteproto # Protocol and Factory
2 from TwistedQuotes import quoters # "give me a quote" code
3 from TwistedQuotes import pbquote # perspective broker binding
5 from twisted.application import service, internet
6 from twisted.python import usage # twisted command-line processing
7 from twisted.spread import pb # Perspective Broker
9 class Options(usage.Options):
10 optParameters = [["port", "p", 8007,
11 "Port number to listen on for QOTD protocol."],
12 ["static", "s", "An apple a day keeps the doctor away.",
13 "A static quote to display."],
15 "A fortune-format text file to read quotes from."],
17 "Port to listen with PB server"]]
19 def makeService(config):
20 svc = service.MultiService()
21 if config["file"]: # If I was given a "file" option...
22 # Read quotes from a file, selecting a random one each time,
23 quoter = quoters.FortuneQuoter([config['file']])
25 # read a single quote from the command line (or use the default).
26 quoter = quoters.StaticQuoter(config['static'])
27 port = int(config["port"]) # TCP port to listen on
28 factory = quoteproto.QOTDFactory(quoter) # here we create a QOTDFactory
29 # Finally, set up our factory, with its custom quoter, to create QOTD
30 # protocol instances when events arrive on the specified port.
31 pbport = config['pb'] # TCP PB port to listen on
33 pbfact = pb.PBServerFactory(pbquote.QuoteReader(quoter))
34 svc.addService(internet.TCPServer(int(pbport), pbfact))
35 svc.addService(internet.TCPServer(port, factory))