Error with first Python Script

Forum rules

This is a legacy forum which is locked for new topics. New topics should be started in one of the other forums under Extending Indigo

Posted on
Fri Apr 27, 2012 11:42 pm
bob offline
User avatar
Posts: 500
Joined: Jun 14, 2006

Error with first Python Script

I am getting this sometimes in my log, can you offer any suggestions?

2012-04-27 18:33:46 Script Error tempalert.py: <urlopen error (60, 'Operation timed out')>
2012-04-27 18:33:46 Script Error Exception Traceback (most recent call shown last):

tempalert.py, line 9, at top level
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/urllib2.py", line 124, in urlopen
return _opener.open(url, data)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/urllib2.py", line 381, in open
response = self._open(req, data)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/urllib2.py", line 399, in _open
'_open', req)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/urllib2.py", line 360, in _call_chain
result = func(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/urllib2.py", line 1118, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/urllib2.py", line 1093, in do_open
raise URLError(err)
URLError: <urlopen error (60, 'Operation timed out')>


This is my script;

Code: Select all
import urllib2
import xml.etree.cElementTree as ET

#make a variable to hold the name of the file that was written to
file_name = "/tmp/xmlfeed.rb"

#location of temp alert file
url = "http://10.0.1.2/xmlfeed.rb"
response = urllib2.urlopen(url)

#open a local file for writing
local_file = open("/tmp/xmlfeed.rb", "w")

#read from request while writing to file
local_file.write(response.read())
local_file.close()

#location of the file to be parsed
content = ET.ElementTree(file="/tmp/xmlfeed.rb")

ports = content.find("ports")

#get all the port elements
for port in ports.findall('port'):

    if port.get('name') == 'Port 1':
        currentTemp = port.find("condition/currentReading")
        indigo.variable.updateValue("tAlertTemp_", currentTemp.text)

    elif port.get('name') == 'Port 2':

        currentTemp = port.find("condition/currentReading")
        indigo.variable.updateValue("tAlertTemp2_", currentTemp.text)

Posted on
Sat Apr 28, 2012 9:06 am
matt (support) offline
Site Admin
User avatar
Posts: 21411
Joined: Jan 27, 2003
Location: Texas

Re: Error with first Python Script

The error is saying that the urllib open function is failing because of a timeout. On your Indigo Server Mac, what happens when you access this URL from Safari:

http://10.0.1.2/xmlfeed.rb

If it works some times (and the script works some times), then the problem is likely that you are executing the script more frequently than whatever XML server that is can handle it, so it doesn't respond.

Image

Posted on
Sat Apr 28, 2012 10:37 am
bob offline
User avatar
Posts: 500
Joined: Jun 14, 2006

Re: Error with first Python Script

Matt,

I can access the http://10.0.1.2/xmlfeed.rb just fine, I never had this problem with the Applescript. I don't know enough about Python yet to know if there is something in the Python script I can change. For now I'll lower the time period for running the script.

If there is anything I can do to improve the script please let me know.

thanks,

bob

Posted on
Sat Apr 28, 2012 10:53 am
matt (support) offline
Site Admin
User avatar
Posts: 21411
Joined: Jan 27, 2003
Location: Texas

Re: Error with first Python Script

Here are the docs on urllib2:

http://docs.python.org/library/urllib2.html

Note that urlopen has an optional timeout argument. You might just need to increase that.

Image

Posted on
Sat Apr 28, 2012 11:39 am
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Error with first Python Script

Unfortunately the timeout option to urlopen was introduced after Python 2.5 so you can't use that. You can however use the socket.setdefaulttimeout method at the top of the script and that should set the timeout for any network communications for the rest of the script. Just do an "import socket" at the top of the script with the other imports than before you do any URL operations do the socket.setdefaulttimeout(SECONDS) to increase the network timeout.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 0 guests