我在尝试实现用python的socket库和mt5自带的socket来实现传递mysql数据的信息,但是python那边的服务端收得到socket发送的数据,mt5能成功连接成功发送但接受数据时一直提示5273错误,尝试过更改发送的编码和发送的数据但一直没有用,有大佬知道为什么吗?
这是mt5的代码,用的是帮助文档里写的。
class HangStrategy {
public:
datetime lastBarTime;//最新k线开盘时间
datetime Time[];
bool ExtTLS;
virtual void init() {
ExtTLS = false;
}
//运行EA
virtual void run() {
//初始化信号系统
ArraySetAsSeries(Time, true);
CopyTime(_Symbol, _Period, 0, 10, Time);
//开仓时点
if(lastBarTime < Time[0]) {
//上次K线时间小于当前K线时间,新K线
lastBarTime = Time[0];
Print("------------------开始测试------------------");
int socket = SocketCreate();
//--- 检查句柄
if(socket != INVALID_HANDLE) {
//--- 一切顺利情况下继续连接
if(SocketConnect(socket, Address, Port, 1000)) {
Print("Established connection to ", Address, ":", Port);
string subject, issuer, serial, thumbprint;
datetime expiration;
//--- 如果连接受到证书保护,则显示其数据
if(SocketTlsCertificate(socket, subject, issuer, serial, thumbprint, expiration)) {
Print("TLS certificate:");
Print(" Owner: ", subject);
Print(" Issuer: ", issuer);
Print(" Number: ", serial);
Print(" Print: ", thumbprint);
Print(" Expiration: ", expiration);
ExtTLS = true;
}
//--- 向服务器发送GET请求
//GET / HTTP/1.1\r\nHost: www.mql5.com\r\nUser-Agent: MT5\r\n\r\n
if(HTTPSend(socket, "get_data")) {
Print("GET request sent");
//--- 读取响应
if(!HTTPRecv(socket, 1000))
Print("Failed to get a response, error ", GetLastError());
} else
Print("Failed to send GET request, error ", GetLastError());
} else {
Print("Connection to ", Address, ":", Port, " failed, error ", GetLastError());
}
//--- 用后关闭套接
SocketClose(socket);
} else
Print("Failed to create a socket, error ", GetLastError());
}
}
//+------------------------------------------------------------------+
//| 向服务器发送命令 |
//+------------------------------------------------------------------+
bool HTTPSend(int socket, string request) {
char req[];
int len = StringToCharArray(request, req) - 1;
if(len < 0)
return(false);
//--- 如果通过443端口使用安全TLS连接
if(ExtTLS)
return(SocketTlsSend(socket, req, len) == len);
//--- 如果使用标准的TCP连接
return(SocketSend(socket, req, len) == len);
}
//+------------------------------------------------------------------+
//| 读取服务器响应 |
//+------------------------------------------------------------------+
bool HTTPRecv(int socket,uint timeout) {
char rsp[];
string result;
uint timeout_check=GetTickCount()+timeout;
//--- 读取套接数据,直至套接数据不长过超时且仍然存在
do {
int len=SocketIsReadable(socket);
if(len) {
int rsp_len;
//--- 各种读取命令,取决于连接是否安全
if(ExtTLS)
rsp_len=SocketTlsRead(socket,rsp,len);
else
rsp_len=SocketRead(socket,rsp,len,timeout);
//--- 分析响应
if(rsp_len>0) {
result+=CharArrayToString(rsp,0,rsp_len);
//--- 只打印响应标题
int header_end=StringFind(result,"\r\n\r\n");
if(header_end>0) {
Print("HTTP answer header received:");
Print(StringSubstr(result,0,header_end));
return(true);
}
}
}
}
while(GetTickCount()<timeout_check && !IsStopped());
return(false);
}
};
input string Address = "127.0.0.1"; //socket地址
input int Port = 5001; //端口号
//实例化
HangStrategy hangStrategy;
//初始化脚本的时候执行一次
int OnInit() {
hangStrategy.init();
return(INIT_SUCCEEDED);
}
//价格每次变化执行一次
void OnTick() {
hangStrategy.run();
}
复制
这是python服务端的代码
def handle_client(conn, addr, conn_db, data):
print(f"Connected with {addr}") #
print(f"Received {data}")
# conn.send("Data received and processed.".encode('utf-8'))
result = get_data(conn_db)
# result2 = bytes('hello', 'utf-8')
try:
conn.send(str(result).encode())
print(f"Sent\r\n\r\n")
except Exception as e:
print(f"Error during sending data: {e}")
print(e)
finally:
conn.close()
复制
mt5提示的错误信息
谢谢。