0
我的腳本是用大多數人[MQL4]不熟悉的語言編寫的,但我已經將python功能導入到代碼中,因此我可以在代碼中執行python代碼劇本。我的問題是,我使用MQL4連接到服務器,我想通過使用python的流發送數據。到目前爲止,我只能讓python創建自己的連接。無論如何連接到已打開的Windows套接字?Python Winsock - 如何通過已經激活的winsock連接發送數據
的MQL代碼
void ircconnect(){
int struct_sockaddr[4];
int addr, port_low, port_high;
int opts[1];
int c;
if (irc_disabled == 1) return(0);
// fill the sockaddr struct
addr = inet_addr(server_addr);
port_low = server_port & 0x00ff;
port_high = (server_port & 0xff00) >> 8;
struct_sockaddr[0] = AF_INET | (port_high << 16) | (port_low << 24);
struct_sockaddr[1] = addr;
struct_sockaddr[2] = 0;
struct_sockaddr[3] = 0;
// connect
s = socket(AF_INET, SOCK_STREAM, 0);
opts[0] = 1000; // send timeout milliseconds
setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, opts, 4);
c = connect(s, struct_sockaddr, 16);
的Python代碼
//Initialize Python Dll
PyInit();
PyExecute("from Tkinter import *");
PyExecute("root = Tk()");
//Import necessary modules
PyExecute("import socket, os");
//Connect
PyExecute("network = \"irc.ircnetwork.com\"");
PyExecute("port = 6667");
PyExecute("irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)");
PyExecute("irc.connect((network, port))");
//Identify
PyExecute("irc.recv(1024)");
PyExecute("irc.send(\\"NICK Nick\\r\\n\")");
PyExecute("irc.send(\\"USER Nick Nick Nick :Nick IRC\\r\\n\")");
PyExecute("irc.send(\\"JOIN #channel\\r\\n\")");
PyExecute("irc.send(\"PRIVMSG NickServ IDENTIFY <pass>\\r\\n\")");
PyExecute("VERSION = \"version>\\r\\n\"");
//Listen for PING
PyExecute("while True:");
PyExecute("data = irc.recv(1024)");
PyExecute("print data");
PyExecute("if data.find(\"PING\") != -1:");
//Send PONG
PyExecute("irc.send(\"PONG \" + data.split() [ 1 ] + \"\\r\\n\")");
PyExecute("elif data.find(\"VERSION\") !=-1:");
PyExecute("irc.send(VERSION + \"\\r\\n\")");
PyExecute("elif data.find(\"NOTICE\") !=-1:");
PyExecute("nick = data.split(\"\!\") [ 0 ].replace (\":\", \"\")");
PyExecute("message = \":\".join(data.split (\":\") [ 2: ])");
可能感興趣的:HTTP: //msdn.microsoft.com/en-us/library/windows/desktop/ms740522%28v=vs.85%29.aspx – mensi 2012-04-26 16:19:11
謝謝。我會檢查出來的。 :) – user1337878 2012-04-26 20:36:37
僅供參考,Python字符串也可以使用單引號,消除了在代碼中如此之多的雙引號。 – 2012-04-27 04:43:50