脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python实现各种中间件的连接

Python实现各种中间件的连接

2022-11-07 10:57林海峰4573 Python

这篇文章主要为大家介绍了Python实现各种中间件的连接实现,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

连接数据库

Redis连接  

1、连接Redis单节点

?
1
2
3
4
5
6
7
8
9
import redis
"""
连接redis ConnectionPool 方式连接
"""
def connRedis(self):
    pool=redis.ConnectionPool(host='172.16.1.2',password='',db=2, port=6379) #按具体情况填写参数
    r=redis.StrictRedis(connection_pool=pool)
    r.set("test_name","admin")
    print(r.get('test_name'))

2、连接Redis cluster集群 

python 操作redis 集群 用redis模块不行,需要导入模块

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env python
#coding:utf-8
 
 
from rediscluster import StrictRedisCluster
import sys
 
def redis_cluster():
    redis_nodes =  [{'host':'192.168.1.2','port':6378},
                    {'host':'192.168.1.2','port':6380},
                    {'host':'192.168.1.2','port':6381},
                    {'host':'192.168.1.2','port':6382},
                    {'host':'192.168.1.2','port':6383},
                    {'host':'192.168.1.2','port':6384},
                    {'host':'192.168.1.2','port':6385}
                   ]
    try:
        redisconn = StrictRedisCluster(startup_nodes=redis_nodes)
    except Exception,e:
        print "Connect Error!"
        sys.exit(1)
 
    redisconn.set('name','admin')
    print "name is: ", redisconn.get('name')
 
redis_cluster()

3、连接Redis哨兵集群

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import redis
from redis.sentinel import Sentinel
# 连接哨兵服务器(主机名也可以用域名)
sentinel = Sentinel([('172.31.0.2', 5001),
                     ('172.31.0.3', 5001),
                     ('172.31.0.4', 5001),
                     ('172.31.0.5', 5001)
             ],
                    socket_timeout=0.5)
# 获取主服务器地址
master = sentinel.discover_master('mymaster')
print(master)
# 输出:('172.31.0.2', 5001)
# 获取从服务器地址
slave = sentinel.discover_slaves('mymaster')
print(slave)
# 输出:[('172.31.3', 5001), ('172.31.0.4', 5001), ('172.31.0.5', 5001)]
# 获取主服务器进行写入
master = sentinel.master_for('mymaster', socket_timeout=0.5, password='redis_auth_pass', db=15)
w_ret = master.set('foo', 'bar')
# 输出:True
# # 获取从服务器进行读取(默认是round-roubin)
slave = sentinel.slave_for('mymaster', socket_timeout=0.5, password='redis_auth_pass', db=15)
r_ret = slave.get('foo')
print(r_ret)
# # 输出:bar

以上就是Python实现各种中间件的连接实现的详细内容,更多关于Python连接中间件的资料请关注服务器之家其它相关文章!

原文链接:https://blog.51cto.com/u_7961702/5329008

延伸 · 阅读

精彩推荐
  • PythonPython图像处理之几何变换

    Python图像处理之几何变换

    这篇文章将详细讲解图像几何变换,包括图像平移、图像缩放和图像旋转。文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编学习一下...

    Eastmount9492022-09-06
  • Pythonpython使用建议技巧分享(三)

    python使用建议技巧分享(三)

    这篇文章主要介绍了python的一些使用建议技巧分享,帮助大家更好的理解和学习python,感兴趣的朋友可以了解下 ...

    songleo4902020-08-18
  • Python在Python中如何使用yield

    在Python中如何使用yield

    在 Python 开发中,yield 关键字的使用其实较为频繁,例如大集合的生成,简化代码结构、协程与并发都会用到它,文中详细介绍了yield的用法,需要的朋友可以参考...

    vincent_duan11252021-11-25
  • PythonPython基础之dict和set的使用详解

    Python基础之dict和set的使用详解

    这篇文章主要为大家详细介绍了Python语言中dict和set的使方法,文中的示例代码讲解详细,对我们学习Python有一定的帮助,需要的可以参考一下...

    廖雪峰4092022-08-10
  • Python10款最好的Web开发的 Python 框架

    10款最好的Web开发的 Python 框架

    这篇文章主要介绍了10款最好的Web开发的 Python 框架,总结的都是非常常用的而且评价都非常不错的框架,需要的朋友可以参考下 ...

    脚本之家3312020-05-22
  • Python基于Python pip用国内镜像下载的方法

    基于Python pip用国内镜像下载的方法

    今天小编就为大家分享一篇基于Python pip用国内镜像下载的方法。具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    mapw19936522021-03-04
  • PythonPython drop方法删除列之inplace参数实例

    Python drop方法删除列之inplace参数实例

    这篇文章主要介绍了Python drop方法删除列之inplace参数实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    碧空之戈11132020-06-28
  • Python总结分析Python的5个硬核函数

    总结分析Python的5个硬核函数

    今天看到一篇很好的 Python 博文,结合自己的经验总结,分享给大家一篇关于eval, exec, compile, locals, globals这些函数的文章...

    Python学习与数据挖掘3992022-03-04