0

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?

vvvvv
  • 25,404
  • 19
  • 49
  • 81

1 Answers1

0

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

vvvvv
  • 25,404
  • 19
  • 49
  • 81
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Thanks John!! You are right on the goal, I start testing some things and build a prototype: https://github.com/hasielhassan/Python2QtScript-SocketServer/tree/master/code But I'm struggling with the QByteArray data type and validate the commands I send. I think that I need to convert it to a string but I found it more complex as I expected, the respective lines are the following: https://github.com/hasielhassan/Python2QtScript-SocketServer/blob/master/code/SocketServer.js#L79 Do you have any clues on how to proceed with this? Its there other way to validate the data to react accordingly? – hasielhassan Apr 22 '16 at 16:21
  • @hasielhassan: You're welcome. I'm not sure what you're asking now. If you get stuck you should consider posting a new question with those details. – John Zwinck Apr 23 '16 at 00:31
  • Hi again, at the end I figure out a way to get the values of the QByteArray into a string, maybe its not the best, but its working... https://github.com/hasielhassan/Python2QtScript-SocketServer/commit/8d9a18c8677769cb459ceb459d5bcc4e94155545 – hasielhassan Apr 23 '16 at 05:38