Python實(shí)現(xiàn)各種中間件的連接
連接數(shù)據(jù)庫(kù)
Redis連接
1、連接Redis單節(jié)點(diǎn)
import redis """ 連接redis ConnectionPool 方式連接 """ def connRedis(self): ? ? pool=redis.ConnectionPool(host='172.16.1.2',password='',db=2, port=6379) #按具體情況填寫(xiě)參數(shù) ? ? r=redis.StrictRedis(connection_pool=pool) ? ? r.set("test_name","admin") ? ? print(r.get('test_name'))
2、連接Redis cluster集群
python 操作redis 集群 用redis模塊不行,需要導(dǎo)入模塊
#!/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哨兵集群
#!/usr/bin/env python # -*- coding:utf-8 -*- import redis from redis.sentinel import Sentinel # 連接哨兵服務(wù)器(主機(jī)名也可以用域名) 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) # 獲取主服務(wù)器地址 master = sentinel.discover_master('mymaster') print(master) # 輸出:('172.31.0.2', 5001) # 獲取從服務(wù)器地址 slave = sentinel.discover_slaves('mymaster') print(slave) # 輸出:[('172.31.3', 5001), ('172.31.0.4', 5001), ('172.31.0.5', 5001)] # 獲取主服務(wù)器進(jìn)行寫(xiě)入 master = sentinel.master_for('mymaster', socket_timeout=0.5, password='redis_auth_pass', db=15) w_ret = master.set('foo', 'bar') # 輸出:True # # 獲取從服務(wù)器進(jìn)行讀取(默認(rèn)是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實(shí)現(xiàn)各種中間件的連接實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于Python連接中間件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python pygorithm模塊用法示例【常見(jiàn)算法測(cè)試】
這篇文章主要介紹了Python pygorithm模塊用法,結(jié)合實(shí)例形式分析了pygorithm模塊的功能、安裝及針對(duì)常見(jiàn)算法的相關(guān)使用操作技巧,需要的朋友可以參考下2018-08-08bpython 功能強(qiáng)大的Python shell
bpython是一個(gè)不錯(cuò)的Python解釋器的界面,本文帶著大家了解認(rèn)識(shí)一下bpython,感興趣的小伙伴們可以參考一下2016-02-02Python3查找列表中重復(fù)元素的個(gè)數(shù)的3種方法詳解
這篇文章主要介紹了Python3查找列表中重復(fù)元素的個(gè)數(shù)方法詳解,需要的朋友可以參考下2020-02-02Java?超詳細(xì)講解核心類(lèi)Spring?JdbcTemplate
JdbcTemplate?JdbcTemplate是Spring?JDBC核心包(core)中的核心類(lèi),它可以通過(guò)配置文件、注解、Java?配置類(lèi)等形式獲取數(shù)據(jù)庫(kù)的相關(guān)信息,實(shí)現(xiàn)了對(duì)JDBC開(kāi)發(fā)過(guò)程中的驅(qū)動(dòng)加載、連接的開(kāi)啟和關(guān)閉、SQL語(yǔ)句的創(chuàng)建與執(zhí)行、異常處理、事務(wù)處理、數(shù)據(jù)類(lèi)型轉(zhuǎn)換等操作的封裝2022-04-04Python爬蟲(chóng)模擬登錄帶驗(yàn)證碼網(wǎng)站
這篇文章主要介紹了Python爬蟲(chóng)模擬登錄帶驗(yàn)證碼網(wǎng)站的相關(guān)資料,需要的朋友可以參考下2016-01-01