简体   繁体   中英

Send data from Django to another server

I have an already existing Django app. I would like to add a system that sends data from my Django application to another Python application hosted on another server, so that the Python application receives data from the Django App in json format, possibly.

So for example, i would need to create a view that every tot seconds sends the data from a DB table to this application, or when a form is hit, the data is sent to this external application.

How can i do this? Is there an example for this particular matter? I don't know what tools i'd need to use to create this system, i only know that i would need to use Celery to perform asynchronous tasks, but nothing else; should i use Webhooks maybe? Or Django channels?

Edit: adding some more context: I have my Django client. Then i have one or two Python applications running on another server. On my Django client i have some forms. Once the form is submitted, the data is saved on the db, but i also want this data to be sent instantly to my Python applications. The Python applications should receive the data from Django in Json format and perform some tasks according to the values submitted by users. Then the application should send a response to Django.

Come on! I'll call your Django app here "DjangoApp" and your Python apps, in Flask or another framework by "OtherApp" .

First as you predicted you will need a framework that is capable of performin g tasks, the new **Django 3.0 allows this, but I haven't used it yet ... I will pass on to you something that you are using and fully functional with Django 2.8 and Python 3.8**.

On your DjangoApp server you will need to structure the communication well with your Celery, let's leave the tasks to him. You can read Celery Docs and this post , its very ok to make this architecture.

Regardless of how your form or Django App looks, when you want it to activate a task in celery, it is basically the function to transmit data but in the background.

from .tasks import send_data

...
form.save()
# Create a function within the form to get the data the way you want it
# or do it the way you want.
values = form.new_function_serializedata()
send_data.delay(values) # [CALL CELERY TASKS][1]
...

Read tooCALL CELERY TASKS

In all your other applications you will need to have a POST route to receive and serialize this data, do this with lightweight frameworks like Pyramid

This way, every time a form is submitted, you will have this data sent to the server within the send_data function.

In my experience, but not knowing much about your problem I would use a similar architecture but using Celery Beat .

CELERY_BEAT_SCHEDULE = {
    'send_data': {
        'task': 'your_app.tasks.send_data',
        'schedule': crontab(), # CONFIGURE YOUR CRON
    },
}

Not only is the above code added, but it is something like that.

Within your models I would create one field for sent . And every 2 seconds, 10 seconds .. as long as I wish I would filter all objects with sent = false, and pass all objects for the send_data task.

I don't know if you got confused, that's a lot to explain. But I hope I can help and answer your questions.

Yeah, webhook would be one of the options, but there are other options available too.

-> You can use Rest Apis to send data from one app to another. but In their case, you need to think about synchronization. That depends on your requirement, If you don't want data in synchronize manner then you may use RabbiMq or other async tools. Just push your rest API request in Rabbitmq and Rabbitmq will handle.

import requests
from django import http

def view(request):
    url = 'python.app.com'  # replace with other python app url or ip
    request_data = {'key': 'value'}  # replace with data to be sent to other app
    response = requests.post(url, json=request_data)
    response_data = response.json()  # data returned by other app
    return http.JsonResponse(response_data)

This is an example of a function based view that uses the requests library to hit an external service. The request lib takes care of encoding/decoding your data to/from json.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM