Let’s see a basic usage of the framework:
# Import from itools
from itools.web import Server, Root
class MyRoot(Root):
GET__access__ = True
def GET(self, context):
return 'Hello World'
if __name__ == '__main__':
root = MyRoot()
server = Server(root)
server.start()
To test the code above, type:
$ python hello.py
Then open a browser and go to http://localhost:8080 to see the famous sentence.
These few lines of code expose several aspects of itools.web that we will see later with more detail:
The class Server implements a Web server. It expects the root of your application as the first parameter.
The root of the application must be an instance of Root. You subclass it to write your own application.
The method GET will be called for HTTP GET requests.
We have to open access to this method with GET__access__, because by default everything is closed.
The GET method expects the context as a parameter. The context object is the primary programming interface.
The GET method returns the string that will be sent to the browser.