Question

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 REQUEST

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/

NOTE

With the curl request i am able to POST JSON data to django db,but how to POST data and RECEIVE success RESPONSE in Client side, that DATA is POSTED @ SERVER DB.

I want to create a python script which will be running in linux CLient and when it runs, it will Extract and POST its SYSTEM Information(mac_id,hostname,host_ip) to django Server and get a response that its Data is submitted.

Can Anybody Help me in creating the Python script(client.py)which will do the above communication between Client and server USING REST/JSON communication Protocol.

for now i tried tastypie as you can see..

Code will be Highly welcomed rather than suggestion(which is also Welcomed)

Hope my Question is Clear and if anything else required information do ask me..

Awaiting for the solution soon..:-)

Était-ce utile?

La solution

I always prefer to use Advance REST Client while developing REST Full API in Django.

Hope this would be helpful to you. :)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top