1
from twisted.application import internet # services that run TCP/SSL/etc.
2
from TwistedQuotes import quoteproto # Protocol and Factory
3
from TwistedQuotes import quoters # "give me a quote" code
5
from twisted.python import usage # twisted command-line processing
8
class Options(usage.Options):
9
optParameters = [["port", "p", 8007,
10
"Port number to listen on for QOTD protocol."],
11
["static", "s", "An apple a day keeps the doctor away.",
12
"A static quote to display."],
14
"A fortune-format text file to read quotes from."]]
17
def makeService(config):
18
"""Return a service that will be attached to the application."""
19
if config["file"]: # If I was given a "file" option...
20
# Read quotes from a file, selecting a random one each time,
21
quoter = quoters.FortuneQuoter([config['file']])
23
# read a single quote from the command line (or use the default).
24
quoter = quoters.StaticQuoter(config['static'])
25
port = int(config["port"]) # TCP port to listen on
26
factory = quoteproto.QOTDFactory(quoter) # here we create a QOTDFactory
27
# Finally, set up our factory, with its custom quoter, to create QOTD
28
# protocol instances when events arrive on the specified port.
29
return internet.TCPServer(port, factory)