Django JSON_RPC doesn't work - python

i want to create a simple Django(1.3) project that uses JSON-RPC. i use this implementation:
django-json-rpc
and this is my project files:
urls.py:
from django.conf.urls.defaults import patterns, include, url
from myapp.views import *
from jsonrpc import jsonrpc_site
urlpatterns = patterns('',
url(r'^json/browse/', 'jsonrpc.views.browse', name="jsonrpc_browser"),
url(r'^json/', jsonrpc_site.dispatch, name="jsonrpc_mountpoint"),
(r'^json/(?P<method>[a-zA-Z0-9.]+)$', jsonrpc_site.dispatch),
)
views.py:
from jsonrpc import jsonrpc_method
#jsonrpc_method('sayHello')
def hello(request, name='Lester'):
return "Hello %s" % name
when i test this code in JSON-RPC Browser(included with the library) it doesn't work. whe is want to add this import in the shell:
from jsonrpc.proxy import ServiceProxy
i get the response like this:
Error:
what's the problem here? it seems a simple process but it doesn't work for me.

i found the solution. in fact json-rpc works but in JSON-RPC Browser i have to treat with some difference than regular way. according to here, we should initialize and call json-rpc methods like this:
from jsonrpc.proxy import ServiceProxy
s = ServiceProxy('http://localhost:8080/json/')
s.myapp.sayHello('Sam')
but it's not true! this method is correct when we use it in django shell or in our main code! in JSON-RPC Browser we just need to call our method like this:
jsonrpc.sayHello('sam')
just that!
thanks to all.

Related

Redirect hostname/endpoint to api.hostname/endpoint in django

I have my api built with this pattern: api.hostname/endpoint.
However there is a plugin to my app that uses hostname/endpoint pattern.
I would like to solve it on the backend side by adding redirection to api.hostname/endpoint.
I tried to experiment with adding urls or paths to urlpatterns, but it didn't help me.
How can I achieve it? Any ideas?
Regards,
Maciej.
You can use urllib
import urllib.parse
url = "https://hostname/endpoint"
split_url = urllib.parse.urlsplit(url)
result = f"{split_url.scheme}://api.{split_url.hostname}/{split_url.endpoint}"
print(result)
>> "https://api.hostname/endpoint"

How to use route_url method in ApplicationCreated event

I subscribed on ApplicationCreated and I need to get same urls in my web app.
In views I can to use route_url method to get urls.
But how to get url in ApplicationCreated event?
For example in view I can use this code:
from pyramid.view import view_config
#view_config(route_name="home")
def home(request):
print request.route_url('home')
Result in console:
http://example.com/
How I can use same code in this case:
from pyramid.events import ApplicationCreated, subscriber
#subscriber(ApplicationCreated)
def app_start(event):
print ????.route_url('home') # how to get access to route_url
I think it's no way. You are trying to use request-required method in event where no request yet. See sourcecode https://github.com/Pylons/pyramid/blob/master/pyramid/config/init.py#L995
W/o request you can get just route object:
home = app.routes_mapper.get_route('home')
print(home.path)
Example with custom generate URL:
delete = app.routes_mapper.get_route('pyramid_sacrud_delete')
print(delete.path) # 'admin/{table}/delete/*pk'
delete.generate({'table': 'foo', 'pk': ['id',2,'id2',3]}) # '/admin/foo/delete/id/2/id2/3'
Your example:
from pyramid.events import ApplicationCreated, subscriber
#subscriber(ApplicationCreated)
def app_start(event):
print event['app'].app.routes_mapper.get_route('home')

Django: Improperly configured url: unbalanced parenthesis

I'm developing a website ina VM on my Mac which then gets deployed to a remote UAT server. The VM is set up with the same OS and software stack as the UAT and live servers. I'm getting the following error when attempting to access the UAT version of my website:
"^accounts/update-user-group/(?P<pk>\d" is not a valid regular expression: unbalanced parenthesis
On first look it seems pretty obvious what's wrong: The given URL pattern is incomplete. However, my urls.py file has the correct full url:
# -*- coding: utf-8 -*-
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.conf.urls import patterns, url
from views import UserGroupList, UserGroupDetail
from views import UserGroupCreate, UserGroupUpdate, UserGroupDelete
from views import UserDeletedGroups, RecoverDeletedGroup
urlpatterns = patterns('',
url(_(r'^accounts/create-user-group/$'), UserGroupCreate.as_view(), name='user_group_create'),
url(_(r'^accounts/update-user-group/(?P<pk>\d+)/$'), UserGroupUpdate.as_view(), name='user_group_update'),
url(_(r'^delete/(?P<pk>\d+)/$'), UserGroupDelete.as_view(), name='user_group_delete'),
url(_(r'^accounts/user-group-deleted/$'), UserDeletedGroups.as_view(), name='user_group_deleted_list'),
url(_(r'^recover/(?P<pk>\d+)/$'), RecoverDeletedGroup.as_view(), name='user_group_recover_deleted'),
url(_(r'^accounts/user-group-details/(?P<pk>\d+)/$'), UserGroupDetail.as_view(), name='user_group_detail'),
url(_(r'^accounts/user-group-list/$'), UserGroupList.as_view(), name='user_group_list'),
)
So the error seems to be getting generated by the second regex in the urlpatterns. However, If I change the regex to this:
url(_(r'^accounts/update-user-group/(?P<pk>[\d]+)/$'), UserGroupUpdate.as_view(), name='user_group_update'),
Then the error moves on to the next line. All I've done here is add square brackets round the \d
for the pk argument. How can this simple difference be the cause of the error? And why would it only be happening in my UAT environment and not local development?
Daniels question pointed me in the right direction for this. The translation for the problem URL in my PO file was incomplete so ensuring that was correct sorted my problem.

How to get current domain name with Python/GAE?

Current url is
http://myapp.appspot.com/something/<user-id>
or
http://127.0.0.1:8080/something/<user-id>
How in my python code I can get http://myapp.appspot.com/ or http://127.0.0.1:8080/?
This is need for dynamic links generation, for ex., to http://myapp.appspot.com/somethingelse.
self.request.path returns the whole path.
self.request.host_url
I think you want app_identity.get_default_version_hostname().
If an app is served from a custom domain, it may be necessary to
retrieve the entire hostname component. You can do this using the
app_identity.get_default_version_hostname() method.
This code:
logging.info(app_identity.get_default_version_hostname())
prints localhost:8080 on the development server.
If self.request.path returns the whole path, can't you just do:
import urlparse
def get_domain(url):
return urlparse.urlparse(url).netloc
>>> get_domain("http://myapp.appspot.com/something/")
'myapp.appspot.com'

web.py url handling: multiple subapplication redirection

When using web.py framework. you can redirect an url to a subapplication.
e.g (code.py):
import web
import subapp1
urls = (
"/sub1", subapp1.app,
"/(.*)", "index"
)
....
this is really straight forward.
However, when writing subapp1.py which has its own url handlers, if i want to re-route some url, say '/sub2', to another subapplication (subapp2), i am failing.
Formerly in subapp1.py
import web
import subapp2
urls = (
"/sub2", subapp2.app,
"/(.*)", "some_local_class"
)
....
GET request to "/sub1/sub2/", is handled by "some_local_class" in supapp1.py. But i need this url is re-routed to subapp2.py.
Is there anything i am missing? Or may be this is not recommended url handling methodology in web.py?
After some trial-error, found that there is nothing wrong with web.py and rerouting from subapp to another subapp. It is all working perfectly.
What is wrong is my method. Do not try to create a subapp in package's init.py file. At least when i moved subapp to its own module, it all worked well.

Categories

Resources