Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
125 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...