How to convert communication form from Java to Python

Asked

Viewed 262 times

0

Good Afternoon,

I have a Java scrip that communicates between the Java application and the MIO Commbox controller.

My question is how to convert this Java script to Python, summarizing, need to perform communication via python.

Follow the JAVA Script:

 URLConnection connection =  new URL("http://192.168.0.8:4091/multiio").openConnection();
            connection.setDoOutput(true);
            connection.setRequestProperty("Accept-Charset", "UTF-8");
            connection.setRequestProperty("Content-Type", "application/json");

            try( OutputStream output = connection.getOutputStream()){ 
                output.write("{\"event\": \"read_inputs_outputs\", \"ip\" : \"192.168.0.100\", \"port\" : \"4091\", \"data\" : []}".getBytes("UTF-8"));
            }         

BufferedReader response = new BufferedReader( new InputStreamReader(connection.getInputStream()));

            String text = "";
            String json = "";
            while((text = response.readLine()) != null){
                       json += text;
                        }

I need to write this same code in Python, I started the encoding as below, but I’m not getting the answer

Initial condification in Python

import socket

sockt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sockt.connect(('192.168.0.8', 4091))
print("Conectado...")

data = bytearray()
payload = '{\"event\":\"read_inputs_outputs\",\"ip\":\"192.168.0.100\",\"port\":\"4091\",\"data\":\"[]\"}'
data = payload.encode('utf-8')

sockt.send(data)

resposta = sockt.recv(4091)
print(resposta)

I only printed on the console "Connected..." and nothing else appears on the console, no errors.

Can anyone tell me what I should do? Thank you!

  • The call sockt.send(data) had some return?

  • Anderson ran print(sockt.send(data)) and returned the number 78 which must be the payload size

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.