每当我启用 HTTPretty 时,我都无法与 PyMongo 建立连接。我知道 HTTPretty 改变了核心套接字模块;有什么办法解决这个问题吗?
代码示例:
import pymongo
import httpretty
import time
httpretty.enable()
try:
client = pymongo.MongoClient()
except pymongo.errors.AutoReconnect:
print("AutoReconnect")
time.sleep(2)
引发异常:
Traceback (most recent call last):
File "C:\Python33\lib\site-packages\pymongo\mongo_client.py", line 363, in __init__
self._ensure_connected(True)
File "C:\Python33\lib\site-packages\pymongo\mongo_client.py", line 924, in _ensure_connected
self.__ensure_member()
File "C:\Python33\lib\site-packages\pymongo\mongo_client.py", line 797, in __ensure_member
member, nodes = self.__find_node()
File "C:\Python33\lib\site-packages\pymongo\mongo_client.py", line 888, in __find_node
raise AutoReconnect(', '.join(errors))
pymongo.errors.AutoReconnect: [WinError 10035] A non-blocking socket operation could not be completed immediately
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "tmp.py", line 7, in
client = pymongo.MongoClient()
File "C:\Python33\lib\site-packages\pymongo\mongo_client.py", line 366, in __init__
raise ConnectionFailure(str(e))
pymongo.errors.ConnectionFailure: [WinError 10035] A non-blocking socket operation could not be completed immediately
我在 Windows 8.1 上使用 Python 3.3。
谁能解释一下这种行为以及如何解决它?谢谢!
最佳答案
看起来引发的异常与 HTTPretty 的猴子补丁有关
每当我们对非 HTTP 请求调用 sendall
时,它都会在自己的套接字上调用 settimeout(0)
(请参阅
real_sendall
)。这
将套接字置于非阻塞模式。套接字上的超时永远不会重置
在 real_sendall
之后,因此对 recv
的后续调用会失败并显示 WSAEWOULDBLOCK
(错误号 10035)。这可能是 HTTPretty 中的一个错误。
解决此问题的一种方法是在之后重置套接字上的超时
real_sendall
。这可以通过猴子修补fakesocket.socket
来完成
在 HTTPretty 中:
from httpretty.core import fakesock
class MySocket(fakesock.socket):
def real_sendall(self, data, *args, **kw):
super(MySocket, self).real_sendall(data, *args, **kw)
# Restore non-zero timeout
self.truesock.settimeout(self.timeout)
fakesock.socket = MySocket
关于python - httpretty.enable() 之后 MongoClient 连接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24327277/