Web: support to query values via TCP/IP communication

This commit is contained in:
wmayer
2021-09-25 19:52:14 +02:00
parent bd46e970d9
commit 5387e12e80
2 changed files with 33 additions and 1 deletions

View File

@@ -112,6 +112,13 @@ const QByteArray& ServerEvent::request() const
AppServer::AppServer(QObject* parent)
: QTcpServer(parent)
{
PyObject* mod = PyImport_ImportModule("__main__");
if (mod) {
module = mod;
}
else {
throw Py::RuntimeError("Cannot load __main__ module");
}
}
void AppServer::incomingConnection(qintptr socket)
@@ -147,12 +154,35 @@ void AppServer::customEvent(QEvent* e)
QByteArray msg = ev->request();
QTcpSocket* socket = ev->socket();
std::string str = runPython(msg);
std::string str;
if (msg.startsWith("GET ")) {
msg = QByteArray("GET = ") + msg.mid(4);
str = runPython(msg);
if (str == "None") {
str = getRequest(str);
}
}
else {
str = runPython(msg);
}
socket->write(str.c_str());
socket->close();
}
std::string AppServer::getRequest(const std::string& str) const
{
try {
Base::PyGILStateLocker lock;
Py::Object attr = module.getAttr(std::string("GET"));
return attr.as_string();
}
catch (Py::Exception &e) {
e.clear();
return str;
}
}
std::string AppServer::runPython(const QByteArray& msg)
{
std::string str;

View File

@@ -89,12 +89,14 @@ public:
protected:
void customEvent(QEvent* e);
std::string getRequest(const std::string&) const;
private Q_SLOTS:
void readClient();
void discardClient();
private:
Py::Object module;
};
}