我最近扩展了用于Windows Azure存储API(PyAzure)的Python API以包含对服务管理API的支持。见https://github.com/bmb/pyazure。针对Azure服务管理API的Python HTTPS在Windows上失败
我使用的HTTPSClientAuthHandler与using pyOpenSSL to create urllib custom opener中建议的HTTPSClientAuthHandler类似。在Linux上,使用各种版本的Python 2.6和2.7,这很好。不过,Windows是另一回事。对Azure的管理主机地址的所有请求失败:
[错误10054]现有的连接被强行远程主机
这一点我觉得封闭,是socket错误号10054“连接重置对等“,在拖动。
这似乎不是我的API代码中的一个问题(除非我使用的客户端证书认证方法是虚假的),但是更低一层。我可以在没有urllib2或httplib的情况下通过简单地设置一个SSL套接字并且像urllib2一样向管道发送相同的HTTP请求来重现问题。列出有效的Azure数据中心位置:
>>> import socket, ssl, sys
>>> sys.version
'2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit (AMD64)]'
>>> s = ssl.wrap_socket(socket.socket(), certfile='c:\\users\\blair\\research\\clouds\\azure\\BlairBethwaiteAzure1.pfx.pem')
>>> s.connect(('management.core.windows.net',443))
>>> s.send("GET /SUBSCRIPTION_ID/locations HTTP/1.1\r\nAccept-Encoding: identity\r\nX-Ms-Version: 2011-10-01\r\nHost: management.core.windows.net\r\nConnection: close\r\nUser-Agent: Python-urllib/2.6\r\n\r\n")
202
>>> s.read()
Traceback (most recent call last):
c:\Users\blair\research\clouds\azure\pyazure\<ipython-input-63-3306c981d8a7>
in <module>()
----> 1 s.read()
C:\Python27\lib\ssl.pyc in read(self, len)
136
137 try:
--> 138 return self._sslobj.read(len)
139 except SSLError, x:
140 if x.args[0] == SSL_ERROR_EOF and self.suppress_ragged_eofs:
error: [Errno 10054] An existing connection was forcibly closed by the remote host
用您的Azure订阅ID替换上面的SUBSCRIPTION_ID。在调用SSLSocket.read之后,异常提升〜45秒。该证书是既包括私钥和证书格式正确的PEM文件,将其从PFX(在Ubuntu 10.04)使用转化:
OpenSSL的PKCS12 -in pfxfile -out pemfile -nodes
我不认为它在这里很重要,但我也尝试unix2dos-PEM文件,但无济于事。即使我没有提供任何证书,我也会得到相同的行为,但在Linux上执行此操作会导致服务器出现适当的API错误:
'HTTP/1.1 403 Forbidden \ r \ nContent-Length:0 \ r \ n日期:2011年12月1日星期四13:59:29 GMT \ r \ n连接:关闭\ r \ n \ r \ n'
已经由另一个人使用Windows 7进行独立验证(与我一样)。这不是客户端防火墙问题 - 相同的代码在同一主机上运行的NAT-ed Linux VM中工作。
我很难过。真的很感谢所有帮助乡亲们在这里也许能提供...
更新: 这似乎与在Python底层SSL实现。 CPython的2.7.1具有如上所示的错误行为,但我因为测试,并取得了成功与ActiveState的Python的(包括2.7和2.6),例如:
>>> import sys, socket, ssl
>>> sys.version
'2.7.1 (r271:86832, Feb 7 2011, 11:30:38) [MSC v.1500 32 bit (Intel)]'
>>> s = ssl.wrap_socket(socket.socket(), certfile='\\\\VBOXSVR\\azure\\BlairBethwaiteAzure1.pfx.pem')
>>> s.connect(('management.core.windows.net',443))
>>> s.send('GET /SUBSCRIPTION_ID/locations HTTP/1.1\r\nAccept-Encoding: identity\r\nX-Ms-Version: 2011-10-01\r\nHost: management.core.windows.net\r\nUser-Agent: Python-urllib/2.6\r\n\r\n')
183
>>> s.read(4096)
'HTTP/1.1 200 OK\r\nContent-Length: 908\r\nContent-Type: application/xml; charset=utf-8\r\nServer: Microsoft-HTTPAPI/2.0\r\nx-ms-request-id: 08ca048cda6b445da6b3a8f3e4890197\r\nDate: Fri, 02 Dec 2011 03:02:14 GMT\r\n\r\n<Locations xmlns="http://schemas.microsoft.com/windowsazure" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Location><Name>Anywhere US</Name><DisplayName>Anywhere US</DisplayName></Location><Location><Name>South Central US</Name><DisplayName>South Central US</DisplayName></Location><Location><Name>North Central US</Name><DisplayName>North Central US</DisplayName></Location><Location><Name>Anywhere Europe</Name><DisplayName>Anywhere Europe</DisplayName></Location><Location><Name>North Europe</Name><DisplayName>North Europe</DisplayName></Location><Location><Name>West Europe</Name><DisplayName>West Europe</DisplayName></Location><Location><Name>Anywhere Asia</Name><DisplayName>Anywhere Asia</DisplayName></Location><Location><Name>Southeast Asia</Name><DisplayName>Southeast Asia</DisplayName></Location><Location><Name>East Asia</Name><DisplayName>East Asia</DisplayName></Location></Locations>'
和预期的一样我的API也工作:
ActivePython 2.6.7.20 (ActiveState Software Inc.) based on
Python 2.6.7 (r267:88850, Jun 27 2011, 13:20:48) [MSC v.1500 64 bit (AMD64)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pyazure import pyazure
>>> pa = pyazure.PyAzure(subscription_id=SUBSCRIPTION_ID, management_cert_path='c:\\users\\blair\\research\\clouds\\azure\\BlairBethwaiteAzure1.pfx.pem')
>>> list(pa.wasm.list_locations())
['Anywhere US', 'South Central US', 'North Central US', 'Anywhere Europe', 'North Europe', 'West Europe', 'Anywhere Asia', 'Southeast Asia', 'East Asia']
的lib \ ssl.py在CPython2.7和ActivePython2.7文件是相同的,所以我想这一定是由于底层的C库差一些,也许在CPython的一个bug。那里有哪位大师?
您是否有任何类似代理的过程,例如坐在您的机器上的Fiddler? –
没有本地代理或嗅探器。 – Blairo