Python中的简单RESTFUL客户端/服务器示例?

时间:2022-09-06 21:32:29

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

是否有一个在线资源,显示如何编写一个简单(但强大)的RESTFUL服务器/客户端(最好是带有身份验证),用Python编写?

The objective is to be able to write my own lightweight RESTFUL services without being encumbered by an entire web framework. Having said that, if there is a way to do this (i.e. write RESFUL services) in a lightweight manner using Django, I'd be equally interested.

目标是能够编写自己的轻量级RESTFUL服务,而不受整个Web框架的阻碍。话虽如此,如果有一种方法可以使用Django以轻量级方式执行此操作(即编写RESFUL服务),我同样感兴趣。

Actually, coming to thing of it, I may even PREFER a Django based solution (provided its lightweight enough - i.e. does not bring the whole framework into play), since I will be able to take advantage of only the components I need, in order to implement better security/access to the services.

实际上,为了解决这个问题,我甚至可以选择基于Django的解决方案(提供足够的轻量级 - 即不会使整个框架发挥作用),因为我将能够仅利用我需要的组件,按顺序实现更好的安全/访问服务。

2 个解决方案

#1


5  

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

好吧,首先你可以使用django-piston,正如@Tudorizer已经提到过的那样。

But then again, as I see it (and I might be wrong!), REST is more of a set of design guidelines, rather than a concrete API. What it essentially says is that the interaction with your service should not be based on 'things you can do' (typical RPC-style methods), but rather 'things, you can act on in predictable ways, organized in a certain way' (the 'resource' entity and http verbs).

但是,正如我所看到的那样(我可能错了!),REST更多的是一套设计指南,而不是具体的API。它本质上说的是,与您的服务的交互不应该基于“您可以做的事情”(典型的RPC风格的方法),而是“事物,您可以以可预测的方式采取行动,以某种方式组织”( '资源'实体和http动词)。

That being said, you don't need anything extra to write REST-style services using django.

话虽这么说,你不需要任何额外的东西来使用django编写REST风格的服务。

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)
    ...

... and so on.

... 等等。

What matters is how your service is exposed to its user, not the library/toolkit/framework it uses.

重要的是您的服务如何向其用户公开,而不是它使用的库/工具包/框架。

#2


2  

This one looks promising. http://parand.com/say/index.php/2009/04/30/django-piston-rest-framework-for-django/ I've used it before and it's pretty nifty. Having said that, it doesn't seem maintained recently.

这看起来很有希望。 http://parand.com/say/index.php/2009/04/30/django-piston-rest-framework-for-django/我之前使用过它,非常漂亮。话虽如此,最近似乎并没有保持。

#1


5  

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

好吧,首先你可以使用django-piston,正如@Tudorizer已经提到过的那样。

But then again, as I see it (and I might be wrong!), REST is more of a set of design guidelines, rather than a concrete API. What it essentially says is that the interaction with your service should not be based on 'things you can do' (typical RPC-style methods), but rather 'things, you can act on in predictable ways, organized in a certain way' (the 'resource' entity and http verbs).

但是,正如我所看到的那样(我可能错了!),REST更多的是一套设计指南,而不是具体的API。它本质上说的是,与您的服务的交互不应该基于“您可以做的事情”(典型的RPC风格的方法),而是“事物,您可以以可预测的方式采取行动,以某种方式组织”( '资源'实体和http动词)。

That being said, you don't need anything extra to write REST-style services using django.

话虽这么说,你不需要任何额外的东西来使用django编写REST风格的服务。

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)
    ...

... and so on.

... 等等。

What matters is how your service is exposed to its user, not the library/toolkit/framework it uses.

重要的是您的服务如何向其用户公开,而不是它使用的库/工具包/框架。

#2


2  

This one looks promising. http://parand.com/say/index.php/2009/04/30/django-piston-rest-framework-for-django/ I've used it before and it's pretty nifty. Having said that, it doesn't seem maintained recently.

这看起来很有希望。 http://parand.com/say/index.php/2009/04/30/django-piston-rest-framework-for-django/我之前使用过它,非常漂亮。话虽如此,最近似乎并没有保持。