【发布时间】:2014-03-22 15:27:53
【问题描述】:
Models.py::
class Device(models.Model):
mac_id = models.CharField(max_length=100, unique=True)
hostname = models.CharField(max_length=100)
host_ip = models.GenericIPAddressField(unique=True)
api.py::
from tastypie.resources import ModelResource
from tastypie.authentication import BasicAuthentication
from tastypie.authorization import Authorization
from .models import *
class DeviceResource(ModelResource):
class Meta:
queryset = Device.objects.all()
allowed_methods = ['post','get']
resource_name = "devices"
authentication = BasicAuthentication()
authorization = Authorization()
卷曲请求
curl -u user:passwd --dump-header - -H "Content-Type: application/json" --data '{"device_name": "sdf-321", "hostname": "sdf", "mac_id": "fsdfa-321", "host_ip": "127.0.0.160"}' -X POST localhost:8000/device/api/v1/devices/
注意
通过 curl 请求,我可以将 JSON 数据发布到 django db,但是如何在客户端发布数据并接收成功响应,该数据是 POSTED @ SERVER DB。
我想创建一个将在 linux 客户端中运行的 python 脚本,当它运行时,它将提取并 POST 其系统信息(mac_id,主机名,host_ip)到 django 服务器并获得其数据已提交的响应。
谁能帮我创建 Python 脚本(client.py),它将使用 REST/JSON 通信协议在客户端和服务器之间进行上述通信。
现在我尝试了如你所见的美味派..
代码将受到高度欢迎,而不是建议(这也是受欢迎的)
希望我的问题很清楚,如果还有其他需要的信息,请询问我..
等待尽快解决..:-)
【问题讨论】:
标签: django python-2.7 client-server django-rest-framework tastypie