Skip to content

Instantly share code, notes, and snippets.

@Drvanon
Created May 2, 2012 23:49
Show Gist options
  • Save Drvanon/2581990 to your computer and use it in GitHub Desktop.
Save Drvanon/2581990 to your computer and use it in GitHub Desktop.
from twisted.application import internet, service
from twisted.internet.protocol import ServerFactory, Protocol
from twisted.python import log
# Normally we would import these classes from another module.
class PoetryProtocol(Protocol):
def connectionMade(self):
poem = self.factory.service.poem
log.msg('sending %d bytes to %s'
% (len(poem), self.transport.getPeer()))
self.transport.write(poem)
self.transport.loseConnection()
class PoetryFactory(ServerFactory):
protocol = PoetryProtocol
def __init__(self, service):
self.service = service
class PoetryService(service.Service):
def __init__(self, poetry_file):
self.poetry_file = poetry_file
def startService(self):
service.Service.startService(self)
self.poem = open(self.poetry_file).read()
# configuration parameters
# so the in-game parameters too
port = 10000
iface = 'localhost'
# this will hold the services that combine to form the server
top_service = service.MultiService()
# the service holds the parameters. it will load the poem when it is
# started
poetry_service = PoetryService(poetry_file)
poetry_service.setServiceParent(top_service)
# the tcp service connects the factory to a listening socket. it will
# create the listening socket when it is started
factory = PoetryFactory(poetry_service)
tcp_service = internet.TCPServer(port, factory, interface=iface)
tcp_service.setServiceParent(top_service)
# this variable has to be named 'application'
application = service.Application("fastpoetry")
# this hooks the collection we made to the application
top_service.setServiceParent(application)
# at this point, the application is ready to go. when started by
# twistd it will start the child services, thus starting up the
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment