2

I'm attempting to use IronPython 2.7.4 to make a basic HTTP request with data and a header, using the urllib2 module. For a few reasons, I cannot use IronPython 2.7.5, and therefore cannot install the lovely requests module -- as far as I've discovered, it's not compatible with earlier versions -- so I'm stuck with urllib2.

My code appears to fail at the response = urllib2.urlopen(request) line:

import json
import urllib2
import logging

#Create log for long-ass error
LOG_FILENAME = 'error.txt'
logging.basicConfig(filename=LOG_FILENAME, level=logging.DEBUG)


try:
    #Main Function
    data = json.dumps({"userID": "user@companycom", "password": "abcd1234"})
    header = {"Content-Type": "application/json"}
    request = urllib2.Request("https://company.autodeskplm360.net/rest/auth/1/login")
    request.add_header("Content-Type", "application/json")
    request.add_data(data)
    response = urllib2.urlopen(request)
    print(response)
#Print long-ass error to file
except:
    logging.exception("THIS IS MY ERROR")
    raise

I receive the following lengthy error... any suggestions??

ERROR:root:THIS IS MY ERROR
Traceback (most recent call last):
  File "API_Template_IPY_simple.py", line 15, in <module>
    response = urllib2.urlopen(request)
  File "C:\Program Files (x86)\IronPython 2.7\Lib\urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Program Files (x86)\IronPython 2.7\Lib\urllib2.py", line 394, in open
    response = self._open(req, data)
  File "C:\Program Files (x86)\IronPython 2.7\Lib\urllib2.py", line 411, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "C:\Program Files (x86)\IronPython 2.7\Lib\urllib2.py", line 372, in _call_chain
    result = func(*args)
  File "C:\Program Files (x86)\IronPython 2.7\Lib\urllib2.py", line 1207, in https_open
    return self.do_open(httplib.HTTPSConnection, req)
  File "C:\Program Files (x86)\IronPython 2.7\Lib\urllib2.py", line 1174, in do_open
    raise URLError(err)
URLError: <urlopen error [Errno errors while performing handshake: ] System.Security.Authentication.AuthenticationException: A call to SSPI failed, see inner exception. ---> System.ComponentModel.Win32Exception: The supplied message is incomplete. The signature was not verified

   --- End of inner exception stack trace ---

   at System.Net.Security.SslState.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, Exception exception)

   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)

   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)

   at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)

   at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)

   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)

   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)

   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)

   at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)

   at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)

   at System.Net.Security.SslStream.AuthenticateAsClient(String targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, Boolean checkCertificateRevocation)

   at IronPython.Modules.PythonSocket.ssl.do_handshake()>

Edit: Solved the problem!

Found a different requests-type library buried in the IronPython documentation which uses the .NET Class WebRequest Modified the example function shown slightly to account for JSON, and blammo. 200 OK response generated.

Johnny B
  • 31
  • 5
  • 1
    Since you're accessing an `https:` url, you need a way to verify the host's security certificate, or else tell urllib to ignore certificates. – John Gordon Jul 21 '16 at 00:07
  • 1
    looked into this, and you're correct -- `urllib` doesn't handle `ssl` so I tried using [this solution](http://stackoverflow.com/questions/27835619/ssl-certificate-verify-failed-error) to bypass SSL verification. Sadly, ironpython doesn't appear to support `ssl.SSLContext()` for one reason or another. I found another solution using `from System.Net import WebRequest`. See post edit. – Johnny B Jul 21 '16 at 22:36

0 Answers0