-
Notifications
You must be signed in to change notification settings - Fork 211
pyjamaswithpylonsjsonrpc
- How to get pylons talking JSONRPC
http://www.machine-envy.com/blog/2006/12/10/howto-pyjamas-pylons-json/
Comment by cornelis.bos, Apr 15, 2009
With the current pyjamas/pylons versions, the directions in the above link needed some adjustments for me. Installing pyjamas¶
mkdir pyjamas cd pyjamas wget http://pyjamas.googlecode.com/files/pyjamas-0.5p1.tgz tar xzf pyjamas-0.5p1.tgz cd pyjamas-0.5p1 python bootstrap.py cd examples ./buildall.sh
cd ../../..
Installing pylons¶
wget http://www.pylonshq.com/download/0.9.7/go-pylons.py python go-pylons.py --no-site-packages pylons
Creating echo project¶ Activate pylons environment¶
cd pylons . ./bin/acivate
Create project directories¶
paster create --template pylons echo
Copy jsonrpc example¶
cd echo/echo/public cp -pr ../../../../pyjamas/pyjamas-0.5p1/examples/jsonrpc/* .
Modify JSONRPCExample.py¶
class EchoServicePython(JSONProxy): def __init__(self): JSONProxy.__init__(self, "/echoservice/json", ["echo", "reverse", "uppercase", "lowercase"])
Build output¶
../../../../pyjamas/pyjamas-0.5p1/bin/pyjsbuild JSONRPCExample.py
Create controler¶
cd ../.. paster controller echoservice
Modify echo/controllers/echoservice.py¶
from echo.lib.base import BaseController, render
- Start addition from pylons.decorators import jsonify import simplejson import urllib
- End addition
log = logging.getLogger(__name__)
...
return 'Hello World'
- Start addition @jsonify def json(self): body = urllib.unquote(request.body[:-1]) json = simplejson.loads(body) data = json['params'][0] method = json['method'] if method == 'reverse': data = data[::-1] if method == 'uppercase': data = data.upper() if method == 'lowercase': data = data.lower() return dict(result=data)
- End addition
This comment is not visible to normal users. Undelete comment Comment by dr.mark.lui, Aug 30, 2009
fgfg This comment is not visible to normal users. Undelete comment Comment by robertoedwins, Nov 29, 2009
With pylons-0.9.7 and pyjamas-0.7pre1:
Write: body = urllib.unquote(request.body:-1?) Instead of: body = urllib.unquote(request.body)
Write: jsonu'params'?0? Instead of: json'params'?0?
Write: jsonu'method'? Instead of: json'method'? This comment is not visible to normal users. Undelete comment Comment by robertoedwins, Nov 29, 2009
inline code This comment is not visible to normal users. Undelete comment Comment by robertoedwins, Nov 29, 2009
inline code This comment is not visible to normal users. Undelete comment Comment by robertoedwins, Nov 29, 2009
verbatim code block
This comment is not visible to normal users. Undelete comment Comment by robertoedwins, Nov 29, 2009
With pyjamas-0.7pre1 and pylons-0.9.7:
Change: json = simplejson.loads(body[:-1]) With: json = simplejson.loads(body)
Change: json[u'params'][0] With: json['params'][0]
Change: json['method'][0] With: json[u'method'][0]
Delete comment Comment by robertoedwins, Nov 29, 2009
With pyjamas-0.7pre1 and pylons-0.9.7:
Change: json = simplejson.loads(body[:-1]) With: json = simplejson.loads(body)
Change: json['params'][0] With: json[u'params'][0]
Change: json['method'] With: json[u'method']
This comment is not visible to normal users. Undelete comment Comment by [email protected], Jun 06, 2010
This example (also with adjustments) do not work for me.
Pylons version 0.9.7 Pyjamas is from Debian testing: 0.7+pre2-3
JSONRPCExample give a HTTP 500 error to me.
Here is what I get from Pylons:
File '<string>', line 2 in json File '/home/martin/pyenv/lib/python2.6/site-packages/Pylons-0.9.7-py2.6.egg/pylons/decorators/init.py', line 36 in jsonify
data = func(args, kwargs)
File '/home/martin/pyenv/lib/echo/echo/controllers/echoservice.py', line 28 in json
json = simplejson.loads(body)
File 'build/bdist.linux-x86_64/egg/simplejson/init.py', line 305 in loads File 'build/bdist.linux-x86_64/egg/simplejson/decoder.py', line 329 in decode File 'build/bdist.linux-x86_64/egg/simplejson/decoder.py', line 345 in raw_decode ValueError?: Expecting object: line 1 column 146 (char 146)
LiveHTTP Headers give:
http://localhost:5000/echoservice/json
POST /echoservice/json HTTP/1.1
Host: localhost:5000
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; de; rv:1.9.1.6) Gecko/20091216 Iceweasel/3.5.8 (like Firefox/3.5.8)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,;q=0.7
Keep-Alive: 300
Connection: keep-alive
Content-Type: text/plain; charset=utf-8
Referer: http://localhost:5000/output/JSONRPCExample.mozilla.cache.html
Content-Length: 148
Pragma: no-cache
Cache-Control: no-cache
{"id":2,"method":"echo","params":["{'Test'} [\"String\"]\n\tTest Tab\nTest Newline\n\nafter newline\nLiteral String:\n{'Test'} [
\"String
\"]\n"]}
HTTP/1.0 500 Internal Server Error
Server: PasteWSGIServer/0.5 Python/2.6.4+
Date: Sun, 06 Jun 2010 17:29:58 GMT
Content-Type: text/html
X-Debug-URL: http://localhost:5000/_debug/view/1275845149
Connection: close
Think it must be something with pylons. Can somebody help me out? This comment is not visible to normal users. Undelete comment Comment by cornelis.bos, Jun 16, 2010
Pyjamas with Pylons example for todays pyjamas and Pylons1.0 Install pyjamas¶
Use git or get a git snapshot:
git clone https://github.com/pyjs/pyjs.git
or
wget -O pyjamas.tar.gz 'http://pyjamas.git.sourceforge.net/git/gitweb.cgi?p=pyjamas/pyjamas;a=snapshot;h=HEAD;sf=tgz' tar xzf pyjamas.tar.gz
and then bootstrap and test:
cd pyjamas python bootstrap.py cd examples ./buildall.sh firefox helloworld/output/Hello.html cd ../..
Install pylons¶
wget http://www.pylonshq.com/download/1.0/go-pylons.py python go-pylons.py pylons
Create echo project¶
Activate pylons:
cd /pylons . ./bin/acivate
Create project directories:
paster create --template pylons echo
Copy jsonrpc example:
cd echo/echo/public cp -pr ../../../../pyjamas/examples/jsonrpc/* .
Modify JSONRPCExample.py:
class EchoServicePython(JSONProxy): def __init__(self): JSONProxy.__init__(self, "/echoservice/jsonrpc", ["echo", ...
Build pyjamas client:
../../../../pyjamas/bin/pyjsbuild JSONRPCExample.py
Create controler:
cd ../.. paster controller echoservice
Modify echo/controllers/echoservice.py:
return 'Hello World'
- Start addition def echo(self, data): return data
def reverse(self, data): return data[::-1]
def uppercase(self, data): return data.upper()
def lowercase(self, data): return data.lower()
- End addition
Modify echo/lib/base.py:
from pylons.templating import render_mako as render
- Start addition from pylons import request from pylons.decorators import jsonify import simplejson import urllib
class JSONError(RuntimeError): def __init__(self, code, message, data=None): self.code = code self.message = message self.data = data
def jsonDict(self): return dict(code=self.code, message=self.message, data=self.data)
def __repr__(self): return "JSONError(%s, %r, %r)" % (self.code, self.message, self.data)
def __str__(self): return self.__repr__()
- End addition
class BaseController(WSGIController):
and
return WSGIController.__call__(self, environ, start_response)
- Start addition @jsonify def jsonrpc(self): session = request.environ['beaker.session'] body = urllib.unquote(request.body) json = simplejson.loads(body) id = json['id'] params = json['params'] method = json['method'] try: if not hasattr(self, method): raise JSONError(-32601, "method '%s' not found" % method) if isinstance(params, dict): kwargs = {} for k, v in params.iteritems(): kwargs[str(k)] = v return dict(jsonrpc="2.0", id=id, result=getattr(self, method)(kwargs)) return dict(jsonrpc="2.0", id=id, result=getattr(self, method)(*params)) except JSONError, e: return dict(jsonrpc="2.0", id=id, error=e.jsonDict())
- End addition
Test pylons echoservice¶
Start pylons:
paster serve --reload development.ini
Check echoserivice "helloworld":
http://127.0.0.1:5000/echoservice/index
Test pyjamas echo client:
http://127.0.0.1:5000/output/JSONRPCExample.html
Delete comment Comment by cornelis.bos, Jun 16, 2010
Pyjamas with Pylons example for todays pyjamas and Pylons 1.0 Install pyjamas¶
Use git or get a git snapshot:
git clone https://github.com/pyjs/pyjs.git
or
wget -O pyjamas.tar.gz 'http://pyjamas.git.sourceforge.net/git/gitweb.cgi?p=pyjamas/pyjamas;a=snapshot;h=HEAD;sf=tgz' tar xzf pyjamas.tar.gz
and then bootstrap and test:
cd pyjamas python bootstrap.py cd examples ./buildall.sh firefox helloworld/output/Hello.html cd ../..
Install pylons¶
wget http://www.pylonshq.com/download/1.0/go-pylons.py python go-pylons.py pylons
Create echo project¶
Activate pylons:
cd pylons . ./bin/acivate
Create project directories:
paster create --template pylons echo
Copy jsonrpc example:
cd echo/echo/public cp -pr ../../../../pyjamas/examples/jsonrpc/* .
Modify JSONRPCExample.py:
class EchoServicePython(JSONProxy): def __init__(self): JSONProxy.__init__(self, "/echoservice/jsonrpc", ["echo", ...
Build pyjamas client:
../../../../pyjamas/bin/pyjsbuild JSONRPCExample.py
Create controler:
cd ../.. paster controller echoservice
Modify echo/controllers/echoservice.py:
return 'Hello World'
- Start addition def echo(self, data): return data
def reverse(self, data): return data[::-1]
def uppercase(self, data): return data.upper()
def lowercase(self, data): return data.lower()
- End addition
Modify echo/lib/base.py:
from pylons.templating import render_mako as render
- Start addition from pylons import request from pylons.decorators import jsonify import simplejson import urllib
class JSONError(RuntimeError): def __init__(self, code, message, data=None): self.code = code self.message = message self.data = data
def jsonDict(self): return dict(code=self.code, message=self.message, data=self.data)
def __repr__(self): return "JSONError(%s, %r, %r)" % (self.code, self.message, self.data)
def __str__(self): return self.__repr__()
- End addition
class BaseController(WSGIController):
and
return WSGIController.__call__(self, environ, start_response)
- Start addition @jsonify def jsonrpc(self): session = request.environ['beaker.session'] body = urllib.unquote(request.body) json = simplejson.loads(body) id = json['id'] params = json['params'] method = json['method'] try: if not hasattr(self, method): raise JSONError(-32601, "method '%s' not found" % method) if isinstance(params, dict): kwargs = {} for k, v in params.iteritems(): kwargs[str(k)] = v return dict(jsonrpc="2.0", id=id, result=getattr(self, method)(kwargs)) return dict(jsonrpc="2.0", id=id, result=getattr(self, method)(*params)) except JSONError, e: return dict(jsonrpc="2.0", id=id, error=e.jsonDict())
- End addition
Test pylons echoservice¶
Start pylons:
paster serve --reload development.ini
Check echoserivice "helloworld":
http://127.0.0.1:5000/echoservice/index
Test pyjamas echo client: