How to connect DRL studio and python via sockets

To communicate between DRL Studio and pyhton you can use a socket connection. This how-to explains how you can send a message from python to DRL studio. This is not a constant socket connection. A funtion returns a value and that value gets send once to the robot.

Code in pyhton:

HOST = '0.0.0.0'         
PORT = 12345

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()
    server_socket, address = s.accept()
    x = quality_control()			# Put your own function here
    server_socket.send(str(x).encode())				# This sends the value you return from your function to the robot

Code in DRL Studio:

from DRCF import *
import socket

IP = "192.168.137.69"			# This should be the IP adress of your own laptop
PORT = 12345

socket = client_socket_open(IP, PORT)

resp, data = client_socket_read(socket, length=4, timeout=60)
decoded_string = data.decode('utf-8')
received_number = int(decoded_string)
tp_log(str(received_number))			# Prints the sent message

Make sure to first run the python code before you run the code in DRL Studio.