I'm trying to communicate an external python script with a C++ program that allows qt-scripting. The goal its to partially control the C++ program (using its qtscript api functions) from python code.
I know how to create a basic socket server/client in python:
#Server code
import socket
server = socket.socket()
server.bind (("localhost", 8080))
server.listen(10)
client, client_adress = server.accept()
while True:
message= cliente.recv(1024)
if message== "quit":
break
print "Received:", message
client.send(message)
print "Goodby"
cliente.close()
server.close()
...
#client code
import socket
client = socket.socket()
client.connect(("localhost", 8080))
while True:
message = raw_input(">" )
client.send(message)
if message == "quit":
break
print "Goodby"
But I can't find much info on how to do it in qtscript (No javascript experience), I know there is the QTcpSocket Class, but I'm not really sure where to start to get a snipet like the python ones I have. There is this question, but it was not useful to my problem. And there is this samples but I can't make it work.
Whats is better, the client or the server in Python? Could I can find a qtscript example?
It sounds like you have a QtScript (Javascript) interpreter running inside your Qt C++ program. And you want a Python client to be able to send commands to it. The simplest (but not the most secure!) way to do this would be to have the Python client connect either with basic TCP or with HTTP POST, send an executable Javascript program across, and have the C++ program execute it internally.
This way the Python side is quite trivial. For the C++ side, you'd first create a QTcpServer
and then when it receives data, read all of it and pass the string to QScriptEngine::evaluate()
. For how to use QTcpServer
, see here: http://doc.qt.io/qt-5/qtnetwork-fortuneserver-example.html