twisted.web.server.Website

Here are the examples of the python api twisted.web.server.Website taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

1 Examples 7

Example 1

Project: Cactus_Refactored Source File: site.py
    def serve(self, uri="localhost:8000"):
        """
        Start a http server and rebuild on changes.
        """
        host, _, port = uri.partition(":")
        port = int(port)

        self.clean()
        self.build(dist=False)

        logging.info('Running webserver at {0}'.format(uri))
        logging.info('Type control-c to exit')

        os.chdir(self.paths['build'])

        def rebuild(changes):
            logging.info("*** Rebuilding ({0} changed)".format(self.path))

            reload_needed = False
            rebuild_needed = False
            for p in changes.get('any', []):
                if os.path.realpath(os.path.dirname(p)) == os.path.realpath(self.path):
                    if os.path.basename(p) == "config.yml":
                        reload_needed = True
                        rebuild_needed = True
                else:
                    rebuild_needed = True

            if rebuild_needed:
                # We will pause the listener while building so scripts
                # that alter the output like coffeescript and less don't
                # trigger the listener again immediately.
                self.listener.pause()
                try:
                    if reload_needed:
                        self._load_config()
                        self.load_plugins()
                        self.load_context_processors()
                    self.build()
                except Exception, e:
                    logging.info("*** Error while building\n{0}".format(e))
                    traceback.print_exc(file=sys.stdout)

                cssonly = False
                if len(changes["added"]) == 0 and len(changes["deleted"]) == 0:
                    exts = set(map(lambda x: os.path.splitext(x)[1], changes["changed"]))
                    cssonly = True
                    for ext in exts:
                        if not re.match(r'\.(?:css|sass|scss|less)$', ext, re.I):
                            cssonly = False
                if cssonly:
                    browser.browserReloadCSS("http://localhost:{0}".format(port), self)
                else:
                    browser.browserReload("http://localhost:{0}".format(port), self)

                self.listener.resume()

        from .listener import Listener

        self.listener = Listener(
            self.path, rebuild, ignore=lambda x: '/.tmp/' in x
        )
        self.listener.run()

        from twisted.web.server import Site as Website
        from twisted.web.static import File
        from twisted.internet import reactor

        resource = File(self.paths['build'])
        factory = Website(resource)
        reactor.listenTCP(port, factory)

        browser.openurl("http://localhost:{0}".format(port), self)

        try:
            reactor.run()
        except (KeyboardInterrupt, SystemExit):
            reactor.stop()

        logging.info('See you!')