Home > Software engineering >  Connection between python and Redis
Connection between python and Redis

Time:04-05

i'm trying to implement via python a mini database with Redis, open in the background via Docker. After opening port 6666: 6379 (working), I wrote some python code that should connect to redis to get the qweries written. however this does not happen. Can anyone help me fix the code? The error that the system gives me is that it cannot connect to Redis. Many thanks in advance

mport redis

close_everytime = False

choice = None
redis = redis.Redis(host='redis', port=6379, db=0, charset='utf-8', decode_responses=True)

if not redis.ping():
    raise "connection error"

if not redis.exists("nextp"):
    redis.set("nextp", 0)

while True:
    print("1 - proposed creation")
    print("2 - vote proposal")
    print("3 - outcome")
    print("4 - Reset")
    print("5 - exit")
    try:
        choice = int(input("> "))
    except ValueError:
        pass

    if choice == 1:
        key = "pr:" redis.get("nextp")
        redis.incr("nextp")

        proposer = input("proposer: ")
        redis.hset(key, "proposer", proposer)

        title = input("title: ")
        redis.hset(key, "title", title)

        description = input("description: ")
        redis.hset(key, "description", description)

        redis.zadd("rank", {key: 0})
    elif scelta == 2:
        userid = int(input("isert your user id: "))
        proposal = input("Insert proposal to vote")
        if not redis.exists("pr:" proposal):
            raise "error, lthis proposal doesn't exists"

        print(redis.smembers("voters:pr:" proposal))
        print(f"'{idutente}'")
        if redis.sismember("voters:pr:" proposal, userid):
            raise "The user already voted"

        redis.sadd("voters:pr:" proposal, userid)
        redis.zincrby("rank", 1, "pr:" proposal)
    elif choice == 3:
        rank = redis.zrange("rank", 0, -1, desc=True, withscores=True)
        # print(rank)

        point_first = int(rank[0][1])
        print("this is the winner rank: ", point_first, "point")
        for i in range(len(classifica)):
            if rank[i][1] == point_first:
                print(rank[i][0], "from title:", redis.hget("pr:" str(i), "title"))
    elif choice == 4:
        redis.flushall()
    elif choice == 5:
        break
    else:
        print("error, tap 5 to esc")

    if close_everytime:
        break

On cmd connecting to docker i type:

docker run --network biscotto --rm --name stefano -d redis
docker run -it --network biscotto -p 6666:6379 --rm redis redis-cli -h stefano

i would expect a connection from python to redis, but this one fails with this error

Unable to establish connection. Persistent rejection of the target computer.

CodePudding user response:

One solution give the redis container a name with --name redis

docker run -it --network biscotto --name redis -p 6666:6379 --rm redis redis-cli -h stefano `

Change host in redis to the redis container name

redis = redis.Redis(host='redis', port=6379, db=0, charset='utf-8', decode_responses=True)

  • Related