简体   繁体   中英

IronPython fails when calling urlopen

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

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.


This answer was posted as an edit to the question IronPython fails when calling urlopen by the OP Johnny B under CC BY-SA 3.0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM