Add Sockets To Django View For Contact With Robot

I am trying to give commands to a robot from a Django site through sockets as this has been recommended as the best possible way to do it. I have been working with Django and sockets in isolation but I have not been able to find an applicable tutorial(only chat apps) on how to integrate the two. If anybody has any experience with this and knows how to add socket to my view any help would be great. (I just need help with the server-side of it, I know how to setup the client)
My Model:

class Robot(models.Model):
    def fetch_choices():
        scope = [REDACTED]
        creds = ServiceAccountCredentials.from_json_keyfile_name("dashboard/Files/creds.json", scope)
        client = gspread.authorize(creds)
        sheet = client.open("tutorial").sheet1
        path_names = []
        path_name_fetch =  sheet.col_values(1)
        for i in path_name_fetch:
            if (i, i) not in path_names:
                path_names.append((i, i))
        return path_names

    name = models.CharField(max_length=200)
    path_options = models.CharField(choices=fetch_choices(), default=fetch_choices(), max_length=100)
    status_choices = [('waiting', 'waiting'), ('running', 'running'), ('stuck', 'stuck')]
    current_status = models.CharField(choices=status_choices, default=status_choices[0], max_length=100)

    def __str__(self):
        return self.name + ' | Current Status: ' + self.current_status

    def get_absolute_url(self):
        return reverse('dashboard-home')

My View:

class RobotDetail(UpdateView):
    model = Robot
    form_class = RobotUpdateForm
    template_name = 'dashboard/robotdetail.html'

My Template:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Dashboard</title>
</head>
<body>
    {% extends 'theblog/base.html' %}
    {% block content %}
        <h1>Dispatcher</h1>
        <form method="POST">
            {% csrf_token %}
            {{ form.as_p}}
            <button class="btn btn-secondary">Dispatch</button>
        </form>
    {% endblock %}
</body>
</html>

Do you need a persistent connection to the robot? Or can your robot function by sending it commands and disconnecting from it? (The answer is going to depend upon your requirements here.)

It can disconnect, it just needs to send the robot 1 string in order to give it some basic instructions and the robot can do the rest.

Are you sending that string through an API (such as an HTTP request or something like that), or is it a raw socket connection?

Either way, the fact that you’re doing it through a view doesn’t matter. Whatever code you use not in a view would be the same code as doing it in the view. The only reason I’m asking about it being an HTTP request is that there are libraries (e.g. requests) that make that process easier - but it doesn’t change the fundamentals.

I don’t know how to do it through an HTTP request though I would be willing to learn how. Right now I am just using pure sockets

That’s up to what the Robot will accept. Does the Robot accept an HTTP request for submitting the commands? If not, then this doesn’t apply.

So that’s the code that goes into your view.

It doesn’t currently accept HTTP requests. My question is what the sockets code for my view should look like, I haven’t found any applicable documentation or tutorials though I put that down to not knowing what to look for. Do you have any recommendations?

The fact that this is being done in a view doesn’t matter - it doesn’t change the code you need to use. Whatever code you would use outside of Django is the code you would put in the view.
At this point, it’s just Python. See the Socket programming how-to guide.

My question is how to do it with a class based function, that is what is confusing me

You do it the same way you would add any processing that needs to be done within a view.

Do not conflate the two items.

  1. You need to identify where in the sequence of events within a CBV you want this work to be done. (It doesn’t matter that you’re opening a socket, it could be anything. If it helps, think about it as if you just wanted to print “Hello Robot” from within the view.)

  2. You need to write the code to open the socket, send the data, and close the socket. The fact that you’re going to add this code into a CBV is irrelevant. You should be able to run this code completely outside of Django and have it work.

These are two separate issues. Do not think that either is dependent upon the other.

I have the code written that I want to use, I just don’t know how to make it work when I click the form submit button for a class based view

Are you using one of the generic CBVs (if so, which one) or is this a user designed CBV?

If you’re using one of the generic CBVs, there are two resources that I suggest people become familiar with, Classy ClassBased Views and the CBV diagrams pages.

Beyond that, we’d need to see the CBV being used to be able to tell you how your code could be integrated.

I am using a generic class based view, and the form is on an update view. Is there any way I could do it with that? If not what should I look back with the classy cbv?

Are you actually updating a table in addition to wanting to send this command to the robot?

Yes, I am setting the designated path for the robot to follow and then need to tell the robot that it is the designated path

Ok, then overriding the form_valid method would be my first suggestion.

I wouldn’t suggest calling super because of the sequence that things happen. But I would save the form, send the data to the robot, and then send the redirect response, all within my custom form_valid.

With classy cbv or just normal django?

??? Classy CBV is a documentation site - it explains how the CBVs work. It’s not code that changes anything.

oh, I hadn’t taken a look and I thought that it was a separate import. My bad. So how would I ovveride the form_valid method to do this?

Standard python - how do you override any method in a child class?

https://docs.python.org/3/tutorial/classes.html