Difference between revisions of "Mulib/Examples"
Which Linden (talk | contribs) (→stacked: clarifying how to use stacked) |
Which Linden (talk | contribs) (→hello world: Slightly modified examples and added a cgi section.) |
||
Line 7: | Line 7: | ||
The following program will bring up a webserver listening on port 8080 which can respond to a single request, "GET /", with the response "hello, world": | The following program will bring up a webserver listening on port 8080 which can respond to a single request, "GET /", with the response "hello, world": | ||
# hello_world.py: | |||
from mulib import mu | from mulib import mu | ||
Line 12: | Line 13: | ||
class HelloWorld(mu.Resource): | class HelloWorld(mu.Resource): | ||
def handle_get(self, req): | |||
req.write("hello, world\n") | |||
if __name__ == "__main__": | |||
root = HelloWorld() | |||
httpd.server( | |||
api.tcp_listener(('0.0.0.0', 8080)), | |||
mu.SiteMap(root)) | |||
=== CGI === | |||
You can treat this resource as a [http://en.wikipedia.org/wiki/Common_Gateway_Interface CGI], by writing a second, wrapper, file that refers to it: | |||
# hello_world.cgi: | |||
#!/usr/bin/python | |||
from mulib import cgiadapter | |||
cgiadapter.run_as_cgi('hello_world', 'HelloWorld') | |||
Configure your web server to execute hello_world.cgi, and you should be able to interact with it just like the standalone version. | |||
== stacked == | == stacked == |
Revision as of 17:42, 11 December 2007
Mulib Examples
These are some short examples to give a flavor of using mulib.
hello world
The following program will bring up a webserver listening on port 8080 which can respond to a single request, "GET /", with the response "hello, world":
# hello_world.py: from mulib import mu from eventlet import api, httpd class HelloWorld(mu.Resource): def handle_get(self, req): req.write("hello, world\n") if __name__ == "__main__": root = HelloWorld()
httpd.server( api.tcp_listener(('0.0.0.0', 8080)), mu.SiteMap(root))
CGI
You can treat this resource as a CGI, by writing a second, wrapper, file that refers to it:
# hello_world.cgi: #!/usr/bin/python from mulib import cgiadapter cgiadapter.run_as_cgi('hello_world', 'HelloWorld')
Configure your web server to execute hello_world.cgi, and you should be able to interact with it just like the standalone version.
stacked
Stacked is a pure REST server, and you can use it to traverse native python objects like dicts
. You invoke these special capabilities of Stacked by placing a python dict or list in the resource hierarchy instead of a mu.Resource, in this case at the root.
from mulib import mu
from eventlet import api, httpd
root = {'':'hello, world\n',
'other':"hello, other\n"}
httpd.server(api.tcp_listener(('0.0.0.0', 8080)), mu.SiteMap(root))
You can then access this dictionary as a REST resource, e.g.
> curl http://localhost:8080/ hello, world > curl http://localhost:8080/other hello, other > curl -X PUT -d "the new data" http://localhost:8080/third > curl http://localhost:8080/third the new data
Mu/stacked can do content negotiation:
> curl -X PUT -H "Content-type: application/json" -d '{"hi": "there"}' http://localhost:8080/fourth > curl http://localhost:8080/fourth/hi there > curl -H "Accept: application/json" http://localhost:8080/fourth {'hi': 'there'}
Note: This means that anyone who has access to your stacked web service can modify the data in your process! In the future we might have a 'read-only' implementation.