A simple RESTFUL client / server example in Python?

Is there an online resource that shows how to write a simple (but reliable) RESTFUL server / client (preferably with authentication) written in Python?

The goal is to be able to write my own lightweight RESTFUL services without being burdened with the entire web map. Having said that, if there is a way to do this (i.e. write RESFUL services) in an easy way using Django, I would be equally interested.

Actually, having come to this, I can even PREFER a Django-based solution (provided that it is quite lightweight, that is, it doesn’t bring the whole game into the game), since I can only use the components that I need to provide the best security / access to services.

+5
source share
2 answers

Well, first of all you can use django-piston , as @Tudorizer already mentioned.

But then again, as I see it (and I could be wrong!), REST is more of a set of design guidelines, not a specific API. In essence, this means that interaction with your service should not be based on “things you can do” (typical RPC style methods), but rather “things you can act in predictable ways organized in a certain way” (resource object and http verbs).

You don’t need to do anything extra to write REST-style services with django.

Consider the following:

# urlconf
from django.conf.urls.defaults import patterns, url
urlpatterns = patterns('',
    url(r'^tickets$', 'myapp.views.tickets', name='tickets'),
    url(r'^ticket/(?P<id>\d+)$', 'myapp.views.tickets', name='ticket'),
    url(r'^ticket$', 'myapp.views.tickets', name='ticket'),
)

# views
def tickets(request):
    tickets = Ticket.objects.all()
    return render_to_response('tickets.html', {'tickets':tickets})

def ticket(request, id=None):
    if id is not None:
        ticket = get_object_or_404(Ticket, id=id)
    if request.method == 'POST':
        # create or update ticket here
    else:
        # just render the ticket (GET)
    ...

... etc.

, , //.

+5

All Articles