时间:2021-01-31 08:10:07 | 栏目:Python代码 | 点击:次
公司服务器的mysql和redis连接都需要有跳板机,网上有很多python ssh远程连接mysql的,那天我研究了下,利用sshtunnel模块连接上了redis,具体如下:
from sshtunnel import SSHTunnelForwarder # ssh连接库
import redis # redis模块
server = SSHTunnelForwarder(
ssh_address_or_host= , # ssh地址
ssh_username= , # ssh连接的用户名
ssh_password= , # ssh连接的用户名
remote_bind_address=('远程机器地址', 端口号))
server.start()
r=redis.Redis(host='redis地址', port=server.local_bind_port, decode_responses=True)
如上就可以连接上redis啦,要关闭连接,我用的server.close()