我希望标题合适。如果没有,请建议替代方案。我正在使用以下Python客户端类。
import Queue
import socket
import struct
import threading
import time
class ClientCommand(object):
CONNECT, SEND, RECEIVE, CLOSE = range(4)
def __init__(self, type, data=None):
self.type = type
self.data = data
class ClientReply(object):
ERROR, SUCCESS = range(2)
def __init__(self, type, data = None):
self.type = type
self.data = data
class SocketClientThread(threading.Thread):
def __init__(self, cmd_q = Queue.Queue(), reply_q = Queue.Queue()):
super(SocketClientThread, self).__init__()
self.cmd_q = cmd_q
self.reply_q = reply_q
self.alive = threading.Event()
self.alive.set()
self.socket = None
#self.stopped = False
self.handlers = {
ClientCommand.CONNECT: self._handle_CONNECT,
ClientCommand.CLOSE: self._handle_CLOSE,
ClientCommand.SEND: self._handle_SEND,
ClientCommand.RECEIVE: self._handle_RECEIVE
}
def run(self):
while self.alive.isSet():
#while not self.stopped:
try:
cmd = self.cmd_q.get(True, 0.1)
self.handlers[cmd.type](cmd)
except Queue.Empty as e:
continue
def stop(self):
self.alive.clear()
def join(self, timeout=None):
self.alive.clear()
threading.Thread.join(self, timeout)
def _handle_CONNECT(self, cmd):
try:
self.socket = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((cmd.data[0], cmd.data[1]))
self.reply_q.put(self._success_reply())
except IOError as e:
self.reply_q.put(self._error_reply(str(e)))
def _handle_CLOSE(self, cmd):
self.socket.close()
reply = ClientReply(ClientReply.SUCCESS)
self.reply_q.put(reply)
def _handle_SEND(self, cmd):
try:
print "about to send: ", cmd.data
self.socket.sendall(cmd.data)
print "sending data"
self.reply_q.put(self._success_reply())
except IOError as e:
print "Error in sending"
self.reply_q.put(self._error_reply(str(e)))
def _handle_RECEIVE(self, cmd):
try:
#TODO Add check for len(data)
flag = True
while flag:
print "Receiving Data"
data = self._recv_n_bytes()
if len(data) != '':
self.reply_q.put(self._success_reply(data))
if data == "Stop":
print "Stop command"
flag = False
except IOError as e:
self.reply_q.put(self._error_reply(str(e)))
def _recv_n_bytes(self):
data = self.socket.recv(1024)
return data
def _error_reply(Self, errstr):
return ClientReply(ClientReply.ERROR, errstr)
def _success_reply(self, data = None):
return ClientReply(ClientReply.SUCCESS, data)
我的主要脚本代码 -
import socket
import time
import Queue
import sys
import os
from client import *
sct = SocketClientThread()
sct.start()
host = '127.0.0.1'
port = 1234
sct.cmd_q.put(ClientCommand(ClientCommand.CONNECT, (host, port)))
try:
while True:
sct.cmd_q.put(ClientCommand(ClientCommand.RECEIVE))
reply = sct.reply_q
tmp = reply.get(True)
data = tmp.data
if data != None:
if data != "step1":
//call function to print something
else:
// call_function that prints incoming data till server stops sending data
print "Sending OK msg"
sct.cmd_q.put(ClientCommand(ClientCommand.SEND, "Hello\n"))
print "Done"
else:
print "No Data"
except:
#TODO Add better error handling than a print
print "Server down"
所以这就是问题所在。一旦线程启动,并且调用了接收处理程序,我得到一些数据,如果该数据不是" Step1",我只是调用一个函数(另一个脚本)来打印它。
但是,如果数据是" step1",我会调用一个函数,然后继续打印服务器接下来发送的任何数据,直到服务器发送"停止"信息。在这一点上,我打破了接收处理程序"并尝试发送" Ok"消息到服务器。
但是,只要我退出"接收处理程序",它就会再次自动调用该功能。因此,当我尝试发回消息时,客户端再次等待来自服务器的数据。所以由于"接收器功能"被再次呼叫,"发送功能"块。
我似乎无法理解如何在接收和发送之间切换。我的方法有什么问题,我应该如何解决这个问题?我是否需要重新编写代码才能有两个独立的线程用于发送和接收?
如果您需要更多详细信息,请在决定无缘无故地提出问题之前告知我们。
答案 0 :(得分:1)
但是,一旦我突破“接收处理程序”,它就会出现 再次自动调用该功能。
这是因为你在sct.cmd_q.put(ClientCommand(ClientCommand.RECEIVE))
循环内调用了while True
,这个循环是针对收到的每一块数据运行的,i。即对于“step1”之前的每个数据,将另一个调用“Receive Handler”的命令(它本身循环直到“Stop”)放入ClientCommand队列,然后这些命令在SEND命令之前执行。如果您在此while True
循环之前放置RECEIVE调用,则您的方法可以正常工作。
答案 1 :(得分:0)
错误是
if msgid != "step1":
NameError: name 'msgid' is not defined
而不是
#TODO Add better error handling than a print
print "Server down"
你最好写
raise
并立即发现它。