Hello Everyone,
I am using the requests module in python scripting for getting the real time weather data based on the location.
I am getting the error while running my script. I attached the image file here kindly help me to solve this error
# Create the JythonHttpClient.
client = system.net.httpClient()
# Sent a GET request.
response = client.get("https://httpbin.org/get", params={"a": 1, "b": 2})
# Validate the response.
if response.good:
# Do something with the response
print response.json['args']['a']
One thing to note in your script is you use an os.environ['weather']. All perspective scripts run in the gateway so this is always telling your Ignition Gateway to get the system variable weather from the gateway computer - this does not change user to user, so I would suggest either hardcoding it into a gateway tag and referencing that, or if you want it to be different user to user, use a session variable.
Whatever is going on here with the codec registry error is ultimately a Jython bug. Migrating to system.net.httpClient is going to be far less headache than trying to maintain an increasingly out-of-date version of requests over time.
@Baskar_Arumugam A general rule of thumb to follow is to look first is there is some system.* implementation of what you need to do before trying to use a jython library.
Jython has some randomly interspersed issues in various packages, so it is much safer (and probably faster, definitely more maintainable) to use Ignition’s implementation whenever possible.
How you guys ensuring if the connection was failed?
My scenario is , I am connecting to third party application using syste,.net.httpClient, if response good everything is ok, but lost connection of third party system , how to handle ? Below code is not working
try:
client = system.net.httpClient()
response = client.get("http://192.168.43.75:5000/data")
if response.good:
system.perspective.openPopup("testing", "popup1")
else:
system.util.getLogger("FlaskCheck").info("Received response but status not OK")
except Exception as e:
system.util.getLogger("FlaskCheck").error("Error while connecting to Flask server: " + str(e))
Jython's Exception cannot catch Java exceptions, as they are not in the same class hierarchy. Use another except Throwable, t: clause for those. Add from java.lang import Throwable outside that function.