python写入redis
import redis
import datetime
class Database:
def __init__(self):
self.host = 'localhost'
self.port = 6379
self.db = 1
def write(self,website,city,year,month,day,deal_number):
try:
key = '_'.join([website,city,str(year),str(month),str(day)])
print(key)
val = deal_number
conn = redis.Redis(host=self.host,port=self.port,db=self.db)
pipe = conn.pipeline()
conn.set(key,val)
except Exception as exception:
print(exception)
def read(self,website,city,year,month,day):
try:
key = '_'.join([website,city,str(year),str(month),str(day)])
r = redis.StrictRedis(host=self.host,port=self.port,db=self.db)
value = r.get(key)
print(value)
return value
except Exception as exception:
print(exception)
if __name__ == '__main__':
db = Database()
db.write('sina','beijing',2020,9,1,8000)
db.read('sina','beijing',2020,9,1)
# def main():
# conn = redis.Redis(host='49.233.145.209',port=6379,db=1)
# pipe = conn.pipeline()
# pipe.set("key1","value1")
# pipe.get("key1")
# #pipe.sadd("runoob","c++")
# pipe.smembers("runoob")
# #pipe.scard("runboob")
# res = pipe.execute()[2]
# print(res)
# if __name__ == "__main__":
# main()