Example

Hello World

Below is a simple HTTP server that answers a request from a browser with "Hello!":

  from weightless import Reactor, Server, HttpProtocol, http, be

  class HelloWorld(object):
      def processRequest(self, *args, **kwargs):
          yield http.ok()
          yield http.headers('Content-Length', 6)
          yield 'Hello!'

  reactor = Reactor()

  dna = \
      (Server(reactor, 8080),
          (HttpProtocol(),
              (HelloWorld(),)
          )
      )

  server = be(dna)
  reactor.loop()

Simply start this server with:

  $ python httpserver.py

Next, use your browser to go to http://localhost:8080.

In practice, you would have the class HelloWorld in a separate file. We call the remaining things in httpserver.py the server configuration.

Real-world server configurations

If you like to see (large!) real-world server configurations, take a look at the examples in Meresco. Meresco runs on Weightless 0.4 (not on sourceforge, only >= 0.5 will be on SF):

  1. Example Application for Meresco 2.13 (stable) with Weightless 0.4: Documentation and code.
  2. Example Application for Meresco 2.20 (testing) with Weightless 0.4.2: code.

Powered by Seek You Too