# HTTP Authenticated Requests

**URL:** <https://forum.inductiveautomation.com/t/http-authenticated-requests/6299>\
**Category:** Ignition\
**Created:** [April 22, 2013, 8:19pm UTC](https://forum.inductiveautomation.com/t/http-authenticated-requests/6299 "2013-04-22T20:19:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gadgetguru69](https://avatars.discourse-cdn.com/v4/letter/g/e274bd/32.png) [@gadgetguru69](https://forum.inductiveautomation.com/u/gadgetguru69)\
**Post date:** [April 22, 2013, 8:19pm UTC](https://forum.inductiveautomation.com/t/http-authenticated-requests/6299/1 "2013-04-22T20:19:49Z")

</div>

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](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](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]

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [May 20, 2013, 9:12pm UTC](https://forum.inductiveautomation.com/t/http-authenticated-requests/6299/2 "2013-05-20T21:12:07Z")

</div>

I would just use the Java libraries instead.

[code]from [java.net](http://java.net) import URL  
from [org.apache.commons.io](http://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](https://something/whatever)”).openConnection()  
connection.setDoInput(1)  
connection.setRequestProperty(“Authorization”, authParam)  
connection.connect()

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

---

<div class="post-metadata">

**Author:** ![gadgetguru69](https://avatars.discourse-cdn.com/v4/letter/g/e274bd/32.png) [@gadgetguru69](https://forum.inductiveautomation.com/u/gadgetguru69)\
**Post date:** [May 20, 2013, 9:22pm UTC](https://forum.inductiveautomation.com/t/http-authenticated-requests/6299/3 "2013-05-20T21:22:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [May 21, 2013, 5:20pm UTC](https://forum.inductiveautomation.com/t/http-authenticated-requests/6299/4 "2013-05-21T17:20:32Z")

</div>

Ok good luck.
