Need script help for logging into https web site

Been running into a wall for how to get a script to use https and auto enter a user name and password. any help would be appreciated.

I’ve been helping with this off of the forum, but for anyone else who might want to do the same thing, this can be used in an app script. You would this call “read”, like “app.net.read(‘myhost.com’, ‘/path/to/resource’, ‘myuser’, ‘mypass’)”

from javax.net.ssl import TrustManager, X509TrustManager
from jarray import array
from javax.net.ssl import SSLContext
class TrustAllX509TrustManager(X509TrustManager):
	'''Define a custom TrustManager which will blindly accept all certificates'''

	def checkClientTrusted(self, chain, auth):
		pass
	
	def checkServerTrusted(self, chain, auth):
		pass
	
	def getAcceptedIssuers(self):
		return None
# Create a static reference to an SSLContext which will use
# our custom TrustManager
trust_managers = array([TrustAllX509TrustManager()], TrustManager)
global TRUST_ALL_CONTEXT 
global DEFAULT_CONTEXT
TRUST_ALL_CONTEXT = SSLContext.getInstance("SSL")
TRUST_ALL_CONTEXT.init(None, trust_managers, None)
# Keep a static reference to the JVM's default SSLContext for restoring
# at a later time
DEFAULT_CONTEXT = SSLContext.getDefault()
 
def trust_all_certificates(f):
	'''Decorator function that will make it so the context of the decorated method
	will run with our TrustManager that accepts all certificates'''
	global TRUST_ALL_CONTEXT
	global DEFAULT_CONTEXT
	def wrapped(*args, **kwargs):		
		from javax.net.ssl import SSLContext
		SSLContext.setDefault(TRUST_ALL_CONTEXT)
		try:
			res = f(*args, **kwargs)
			return res
		finally:
			SSLContext.setDefault(DEFAULT_CONTEXT)
		
	return wrapped

@trust_all_certificates
def read(host, url, username, password): 	
	import httplib
	import base64
	
	# token is base64 encoded "username:password"
	authToken = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
		
	webservice = httplib.HTTPSConnection(host)
	webservice.putrequest("GET", url)
	# write the Authorization header like: 'Basic base64encode(username + ':' + password)
	webservice.putheader("Authorization", "Basic %s" % authToken) 
	webservice.endheaders()
	
	resp = webservice.getresponse()
	data = ''
	#200 is http 'good'
	if(resp.status==200):
		data = resp.read()
	else:
		#Would likely be better to throw error here, like:
		#raise Exception('HTTP Error Code %d' % resp.status)
		#Common codes: 302=invalid login, 404=resource not found.
		data = 'Error response %s' % resp.status
		
	return data

Everything before the actual definition of “read” is only necessary when going against self-signed certificates. Real ssl certificates seem to work find without all of that.

Regards,

Hi Colby,

I was curious to see if there are any advantages to using java instead of python for scripting in Ignition? Are there certain things that can be only accessed by one or the other? Thanks

Hi,

Well, you’re always using python, it’s just that it’s possible to access java classes through it as well. Using java objects can sometimes be useful in more advanced cases, though it’s not really something that we expect people to do much. In this example, due to some issues with SSL that couldn’t easily be addressed directly in python, we had to go to java.

Now, just a note about this thread: As a result of this, we’re going to update the system.net.httpPost/httpGet methods to support authentication as well, which would have made all of this 1 line of scripting.

Regards,

Thanks a lot for your help Colby could not have done it with out your help.

Hi,
In which version of Ignition system.net.httpPost/httpGet methods started to support authentication?