Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X
can someone point me to a sample python script that sets thingworx properties (through Rest API?)
I am a thingworx starter and looking for some reference material on how to use python to communicate with thingworx server to get me started.
Hi Bhaskar,
The Requests HTTP library for Python makes interacting with the ThingWorx API a breeze. The following sample code uses this library to set two Properties (stringProperty and numberProperty) defined on a Thing:
import requests
import json
url = 'https://localhost/Thingworx'
headers = { 'Content-Type': 'application/json', 'appKey': 'ed18a4a1-c6d5-46b3-ace8-b55d148e2384' }
payload = { 'stringProperty': 'ThingWorx', 'numberProperty': 14.3 }
response = requests.put(url + '/Things/ExampleThing/Properties/*', headers=headers, json=payload, verify=False)
Since Tomcat is configured with a self-signed certificate, False must be passed as an argument to the verify parameter. In a production environment, you will want to verify the SSL certificate.
You can find information about the ThingWorx API here.
Thanks a LOT Adam for the detailed reply.
Can you please also provide information on how to “get” thingworx property value using python?
Thanks in advance,
Bhaskar Walke
You're welcome : )
To retrieve a Property value, you need to modify the headers:
headers = { 'Accept': 'application/json', 'appKey': 'ed18a4a1-c6d5-46b3-ace8-b55d148e2384' }
and use the method that corresponds to a GET:
response = requests.get(url + '/Things/ExampleThing/Properties/stringProperty', headers=headers, verify=False)
Hi Adam,
I am getting SSL Error to send request from the python.
I tried to go through the link you provided, but i did not the solution. Can you give some solution?
I have tried passing the PEM file also for SSL error, but i could not able to make it. .
headers = { 'Accept': 'application/json', 'appKey': '4ddd126f-c719-431a-9b42-36b75eeebc45' }
response = requests.post(url + '/Things/TestDevice8/Properties/BatteryVoltage', headers=headers, verify="C:\All_Builds\PythonScripts\URL_Test_Copy\ems_get_connected.pem")
raise SSLError(e, request=request)
requests.exceptions.SSLError: [Errno 8] _ssl.c:504: EOF occurred in violation of protocol