Difference between revisions of "Mulib/Examples"

From Second Life Wiki
Jump to: navigation, search
m (stacked)
(stacked: shoulda previewed)
Line 26: Line 26:
 
Stacked is a pure REST server, and you can use it to traverse native python objects like <code>dicts</code>.
 
Stacked is a pure REST server, and you can use it to traverse native python objects like <code>dicts</code>.
  
<nowiki>
+
<code><pre><nowiki>
 
  from mulib import mu
 
  from mulib import mu
 
   
 
   
Line 35: Line 35:
 
   
 
   
 
  httpd.server(api.tcp_listener(('0.0.0.0', 8080)), mu.SiteMap(root))
 
  httpd.server(api.tcp_listener(('0.0.0.0', 8080)), mu.SiteMap(root))
</nowiki>
+
</nowiki></pre></code>
  
 
You can then access this dictionary as a REST resource, e.g.
 
You can then access this dictionary as a REST resource, e.g.
<nowiki>
+
<pre><nowiki>
 
  > curl http://localhost:8080/     
 
  > curl http://localhost:8080/     
 
  hello, world
 
  hello, world
Line 46: Line 46:
 
  > curl http://localhost:8080/third
 
  > curl http://localhost:8080/third
 
  the new data
 
  the new data
</nowiki>
+
</nowiki></pre>
  
 
Mu/stacked can do content negotiation:
 
Mu/stacked can do content negotiation:
  
<nowiki>
+
<pre><nowiki>
 
  > curl -X PUT -H "Content-type: application/json" -d '{"hi": "there"}' http://localhost:8080/fourth
 
  > curl -X PUT -H "Content-type: application/json" -d '{"hi": "there"}' http://localhost:8080/fourth
 
  > curl http://localhost:8080/fourth/hi
 
  > curl http://localhost:8080/fourth/hi
Line 56: Line 56:
 
  > curl -H "Accept: application/json" http://localhost:8080/fourth
 
  > curl -H "Accept: application/json" http://localhost:8080/fourth
 
  {'hi': 'there'}
 
  {'hi': 'there'}
</nowiki>
+
</nowiki></pre>
  
 
'''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.
 
'''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.

Revision as of 16:19, 24 August 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":

from mulib import mu

from eventlet import api, httpd

class HelloWorld(mu.Resource):
   def handle_get(self, req):
       req.write("hello, world\n")

root = HelloWorld()

httpd.server(
   api.tcp_listener(('0.0.0.0', 8080)),
   mu.SiteMap(root))


stacked

Stacked is a pure REST server, and you can use it to traverse native python objects like dicts.

 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.