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=‘’,user=username,passwd=password)
handler = urllib2.HTTPBasicAuthHandler(auth_handler)

opener = urllib2.build_opener(handler)

import xml.etree.ElementTree as ET
mytree = ET.parse(response)
for elem in mytree.findall(‘var’):
value = elem.find(‘val’).text
tagName = elem.attrib.get(‘id’)
if system.tag.exists(“ISY/Vars/”+tagName):

I would just use the Java libraries instead.

[code]from import URL
from 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.setRequestProperty(“Authorization”, authParam)

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.