Web dev example : JSON & jQuery Mobile & Bottle
Bottle is a fast, simple and lightweight WSGI micro web-framework for Python. jQuery Mobile base on jQuery for mobile device. jQuery vs. jQuery Mobile vs. jQuery UI Install bottle: $ sudo apt-get install python-setuptools $ easy_install bottle Demo server deployment file structure: BottlejQuery ├── bottleJQuery.py └── index.html run command: $ ./bottleJQuery.py connect to server: http://localhost:8080/bottle Building simple web server use bottle bottleJQuery.py #!/usr/bin/env python from bottle import route, static_file, debug, run, get, redirect from bottle import post, request import os, inspect, json #enable bottle debug debug(True) # WebApp route path routePath = '/bottle' # get directory of WebApp (bottleJQuery.py's dir) rootPath = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) @route(routePath) def rootHome(): return redirect(routePath+'/index.html') @route(routePath + '/<filename:re:.*\.html>') def html_file(filename): return static_file(filename, root=rootPath) @get(routePath + '/jsontest') def testJsonGET(): print "GET Header : \n %s" % dict(request.headers) #for debug header return {"id":2,"name":"Jone"} @post(routePath + '/jsontest') def testJsonPost(): print "POST Header : \n %s" % dict(request.headers) #for debug header data = request.json print "data : %s" % data if data == None: return json.dumps({'Status':"Failed!"}) else: return json.dumps({'Status':"Success!"}) run(host='localhost', port=8080, reloader=True) Test GET request: $ curl -i -X GET http://localhost:8080/bottle/jsontest HTTP/1.0 200 OK Date: Wed, 07 Aug 2013 16:03:53 GMT Server: WSGIServer/0.1 Python/2.7.3 Content-Length: 25 Content-Type: application/json {"id": 2, "name": "Jone"} bottle debug message: ...