summaryrefslogtreecommitdiff
path: root/doc/core/howto/listings/application/service.tac
diff options
context:
space:
mode:
Diffstat (limited to 'doc/core/howto/listings/application/service.tac')
-rw-r--r--doc/core/howto/listings/application/service.tac34
1 files changed, 34 insertions, 0 deletions
diff --git a/doc/core/howto/listings/application/service.tac b/doc/core/howto/listings/application/service.tac
new file mode 100644
index 0000000..b0167fa
--- /dev/null
+++ b/doc/core/howto/listings/application/service.tac
@@ -0,0 +1,34 @@
+# You can run this .tac file directly with:
+# twistd -ny service.tac
+
+"""
+This is an example .tac file which starts a webserver on port 8080 and
+serves files from the current working directory.
+
+The important part of this, the part that makes it a .tac file, is
+the final root-level section, which sets up the object called 'application'
+which twistd will look for
+"""
+
+import os
+from twisted.application import service, internet
+from twisted.web import static, server
+
+def getWebService():
+ """
+ Return a service suitable for creating an application object.
+
+ This service is a simple web server that serves files on port 8080 from
+ underneath the current working directory.
+ """
+ # create a resource to serve static files
+ fileServer = server.Site(static.File(os.getcwd()))
+ return internet.TCPServer(8080, fileServer)
+
+# this is the core part of any tac file, the creation of the root-level
+# application object
+application = service.Application("Demo application")
+
+# attach the service to its parent application
+service = getWebService()
+service.setServiceParent(application)