我正在尝试使用IronPython 2.7.4使用urllib2
模块使用数据和标头发出基本HTTP请求。由于一些原因,我不能使用IronPython 2.7.5,因此无法安装可爱的requests
模块 - 据我发现,它与早期版本不兼容 - 所以我坚持{ {1}}。
我的代码似乎在urllib2
行失败了:
response = urllib2.urlopen(request)
我收到以下冗长的错误......有任何建议吗?
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
编辑:解决了问题!
发现了一个不同的请求类型库in the IronPython documentation,它使用了.NET类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()>
修改了稍微显示的示例函数来解释JSON和blammo。 WebRequest
已生成响应。