我正在使用Httpretty来模拟Web API。
如果我使用httpretty.enable(allow_net_connect=False)
(当allow_net_connect为False时,与未注册uri的任何连接都会抛出httpretty.errors.UnmockedError),因为pymysql无法通过套接字连接到本地数据库,所以我收到了错误消息。
但是,当我使用httpretty.enable(allow_net_connect=True)
时,由于连接丢失而出现错误。
../../.virtualenvs/3.6espiga/lib/python3.6/site-packages/pymysql/__init__.py:90: in Connect
return Connection(*args, **kwargs)
../../.virtualenvs/3.6espiga/lib/python3.6/site-packages/pymysql/connections.py:699: in __init__
self.connect()
../../.virtualenvs/3.6espiga/lib/python3.6/site-packages/pymysql/connections.py:935: in connect
self._get_server_information()
../../.virtualenvs/3.6espiga/lib/python3.6/site-packages/pymysql/connections.py:1249: in _get_server_information
packet = self._read_packet()
../../.virtualenvs/3.6espiga/lib/python3.6/site-packages/pymysql/connections.py:991: in _read_packet
packet_header = self._read_bytes(4)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <pymysql.connections.Connection object at 0x7fefe1ad40f0>, num_bytes = 4
def _read_bytes(self, num_bytes):
self._sock.settimeout(self._read_timeout)
while True:
try:
data = self._rfile.read(num_bytes)
break
except (IOError, OSError) as e:
if e.errno == errno.EINTR:
continue
self._force_close()
raise err.OperationalError(
CR.CR_SERVER_LOST,
"Lost connection to MySQL server during query (%s)" % (e,))
if len(data) < num_bytes:
self._force_close()
raise err.OperationalError(
> CR.CR_SERVER_LOST, "Lost connection to MySQL server during query")
E pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')
../../.virtualenvs/3.6espiga/lib/python3.6/site-packages/pymysql/connections.py:1037: OperationalError
我如何让Httpretty模拟Web API并保持数据库连接正常运行?