Forum: Poser Python Scripting


Subject: PoserTalkr: TCP Server for Poser

adp001 opened this issue on Aug 10, 2020 ยท 4 posts


adp001 posted Tue, 11 August 2020 at 4:22 AM

I made the server TCP (not UDP) because it is simpler to deal with large data packages. Such as required to exchange data between Poser and other apps (for me C4D at the moment).

And it is possible to connect to the server with standard apps. Even telnet :)

A basic connection written in Python can look like this:

from __future__ import print_function
import sys
import socket
import time
import threading
import thread

HOST = "192.168.1.215"
PORT = 55555

stopflag = threading.Event()
_tcpsocket = None


def incoming(connection, outstream):
    assert isinstance(connection, socket.socket)
    # We need a timeout to be able to watch stopflag.
    connection.settimeout(0.5)

    while not stopflag.is_set():
        data = None
        try:
            data = connection.recv(1024 * 4)
        except socket.timeout:
            continue  # timeout is fine
        except socket.error as e:
            print(e)
            stopflag.set()

        else:
            if data:
                print("nIN >>> ", data, file=outstream)

    try:
        connection.close()
    except socket.error as e:
        print(e)
        
    print("IN >>> STOPPED.")


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    s.connect((HOST, PORT))
except socket.error as e:
    raise IOError("Can't connect to server %s an port %s" % (HOST, PORT))

thread.start_new_thread(incoming, (s, sys.stdout))

while True:
    try:
        print("n(Ctl-D or Ctl-Z+Return to stop)")
        cmd = raw_input("Cmd to send: ")
    except EOFError:
        break
    else:
        s.sendall(cmd)

stopflag.set()
s.shutdown(socket.SHUT_RDWR)