By Seecr

Software Craftsmanship

Seecr - Software Craftsmanship By Erik J. Groeneveld,
This site was last updated on June 24th 2011

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 example in Meresco: server.py.