[프로젝트] 파이썬으로 redis 구현하기[1]
Introduction
Redis는 데이터베이스, 캐시, 메시지 브로커, streaming 엔진으로 사용되는 in-memory 데이터 구조이다. 기본 명령어를 제공하는 redis 서버를 만들면서, TCP 서버와 redis 프로토콜 방식을 이해한다.
git clone https://git.codecrafters.io/c87c57b60b92b708 codecrafters-redis-python
cd codecrafters-redis-python
Bind to a port
포트 6379를 사용하는 TCP 서버를 연다
Redis의 서버 및 클라이언트 모두 TCP 프로토콜 위에서 동작한다.
import socket
def main():
print("Logs from your program will appear here!")
server_socket = socket.create_server(("localhost", 6379), reuse_port=True)
server_socket.accept() # wait for client
if __name__ == "__main__":
main()
Response to PING
Redis의 기초 명령어 중 하나인 PING 커멘드를 구현한다. 클라이언트가 PING 명령어를 보내면 +PONG\r\n 로 응답한다.
import socket
def main():
print("Logs from your program will appear here!")
PONG = "+PONG\r\n"
server_socket = socket.create_server(("localhost", 6379), reuse_port=True)
client, addr = server_socket.accept() # wait for client
with client:
client.recv(1024)
client.send(PONG.encode())
if __name__ == "__main__":
main()