Skip to main content
 首页 » 编程设计

python之使用 django-piston 进行基本的 http 身份验证

2024年09月03日26lvdongjie

我是新手。我在官方网站上看到了代码片段(粘贴在下面)。 问题是如何将其部署到服务器?在哪里设置用户名和密码凭据?在 Apache 的 httpd.conf 文件中?


from django.conf.urls.defaults import * 
from piston.resource import Resource 
from piston.authentication import HttpBasicAuthentication 
 
from myapp.handlers import BlogPostHandler, ArbitraryDataHandler 
 
auth = HttpBasicAuthentication(realm="My Realm") 
ad = { 'authentication': auth } 
 
blogpost_resource = Resource(handler=BlogPostHandler, **ad) 
arbitrary_resource = Resource(handler=ArbitraryDataHandler, **ad) 
 
urlpatterns += patterns('', 
    url(r'^posts/(?P<post_slug>[^/]+)/$', blogpost_resource),  
    url(r'^other/(?P<username>[^/]+)/(?P<data>.+)/$', arbitrary_resource),  
) 

请您参考如下方法:

默认 piston.authenticate.HttpBasicAuthentication 使用 django.contrib.auth.authenticate检查凭据。

换句话说:您只需创建普通的 Django 用户即可“设置用户名和密码凭据”。