IronPython urllib2基本身份验证异常? (Shopify)

时间:2014-11-07 21:54:55

标签: .net urllib2 ironpython shopify

我正在创建一个使用基本身份验证的私人Shopify应用。 该程序将在IronPython中编写,但是,我在使urllib2工作时遇到了麻烦。

我从这里尝试了解决方案: Python urllib2 Basic Auth Problem

并且它与普通的python一样正常工作,但是当使用IronPython运行时,我收到此错误:

IOError: System.IO.IOException: Unable to read data from the transport connection: An existing       connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) 

--- End of inner exception stack trace --- 
at Microsoft.Scripting.Runtime.LightExceptions.CheckAndThrow(Object value) at DLRCachedCode.do_open$5398(Closure , PythonFunction $function, Object self, Object http_class, Object req) at IronPython.Runtime.FunctionCaller`3.Call3(CallSite site, CodeContext context, Object func, T0 arg0, T1 arg1, T2 ar...

我尝试使用IronPython中的所有.NET解决方案(不使用urllib2)并使用此方法工作:http://www.ironpython.info/index.php?title=Fetching_a_Page_with_Basic_Authentication

幕后是否有一些魔法(python - > .NET)会导致这种情况无效?

1 个答案:

答案 0 :(得分:3)

当前版本的IronPython默认为SSL v2。假设您的服务器正在接受TLS1,则在使用urllib2之前需要以下内容

import ssl
import functools
old_init = ssl.SSLSocket.__init__
@functools.wraps(old_init)
def init_with_tls1(self, *args, **kwargs):
    kwargs['ssl_version'] = ssl.PROTOCOL_TLSv1
    old_init(self, *args, **kwargs)
ssl.SSLSocket.__init__ = init_with_tls1