Redis Subscribe and publish Messages
First, the publisher starts the Redis-server.exe service
Publishing End pub.py
#!/usr/bin/env python
#-*-coding:utf-8-*-
' Created on
2015-9-9
@author: Sxli
'
import Redis
Import sys
class Publishchannel (object):
#kword = u "desktop". Encode (' gb2312 ')
def send_ Pybot (self,msg):
message=["Msg1", "MSG2", "MSG3"]
'
msg1: External window operation on participant-side shared app
MSG2: Shared app for participant side inside window Operation
MSG3: Participant End Pallet Disconnect reconnection operation ' Pool=redis '
. ConnectionPool (host= ' 192.168.3.58 ', port=6379,db=0)
r = Redis. Strictredis (Connection_pool=pool)
# input = Raw_input ("Publish:")
if msg in:
r.publish (' spub ' , msg)
if input = = ' over ':
print ' Stop publishing '
# break;
if __name__ = = "__main__": Do
= Publishchannel ()
Do.send_pybot (sys.argv[1])
print "Finish msg to Channel !"
Subscriber End sub.py
#!/usr/bin/env python #-*-coding:utf-8-*-' Created on 2017-12-18 @author: Sxli ' ' Import redis import Win32API I Mport OS Pool=redis. ConnectionPool (host= ' 192.168.3.58 ', port=6379,db=0) #db = 0, select operation No. 0 database Redis The default has 16 database conf can configure r = Redis. Strictredis (connection_pool=pool) p = r.pubsub () p.subscribe (' spub ') message=["MSG1", "MSG2", "MSG3"] for item in
P.listen (): Print item if item[' type '] = = ' message ': data = item[' data '] # print data if data = = Message[0]: print U "External window operation for participant-side shared app" Os.system ("C:\JHAppTestAutomation\JH
Codesign-5.0\jhapp-4.0.0\bat\gototestcase1.bat ") Print U" MSG1 execution complete "elif data = = Message[1]: Print U "Internal window operation on participant-side shared App" #os. System ("C:\\jhapptestautomation\\jhcodesign-5.0\\bat\\gototestcase2.bat%s" % (x)) elif data = = Message[2]: print U "Internal window operation on participant-side shared App" #os. System ("C:\\jhapptestauto Mation\\jhcodesign-5.0\\bat\\gotoTestcase2.bat%s "% (x)) elif data = = Message[3]: print u" Log off user! "
#os. System ("C:\\jhapptestautomation\\jhcodesign-5.0\\bat\\gototestcase2.bat%s"% (x)) if item[' data ']== ' over ':
Break P.unsubscribe (' spub ') print ' unsubscribe '
Can be tested by Redis-cli.exe;
CMD Open Redis-cli.exe
To see how to use:
"Redis-cli.exe" –help
Connect to the Redis service side:
"Redis-cli.exe"-H localhost-p (default 6379)
Subscribe to a channel:
192.168.3.58:6379> Subscribe Spub
Reading messages ... (Press Ctrl-c to quit)
1) "Subscribe"
2) "Spub"
3) (integer) 1
Post a message:
192.168.3.58:6379> publish Spub "Hello!"
(integer) 1
Finally run through after testing:
Subscriber side : Python sub.py
{' Pattern ': None, ' type ': ' Subscribe ', ' Channel ': ' Spub ', ' Data ': 1L}
publish-side execution scripts: Python pub.py msg1
Subscriber side : Python sub.py
{' Pattern ': None, ' type ': ' Subscribe ', ' Channel ': ' Spub ', ' Data ': 1L}
External window operation for shared app on contributor side
MSG1 Execution completed
Related connections:
https://blog.csdn.net/kwsy2008/article/details/48372079
https://blog.csdn.net/dgatiger/article/ details/50436014
http://redisbook.readthedocs.io/en/latest/feature/pubsub.html