我正在为连接在端口9000上的redis客户端(使用redis-py)编写一个redis服务器。
我现在编写了一个简单的测试代码,以检查服务器是否正在接收客户端的命令。我的代码如下:
client.py
import redis
r = redis.Redis(
host="127.0.0.1",
port=9000
)
v = r.set('foo', 1)
print(v)
v = r.set('bar', '1')
server.py
import asyncio
HOST = "127.0.0.1"
PORT = 15000
async def exo_redis(reader, writer):
try:
while True:
data = await reader.readuntil(b'\r\n')
# data = await reader.read(1024)
if not data:
break
print("Data: ", data)
response = "+OK\r\n"
writer.write(response.encode())
await writer.drain()
writer.close()
except asyncio.streams.IncompleteReadError:
pass
def start_server():
loop = asyncio.get_event_loop()
coro = asyncio.start_server(exo_redis, HOST, PORT)
server = loop.run_until_complete(coro)
print("Serving on ", HOST, "port: ", PORT)
try:
loop.run_forever()
except KeyboardInterrupt:
print("\nShutting Down Server...\n")
finally:
server.close()
loop.run_until_complete(server.wait_closed())
loop.close()
if __name__ == '__main__':
start_server()
运行此命令后,出现以下异常:
Data: b'*3\r\n'
Data: b'$3\r\n'
Data: b'SET\r\n'
Data: b'$3\r\n'
Data: b'foo\r\n'
Task exception was never retrieved
future: <Task finished coro=<exo_redis() done, defined at app.py:13> exception=ConnectionResetError('Connection lost')>
Traceback (most recent call last):
File "app.py", line 23, in exo_redis
await writer.drain()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/streams.py", line 348, in drain
await self._protocol._drain_helper()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/streams.py", line 202, in _drain_helper
raise ConnectionResetError('Connection lost')
ConnectionResetError: Connection lost
Data: b'*3\r\n'
Data: b'$3\r\n'
Data: b'SET\r\n'
Data: b'$3\r\n'
Data: b'bar\r\n'
Data: b'$1\r\n'
Data: b'1\r\n'
由于某种原因,从客户端发送的第一个命令会引发异常,而第二个命令则不会。
如果我使用reader.read(1024)(在代码中注释),则会得到以下输出:
Data: b'*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$1\r\n1\r\n'
Data: b'*3\r\n$3\r\nSET\r\n$3\r\nbar\r\n$1\r\n1\r\n'
这有时可行,并且没有抛出异常,但是在其他时候,我收到了此异常,而命令没有被完全读取。关于如何解决这个问题的任何想法,我可以确保在服务器端收到整个命令?