Python GUI没有捕获zeromq消息

2024-09-30 06:26:45 发布

您现在位置:Python中文网/ 问答频道 /正文


我有一个小程序,没有收到服务器的响应。
这是在Ubuntu14.04上使用python3.4和最新的zeromq和pyqt5。

客户机GUI向服务器发送一条消息,服务器获取消息并作出响应,但是客户机看不到响应。在

代码如下:
迷你服务器.py公司名称:

import os
import sys
import time
import zmq
from zmq.eventloop import ioloop
from zmq.eventloop.zmqstream import ZMQStream

# Prepare for client socket
ctx = zmq.Context.instance()
sIn = ctx.socket(zmq.PULL)
urlIn = 'tcp://127.0.0.1:1234'
sIn.bind(urlIn)
# Prepare for sockets to GUI and Web
guiCalled = False
webCalled = False
urlGui = 'tcp://127.0.0.1:2345'
urlWeb = 'tcp://127.0.0.1:3456'
sGUI = None
sWeb = None

def __GetConfig__(sender, data):
    if "GUI" == sender:
        print("Sending back config list to GUI")
        sGUI.send_string("From Server to GUI")
    elif "WEB" == sender:
        sWeb.send_string("From Server to Web")
def __CheckGUICalled__():
    # Used to only connnect once
    global guiCalled
    global sGUI
    if not guiCalled:
        print("Connected to client GUI at port 2345")
        guiCalled = True
        sGUI = ctx.socket(zmq.PUSH)
        sGUI.connect(urlGui)
def __CheckWebCalled__():
    # Used to only connnect once
    global webCalled
    global sWeb
    if not webCalled:
        webCalled = True
        sWeb = ctx.socket(zmq.PUSH)
        sWeb.connect(urlWeb)

actions = {
    "GET_CONFIG": __GetConfig__}
clients = {
    "GUI": __CheckGUICalled__,
    "WEB": __CheckWebCalled__}

def check_msg(msg):
    newStr = msg[0].decode("utf-8", "strict")
    if newStr.count(":") == 2:
        [sender, command, data] = newStr.split(":")
        print("Sender: " + sender + ", Command: " + command + ", Data: " + data)
        # connect if not already done
        clients.get(sender, lambda: None)()
        # execute the command sent from client
        actions.get(command, lambda: None)(sender, data)

# register the check_msg callback to be fired
# whenever there is a message on our socket
stream = ZMQStream(sIn)
stream.on_recv(check_msg)

# Setup callback handling the XMOS
tic = time.time()
def xmos_handler():
    # just testing
    print("Loop time: %.3f" % (time.time() - tic))

pc = ioloop.PeriodicCallback(xmos_handler, 200)
pc.start()

# start the eventloop
ioloop.IOLoop.instance().start()


迷你图.py公司名称:

^{pr2}$

一次性启动服务器:

python3.4 miniServer.py

然后是图形用户界面:

python3.4 miniGUI.py

在编辑小部件中写入字符串:

GUI:GET_CONFIG:0

按“提交”按钮将在服务器控制台上打印:

Sender: GUI, Command: GET_CONFIG, Data: 0

Connected to client GUI at port 2345

Sending back config list to GUI

在GUI控制台上

Message GUI:GET_CONFIG:0 sent

在预期结果应为:

Message GUI:GET_CONFIG:0 sent

Message: From Server to GUI received

我做错什么了?在


Tags: toimport服务器configgetiftimedef

热门问题