Image through a socket to Python

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By user123

I would like to know how to send a screenshot of the game to a python program through a socket connection.

I already know how to get the image:

var img = get_viewport().get_texture().get_data()
yield(get_tree(),“idle_frame”)

The UDP interconnection is ok, but the tcp for to sent the data is not connecting. My first trial was to sent a string, where my python server is:

import socket

HOST = ‘127.0.0.1’
PORT = 4343

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
orig = (HOST, PORT)
sock.bind((HOST,PORT))
sock.listen()
print(“Listening”)
conn, addr = sock.accept()
print ('Connected to: ', addr)

while conn:
data = conn.recv(5)
if len(data) == 0:
break
print("Received: ", data.decode(‘utf-8’))
sock.close()

My godot client is:

var socketTCP

var thread
var msg = “>”
var ip
var port

func _ready():
thread = Thread.new()
socketTCP = StreamPeerTCP.new()

icon.position.x = get_viewport().size.x/2
icon.position.y = get_viewport().size.y/2
randomize()
vel_x = rand_range(5,20)
vel_y = rand_range(5,20)

print("v_x: " + str(vel_x))
print("v_y: " + str(vel_y))
move_obj.wait_time = interval
move_obj.start()

if thread.is_active():
	print("thread already active")
else:
	print("starting thread ...")

thread.start(self,"connect_server","values")

print("Thread ID: " + str(thread.get_id()))

if thread.is_active():
	print("thread is working!!!")
else:
	print("thread not working!!!")
pass

func connect_server(values):
var done = false

ip = "127.0.0.1"
port = 4343

socketTCP.set_no_delay(true)

var connection
while !done:
	print("connecting ...")
	connection = socketTCP.connect_to_host("127.0.0.1",4343)
	done = socketTCP.is_connected_to_host()
	print(done)

socketTCP.put_utf8_string("Test of connection")

socketTCP.disconnect_from_host()
print("Connection finnished!!!")

Any idea of how to sent the img through this connection?