I’m messing around with trying to send data to MoBu’s default telnet port from an external application. I’ve been able to get this example using telnetlib to work, but now I feel determined to understand why I cannot get the following implementation using socket to work:
So I’ve made some headway on this topic. Still some work to do but it’s getting there. I’m able to connect and send commands to MoBu with the code below. Every so often the code will randomly decide to report an error… so there’s something still off with timing or synchronization, or something I’ve yet to discover.
I’ve messed around with different newline combos, but I haven’t found any one combo to solve the inconsistency of execution.
I have figured out that MoBu doesn’t want any commands sent to it before the command prompt is ready for more input, so you have to read, wait, write. This seems to differ slightly from the way Maya appears to work. I started this venture with Eric Pavey’s WingIDE to Maya implementation and wound up here:
import socket
host = 'localhost'
port = 4242
newline = '
\r'
prompt = '>>> '
# These are the example commands we want to send. This is not the ideal
# method of command gathering, but serves as a placeholder example.
cmds = ['import pyfbsdk as fb',
'myMarker = fb.FBModelMarker("marker")',
'myMarker.Show = True',
'myMarker.Selected = True']
cmds = [cmd + newline for cmd in cmds]
print(repr(cmds))
# Open a socket connection to MoBu at localhost:4242
try:
# Establish a connection socket
try:
skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
skt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
except socket.error, e:
print ("Error creating socket: %s" % e)
# Perform the connection
try:
skt.connect((host, port))
print 'Connected to Mobu!'
except socket.gaierror, e:
print("Address-related error connecting to server: %s" % e)
except socket.error, e:
print("Error connecting to socket: %s" % e)
# Send MoBu the data
try:
# first we must wait for the command prompt
while True:
data = skt.recv(1024)
if data.find(prompt):
break;
# then loop through all commhost
for cmd in cmds:
# sending each command separately, making sure that
# each command ends with newline in order to trigger
skt.send(cmd)
while True:
data = skt.recv(1024)
if data == prompt:
break
else:
# print the return output
# Note: repr(data) shows us that output lines end in '
\r'
for d in data.split('
\r'):
print d.rstrip()
except socket.error, e:
print 'Error sending data: %s' % e
finally:
skt.close()
print 'Connection to MoBu closed.'
You’ll greatly benefit from dropping this low level stuff and working with something like ZeroMQ or a similar higher-level socket construct. Avoid these problems entirely.
[QUOTE=Rob Galanakis;15944]You’ll greatly benefit from dropping this low level stuff and working with something like ZeroMQ or a similar higher-level socket construct. Avoid these problems entirely.[/QUOTE]
Yeah, Randall Hess mentioned that to me as well. I’ll probably save time learning a new package instead of digging deeper into this stuff. I really liked the idea of not having another package to distribute though.
-csa