Redis连接返回UnicodeError

问题描述

我正在尝试使用Python 3.8连接到Heroku Redis,以保存Redis字典和字符串。 我试图将Herokus Redis用作Telegram机器人的持久性(库:python-telegram-bot和Telethon) 在SET()上调用Redis实例后,立即收到以下错误消息:

2020-10-06 16:15:26,335 - telegram.utils.promise - ERROR - An uncaught error was raised while running the promise
app[web.1]: Traceback (most recent call last):
app[web.1]: File "/app/.heroku/python/lib/python3.8/encodings/idna.py",line 165,in encode
app[web.1]: raise UnicodeError("label empty or too long")
app[web.1]: UnicodeError: label empty or too long
app[web.1]: 
app[web.1]: The above exception was the direct cause of the following exception:
app[web.1]:
app[web.1]: Traceback (most recent call last):
app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/telegram/utils/promise.py",line 56,in run
app[web.1]: self._result = self.pooled_function(*self.args,**self.kwargs)
app[web.1]: File "/app/karim/bot/commands/account.py",line 7,in check_account
app[web.1]: manager= SessionManager(Persistence.ACCOUNT,chat_id=update.effective_chat.id,user_id=update.effective_chat.id,message_id=update.message.message_id)
app[web.1]: File "/app/karim/classes/persistence.py",line 14,in wrapper
app[web.1]: self.serialize()
app[web.1]: File "/app/karim/classes/persistence.py",line 64,in serialize
app[web.1]: redis_connector.hmset('persistence:{}{}{}'.format(self.method,self.user_id,self.chat_id),obj_dict)
app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/redis/client.py",line 3075,in hmset
app[web.1]: return self.execute_command('HMSET',name,*items)
app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/redis/client.py",line 898,in execute_command
app[web.1]: conn = self.connection or pool.get_connection(command_name,**options)
app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/redis/connection.py",line 1192,in get_connection
app[web.1]: connection.connect()
app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/redis/connection.py",line 559,in connect
app[web.1]: sock = self._connect()
app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/redis/connection.py",line 584,in _connect
app[web.1]: for res in socket.getaddrinfo(self.host,self.port,self.socket_type,app[web.1]: File "/app/.heroku/python/lib/python3.8/socket.py",line 918,in getaddrinfo
app[web.1]: for res in _socket.getaddrinfo(host,port,family,type,proto,flags):

我想知道错误是在连接到Redis时引起的(我只启动了一次连接,并且Heroku可能正在更改REdis_URL)还是尝试传递字典时引起了错误

这是redis_connector初始化程序:

redis_connector = redis.Redis(host=secrets.get_var('REdis_URL'),port=14909,db=0,decode_responses=False)

这是导致错误代码行:

redis_connector.hmset('persistence:{}{}{}'.format(self.method,obj_dict)

这是我要通过的词典:

{'method': 'account','chat_id': 1234,'user_id': 1234,'message_id': 55,'phone': -1,'password': -1,'code': -1,'phone_code_hash': -1,'code_tries': 0}

有关完整的代码参考: https://github.com/davidwickerhf/karim(尤其是https://github.com/davidwickerhf/karim/blob/main/karim/classes/persistence.pyhttps://github.com/davidwickerhf/karim/blob/main/karim/classes/session_manager.pyhttps://github.com/davidwickerhf/karim/blob/main/karim/__init__.py-调用了redis连接的文件

谢谢您的帮助

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)