Error with first Python Script

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
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
User avatar
bob
Posts: 500
Joined: Wed Jun 14, 2006 7:55 am

Error with first Python Script

Post by bob »

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)
User avatar
matt (support)
Site Admin
Posts: 21542
Joined: Mon Jan 27, 2003 1:17 pm
Location: Texas
Contact:

Re: Error with first Python Script

Post by matt (support) »

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
User avatar
bob
Posts: 500
Joined: Wed Jun 14, 2006 7:55 am

Re: Error with first Python Script

Post by bob »

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
User avatar
matt (support)
Site Admin
Posts: 21542
Joined: Mon Jan 27, 2003 1:17 pm
Location: Texas
Contact:

Re: Error with first Python Script

Post by matt (support) »

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
User avatar
jay (support)
Site Admin
Posts: 19232
Joined: Wed Mar 19, 2008 11:52 am
Location: Austin, Texas
Contact:

Re: Error with first Python Script

Post by jay (support) »

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
Locked

Return to “Extending Indigo with Plugins and Python”