HTTP Authenticated Requests

Well I need a bit of help I have come to find out after bring down our system that apparently the python library for urllib2 is buggy as all get out. Apparently they can’t figure out how to fix it so it doesn’t keep locking connections and never closes them.

So with that said does anyone know how I can make an authenticated connection to url in a script?
The following works but over time will stop working because the connections don’t get closed. From everything I have found this appears to be know error but no real work around that works.

[code]import urllib2
import sys, system

username = ‘usr’
password = ‘pwd’

auth_handler = urllib2.HTTPPasswordMgrWithDefaultRealm()
auth_handler.add_password(None, uri=‘http://xxx.xxx.xxx.xxx’,user=username,passwd=password)
handler = urllib2.HTTPBasicAuthHandler(auth_handler)

opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)

import xml.etree.ElementTree as ET
response=urllib2.urlopen(‘http://xxx.xxx.xxx.xxx/rest/vars/get/1’)
mytree = ET.parse(response)
response.close()
for elem in mytree.findall(‘var’):
value = elem.find(‘val’).text
tagName = elem.attrib.get(‘id’)
if system.tag.exists(“ISY/Vars/”+tagName):
system.tag.write(“ISY/Vars/”+tagName,value)[/code]

I would just use the Java libraries instead.

[code]from java.net import URL
from org.apache.commons.io import IOUtils
import base64
username = “something”
password= “some other thing”
authParam = “Basic " + base64.b64encode(”%s:%s"%(username,password))
connection = URL(“https://something/whatever”).openConnection()
connection.setDoInput(1)
connection.setRequestProperty(“Authorization”, authParam)
connection.connect()

if connection.getResponseCode()==200:
response = IOUtils.toString(connection.getInputStream())[/code]

Thanks Carl I give that a try and see how it does.

Ok good luck.