Read data from TCP Printer

Hi,
I am trying to read data from a TCP-connected printer. The connection has been verified using PowerShell, and the printer is reachable. However, when attempting to communicate via Python or from Ignition, the connection fails even after restarting the printer.

Here is the Python script I used:

import socket

host = "<Printer_IP>"  
port = <Printer_Port>  

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((host, port))
    print("Connection Successful!")

    command = "\n"  # Sending a newline to test response
    s.sendall(command.encode())

    response = s.recv(4096).decode()
    print("Printer Response:", response)

    s.close()
except Exception as e:
    print("Connection Failed:", e)

Despite the TCP connection test passing in PowerShell, the script fails to establish communication. Ignition also reports a connection failure.

What's the exact error and stack trace?

Where are you executing this script in Ignition and what computer was this verified on with powershell?

I have verified it in the gateway server:
ComputerName : host
RemoteAddress :host
RemotePort : 3000
InterfaceAlias : Ethernet
SourceAddress : gateway ip
TcpTestSucceeded : True

and am trying it from script console /gateway event
Error:('Connection Failed:', error(107, 'Connection refused'))