# Sending commands with Python

**URL:** <https://forum.universal-robots.com/t/sending-commands-with-python/2198>\
**Category:** URScript\
**Created:** [June 28, 2018, 9:02am UTC](https://forum.universal-robots.com/t/sending-commands-with-python/2198 "2018-06-28T09:02:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon82484413](https://avatars.discourse-cdn.com/v4/letter/a/d2c977/32.png) [@anon82484413](https://forum.universal-robots.com/u/anon82484413)\
**Post date:** [June 28, 2018, 9:02am UTC](https://forum.universal-robots.com/t/sending-commands-with-python/2198/1 "2018-06-28T09:02:37Z")

</div>

I am trying to send script commands with Python, and got some of it working

```
import socket
import time

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
s.connect(('192.168.1.99', 30002))

def sendCommand(cmd):
    #cmd = 'def program():\n ' + cmd + '\n end\n'
    cmd = cmd + '\n'
    s.sendall(cmd.encode())

while (1):
    sendCommand('set_digital_out(0, False)')
    time.sleep(1)
    sendCommand('set_digital_out(0, True)')
    time.sleep(1)

```

As this it works, and turns the digital out 0 on and off. But if I try to wrap it in program() instead, nothing happens. Am I sending it wrong? Or does it expect to receive the function different?

I know I don’t need to put it like that for just a single command, but it is in preperation for something more exciting. 🙂

---

<div class="post-metadata">

**Author:** ![anon82484413](https://avatars.discourse-cdn.com/v4/letter/a/d2c977/32.png) [@anon82484413](https://forum.universal-robots.com/u/anon82484413)\
**Post date:** [June 28, 2018, 9:18am UTC](https://forum.universal-robots.com/t/sending-commands-with-python/2198/2 "2018-06-28T09:18:00Z")

</div>

Why is it I always find the problem right after posting here???

The problem was the space in “\n end\n”

```
import socket
import time

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
s.connect(('192.168.1.99', 30002))

def sendCommand(cmd):
    cmd = 'def program():\n ' + cmd + '\nend\n'
    print(cmd)
    s.sendall(cmd.encode())

while (1):
    sendCommand('set_digital_out(0, False)')
    time.sleep(1)
    sendCommand('set_digital_out(0, True)')
    time.sleep(1)

```

This works, and will just toggle digital out 0 on and off.

---

<div class="post-metadata">

**Author:** ![anon69967135](https://avatars.discourse-cdn.com/v4/letter/a/e68b1a/32.png) [@anon69967135](https://forum.universal-robots.com/u/anon69967135)\
**Post date:** [October 20, 2021, 10:58am UTC](https://forum.universal-robots.com/t/sending-commands-with-python/2198/3 "2021-10-20T10:58:56Z")

</div>

Hi,

The python code works great, I am using the URScript manual to send commands to the robot and it is fine

In the other hand, some commands should get an answer from the robot like get\_actual\_joint\_positions(), I tried the s.recv(1024) but I get a lot of binary data, do you have an idea how to format the answer and get the right data ?

Thank you in advance
