View unanswered posts | View active topics It is currently Sat Apr 19, 2014 6:39 pm

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



This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
 Error with first Python Script 
Author Message
User avatar

Joined: Jun 14, 2006
Posts: 500
Post 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)


Fri Apr 27, 2012 11:42 pm
Profile
Site Admin
User avatar

Joined: Jan 27, 2003
Posts: 13210
Location: Texas
Post 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


Sat Apr 28, 2012 9:06 am
Profile WWW
User avatar

Joined: Jun 14, 2006
Posts: 500
Post 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


Sat Apr 28, 2012 10:37 am
Profile
Site Admin
User avatar

Joined: Jan 27, 2003
Posts: 13210
Location: Texas
Post 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


Sat Apr 28, 2012 10:53 am
Profile WWW
Site Admin
User avatar

Joined: Mar 19, 2008
Posts: 8356
Location: Austin, Texas
Post 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)
Image


Sat Apr 28, 2012 11:39 am
Profile WWW
Display posts from previous:  Sort by  
This topic is locked, you cannot edit posts or make further replies.   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.   Template designed by STSoftware.