-1

I have a Django app created using Django rest framework. Below is the configuration that my setup is using:

Django 1.9 mongoDB as backend gunicorn nginx

Now I have created an API to enter data in DB and retrieve it using REST. I have test it via postman and it is working fine. We have a firmware which is consuming those APIs and that team want to use SSL socket connection instead of REST API.

I am new in SSL Socket connection and I am not able to find anything on internet that can help me on this. I know it is possible to create socket in Python but I am not able to understand how to use it in Django app to Read/Write data in mongoDB.

Any guidance will be very helpful.

TO READER : I understand you may want to close this question but please put up a remark on how to get guidance on this as SO is the biggest portal for putting up questions.

EDIT 1 : I am adding the code of my serializer.py API.

from rest_framework import serializers

class SaveUserLogs(serializers.Serializer):
    token = serializers.CharField(label=_("Token"))
    device_ip = serializers.CharField(label=_('Device IP'))
    device_name = serializers.CharField(label=_('Device Name'))
    device_os = serializers.CharField(label=_('Device OS'))
    device_macid = serializers.CharField(label=_('Device MAC ID'))
    dest_port = serializers.CharField(label=_('Device Port'))
    conn_type = serializers.CharField(label=_('Connection Type'))
    date = serializers.CharField(label=_('modified date'))

    def validate(self, attrs):
        device_macid = attrs.get('device_macid')
        token = attrs.get('token')
        if device_macid:
            tokendetails = validate_token(token)
            if not tokendetails:
                msg = _('Invalid token.')
                raise serializers.ValidationError(msg)
            else:

                userdetails = tokendetails.user
                if userdetails.check_mac_id(device_macid):
                    all_logs = UserLog.objects.all().order_by("-id")
                    log_id = 1

                    if all_logs:
                        getid = all_logs[0].id
                        log_id = getid + 1

                    new_log = UserLog(
                        id=log_id,
                        device_ip=attrs.get('device_ip'),
                        device_name=attrs.get('device_name'),
                        device_os=attrs.get('device_os'),
                        device_macid=attrs.get('device_macid').upper(),
                        dest_port=attrs.get('dest_port'),
                        conn_type=attrs.get('conn_type'),
                    )
                    new_log.save()

                    print("saving log", log_id)
                else:
                    msg = _('Invalid MAC ID')
                    raise serializers.ValidationError(msg)
        else:
            msg = _('Must include "MAC ID".')
            raise serializers.ValidationError(msg)

        attrs['mac_id'] = userdetails.mac_id
        return attrs
Pratibha
  • 1,730
  • 7
  • 27
  • 46

1 Answers1

0

It looks like you require socket library provided with python. But using this is not recommended since you will have to take care of lot of low level networking stuff and your program will get complicated.

You should instead keep your REST api and run your nginx server on https. You can then write the code on firmware to send and receive data from the server using https.

If for some reason you don't want to use https then you should use requests library to write your server.

vishal
  • 1,081
  • 2
  • 10
  • 27
  • I have to connect to firmware hence have to use SSL Socket – Pratibha Apr 16 '18 at 15:33
  • I have to connect to firmware hence have to use SSL Socket as per the requirement.this is for sending data to firmware – Pratibha Apr 16 '18 at 15:47
  • [http://grantcurell.com/2017/03/10/a-simple-ssl-client-and-server-in-python/] . This link should get you started with socket programming. [https://stackoverflow.com/a/18760222/8370670] This explains how to access django models from a python script. – vishal Apr 17 '18 at 05:11
  • We need something which is push based as HTTPS is pull based. – Pratibha Apr 20 '18 at 14:06