我已经创建了一个类似于以下内容的套接字文件,并希望该套接字的输出必须由MQL5读取。请参阅以下Python代码:
daemon.py
import socket
#import arcpy
def actual_work():
#val = arcpy.GetCellValue_management("D:\dem-merged\lidar_wsg84", "-95.090174910630012 29.973962146120652", "")
#return str(val)
return 'dummy_reply'
def main():
sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
try:
sock.bind( ('127.0.0.1', 6666) )
while True:
data, addr = sock.recvfrom( 4096 )
reply = actual_work()
sock.sendto(reply, addr)
except KeyboardInterrupt:
pass
finally:
sock.close()
if __name__ == '__main__':
main()
client.py
import socket
import sys
def main():
sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
sock.settimeout(1)
try:
sock.sendto('', ('127.0.0.1', 6666))
reply, _ = sock.recvfrom(4096)
print reply
except socket.timeout:
sys.exit(1)
finally:
sock.close()
if __name__ == '__main__':
main()
请帮助我通过MQL5接受套接字的输出
编辑过
我只希望
reply
应该在daemon.py
产生的变量中的MQL5上被接受。我该怎么做?假设我希望MQL5应该打印来自Python的响应,如上例所示,我希望MQL5应该在字符串变量中以dummy_reply
的形式给出输出。ZeroMQ是否有可能?
我想使用MQL5而不是使用Python来完成
client.py
。请帮我。 最佳答案
请找到一个正在运行的示例。重要元素是在作为回复发送之前,创建有效负载的字节对象而不是字符串。套接字对象仅产生并提取字节
import socket
import threading
import sys
def actual_work(data):
print(data)
return b'ACK'
def daemon():
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('127.0.0.1', 6666))
print("Listening on udp %s:%i" % ('127.0.0.1', 6666))
try:
while True:
data, addr = sock.recvfrom(4096)
ack = actual_work(data)
sock.sendto(ack, addr)
except Exception as e:
print(e)
finally:
sock.close()
def client():
sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
sock.settimeout(1)
try:
sock.sendto(b'payload', ('127.0.0.1', 6666))
reply, _ = sock.recvfrom(4096)
print(reply)
except socket.timeout as e:
print(e)
sys.exit(1)
finally:
sock.close()
if __name__ == '__main__':
thread = threading.Thread(target=daemon)
thread.start()
client()
client()
client()
client()
#Issue kill thread here
thread.join()