设置redis能远程访问

远程服务器,redis 安装在/opt下redis-4.0.10

cd redis-4.0.10

修改配置文件redis.conf配置文件:(注释掉bind:127.0.0.1)和修改保护模式为no

修改另一个配置文件:

 vim  redis-6379.conf,修改保护模式:

启动redis服务

redis-server redis-6379.conf

进入redis 客户端

redis-cli

 python 中操作redis

# coding=utf8
"""
author:dengjiyun
"""
import redis

pool=redis.ConnectionPool(host='182.61.24.122',port=6379)

r =redis.Redis(connection_pool=pool)

# 从数据库中获取数据
name=r.get('name').decode('utf-8')

print(name)

 利用redis中set集合进行去重url

# coding=utf8
"""
author:dengjiyun
"""
import redis

pool=redis.ConnectionPool(host='182.61.24.122',port=6379)

r =redis.Redis(connection_pool=pool)

# set 集合

res=r.sadd('url_set','url1')

# 当res=1,表示要添加的url不在  url_set 中  当res=0 时表示url已经存在url_set 从而实现去重的功能
if res:
    print('新添加成功!') # 此处可以让把该url直接让爬虫进行爬取
    
else:
    print('该url已经存在')

猜你喜欢

转载自www.cnblogs.com/knighterrant/p/10921923.html