|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 wnhuiyou 于 2020-3-19 14:18 编辑
我现在想实现这么一个功能,return返回一个值之后,不要结束代码,让它继续在while里面循环,代码如下:
A.pyimport B
t = B.tcp()
while True:
print(t.tcp_server())
B.pyimport socket
class tcp(object):
def tcp_server(slef):
# 创建套接字
slef.tcp_client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 绑定IP和端口
slef.tcp_client_socket.bind(('192.168.0.114', 8081))
# 设置监听
slef.tcp_client_socket.listen(128)
while True:
# 等待连接
slef. new_client_socket, slef.client_addr = slef.tcp_client_socket.accept()
while True:
# 接收客户端发送的请求
slef.recv_data = slef.new_client_socket.recv(1024)
# 回发信息给客户端
slef.new_client_socket.send("成功!".encode("GBK"))
if slef.recv_data.decode("GBK") == "退出":
# 关闭套接字
print("连接断开!")
slef.new_client_socket.close()
return exit()
return slef.recv_data.decode('GBK')
我现在用网络调试工具,发送数据 A.py 能正常打印出来 我输入的数据,但是只能打印一次,因为return后 B.py 就结束了,无法做到 不停发送 不停接收 我了解到可以用迭代器 yield 但是我用了之后没有效果,想请教下大家怎么修改?
我把代码全部粘贴出来了
A.pyfrom ctypes import *
import B.tcp_server
dll = CDLL("D:\python\DLL\proRFL.dll")
class User_Car(object):
def __init__(self, d_12, dlsCoID, CardNo, dai, LLock, pdoors, BDate, EDate, LLockNo):
self.d_12 = d_12
self.dlsCoID = dlsCoID
self.CardNo = CardNo
self.dai = dai
self.LLock = LLock
self.pdoors = pdoors
self.BDate = BDate
self.EDate = EDate
self.LockNo = LLockNo
self.cardHexStr = bytes(72)
# 初始化
def csh(self):
ret = dll.initializeUSB(1) # 打开USB接口
return ret
# 蜂鸣 1秒
def jiao(self):
self.ret = dll.Buzzer(1, 10)
return self.ret
# 发行“客人卡”,又称“宾客卡”或“房卡”
def faka(self):
self.ret = dll.GuestCard(self.d_12, self.dlsCoID, self.CardNo, self.dai, self.LLock, self.pdoors, self.BDate, self.EDate, self.LockNo, self.cardHexStr)
return self.ret
# 读卡数据
def du(self):
self.buffData = bytes(72)
self.ret = dll.ReadCard(self.d_12, self.buffData)
return self.ret
# 读卡锁号
def suohao(self):
self.LockNo = bytes(8)
self.cardHexStr = self.buffData
self.ret = dll.GetGuestLockNoByCardDataStr(self.dlsCoID, self.cardHexStr, self.LockNo)
return self.ret
tcp = DLL.tcp_server.tcp()
p = tcp.tcp_server().split(',')
t = User_Car(p[0], p[1], p[2], p[3], p[4], p[5], p[6].encode('GB2312'), p[7].encode('GB2312'), p[8].encode('GB2312'))
# 发卡
t.faka()
print(t.cardHexStr.decode('UTF-8'))
# 读卡数据
t.du()
print(t.buffData.decode('GB2312'))
B.pyimport socket
# 服务端
class tcp(object):
def tcp_server(self):
# 创建套接字
self.tcp_client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 绑定IP和端口
self.tcp_client_socket.bind(('192.168.0.114', 8081))
# 设置监听
self.tcp_client_socket.listen(128)
while True:
# 等待连接
self. new_client_socket, self.client_addr = self.tcp_client_socket.accept()
while True:
# 接收客户端发送的请求
self.recv_data = self.new_client_socket.recv(1024)
# 回发信息给客户端
self.new_client_socket.send("成功!".encode("GBK"))
if self.recv_data.decode("GBK") == "退出":
# 关闭套接字
print("连接断开!")
self.new_client_socket.close()
return exit()
return self.recv_data.decode('GBK')
yield在循环内使用就能不断返回值
比如
def test():
s = 0
while s < 100:
yield s
s += 1
for i in test():
print(i)
|
|