~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to doc/core/howto/listings/TwistedQuotes/quotetap2.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-01-16 19:56:10 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060116195610-ykmxbia4mnnod9o2
Tags: 2.1.0-0ubuntu2
debian/copyright: Include copyright for python 2.3; some 2.3 files
are included in the upstream tarball, but not in the binary packages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
4
 
        
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
8
 
 
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."],
14
 
                     ["file", "f", None,
15
 
                      "A fortune-format text file to read quotes from."],
16
 
                     ["pb", "b", None,
17
 
                      "Port to listen with PB server"]]
18
 
 
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']])
24
 
    else:                               # otherwise,
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
32
 
    if pbport:
33
 
        pbfact = pb.PBServerFactory(pbquote.QuoteReader(quoter))
34
 
        svc.addService(internet.TCPServer(int(pbport), pbfact))
35
 
    svc.addService(internet.TCPServer(port, factory))
36
 
    return svc