我尝试分别提供主机和端口信息,但出现问题中提到的错误。原因是因为zmq仅采用地址之类的链接,例如'tcp://192.X.X.X:5643'。因此,我无法自己提供主机地址,也不能只输入主机地址。我想单独提供主机地址,因为我是从其他函数获取主机地址的,因此更易于传递给变量。
代码如下:
def req_con:
Context=zmq.Context()
socket=context.socket(zmq.REQ)
aad="192.x.x.x"
port =8574
host=["%s".format(aad,port)]
for adres in host:
socket.connect("tcp://" %adres) ---> error is here
socket.send_string("get")
已更新:
def req_con:
..
..
host:['{p.aad}:{p.port}'.forma(p=req_con())]
我试图做得更好,但现在我在调用python对象错误时获得了最大递归深度超过
答案 0 :(得分:0)
通过
解决host="192.x.x.X"
port=...
socket.connect("tcp://" "%s:%d" %(format(host),port))
答案 1 :(得分:0)
.format()
是使用字符串的简单方法:
host = "192.x.x.x"
port = 1234
socket.connect("tcp://{}:{}".format(host, port))