Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the interaction between Redis and Python

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of how to realize the interaction between Redis and Python, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to realize the interaction between Redis and Python. Let's take a look at it.

Network installation pip install redis installs with source code

Go to the Chinese official website to find the client code

Unzip redis-py-master.zipcd redis-py-masterpython setup.py install 1 import redis 2 3 4 # 1. Connect to Redis server 5 try: 6 r=redis.StrictRedis (host='localhost', port=6379) 7 except Exception as e: 8 print (e.message) 9 10 # 2. Read and write data 11 # method 1: according to different data types, call the corresponding method to complete reading and writing 12 r.set ('name','hello') # set string data 13 r.get (' name') # read string data 14 15 # method 2: use pipline16 # to buffer multiple commands and then execute them at one time to reduce the frequency of data transmission Thus improving the efficiency 17 pipe = r.pipeline () 18 pipe.set ('name',' world') 19 pipe.get ('name') 20 pipe.execute ()

The part of connecting to the Redis server is consistent.

Encapsulate reads and writes of the String type.

1 import redis 2 3 4 # Redis tool class 5 class RedisTool (): 6 7 # initialize connection Redis 8 def _ init__ (self, host='localhost', port=6379): 9 self.__redis = redis.StrictRedis (host, port) 10 11 # read String value 12 def get (self Key): 13 if self.__redis.exists (key): # if 14 return self.__redis.get (key) 15 else: # otherwise return null value 16 return "" 17 18 # set String key value 19 def set (self, key, value): 20 self.__redis.set (key, value)

The business process is as follows:

Enter user name and password

Password encryption

Determine whether the user name is recorded in Redis, and if so, it is successful

If there is no user name in Redis, query it in Mysql

After the query from Mysql is successful, record the user name to Redis

1 from T2 import RedisTool 2 from T3 import MysqlTool 3 import hashlib 4 56 name=input ("Please enter user name:") 7 pwd=input ("Please enter password:") 8 9 # password encryption 10 sha1=hashlib.sha1 () 11 sha1.update (pwd) 12 pwd1=sha1.hexdigest () 13 14 # to determine whether there is cache data for this user information in Redis 15 try:16 redis=RedisTool () 17 if redis.get ('uname') = = name:18 print ( 'ok') 19 # No cache Then check the user information through the database 20 else:21 mysql = MysqlTool ('localhost', 3306,' test1', 'root',' mysql') 22 upwd = mysql.get_one ('select upwd from userinfos where uname=%s', [name]) 23 if upwd = = None:24 print (' user name error') 25 elif upwd [0] = pwd1:26 redis.set ('uname' Name) # user information verification passed Then write to the cache 27 print ('login successful') 28 else:29 print ("password error") 30 except Exception as e.message 31 print (e.message) on "how to implement the interaction between Redis and Python" this article is introduced here, thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to realize the interaction between Redis and Python". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report