lynch_ppl Posted August 27, 2006 Share Posted August 27, 2006 Hello all, The script I am referencing is here: http://odforce.net/wiki/index.php/PythonToHoudini It opens up a socket and send data back and forth from Python to Houdini. I am attempting to make a class out of some of it... The first time I run hou.command(), it works. The second time I try to repeat a hou.command('some hscript here'), I get an error: >>> import hou >>> new=hou.hou() >>> new.command('ls') geo1 geo2 geo3 geo4 >>> new.command('ls') Traceback (most recent call last): File "<stdin>", line 1, in ? File "hou.py", line 34, in command self.hou_conn() File "hou.py", line 10, in hou_conn self.hou.connect(('localhost',12000)) File "<string>", line 1, in connect File "/usr/lib/python2.4/socket.py", line 144, in _dummy raise error(EBADF, 'Bad file descriptor') socket.error: (9, 'Bad file descriptor') any ideas of what is happening? any help is much appreciated! Here is the class: #!/usr/bin/python class hou: import socket hou = socket.socket(socket.AF_INET, socket.SOCK_STREAM) def hou_conn(self): self.hou.connect(('localhost',12000)) return() def send_data(self,command): self.hou.send(command + '\n') return() def get_data(self): delimiter = '\x00' buffer = "" while delimiter not in buffer: buffer = buffer + self.hou.recv(8192) return(buffer[:-2]) def hou_close(self): self.hou.close() return() def command(self,command): self.hou_conn() self.send_data(command) buffer = self.get_data() print buffer self.hou_close() Quote Link to comment Share on other sites More sharing options...
TheUsualAlex Posted August 28, 2006 Share Posted August 28, 2006 Hello, It seems like problems is occuring in your command method where you are calling self.hou_conn() the second time around. Perhaps your socket wasn't closed or something that's causing the problem. What I did was to comment out your self.hou_conn() and self.hou_close() in your command method, and then when you're actually running your class, do something like: new = hou() new.hou_conn() new.command('ls') new.command('ls') new.hou_close() That seems to work. I think that it might be a good idea to actually do your hou_conn() call inside the hou constructor ( def __init__(self): ), so that whenever you instantiate your class object, hou_conn() get called automatically, you run your commands until you are done, then manually call your hou_close() method. That way, you are not constantly opening and closing your connections whenever command method get called. Anyways, about a year ago while I was just starting to learn python, I put together this version of Houdini socket (as an exercise), I never got around to finishing other funcationalities I wanted to add for this module and hasn't even touched this file since then... Perhaps it might be helpful to you? Hope this helped. Alex classHoudini.py.gz Quote Link to comment Share on other sites More sharing options...
lynch_ppl Posted August 29, 2006 Author Share Posted August 29, 2006 Alex, Much appreciated. That definetly helped out! Thank you thank you. Here is the newly updatred class: #!/usr/bin/python class hou: import socket hou = socket.socket(socket.AF_INET, socket.SOCK_STREAM) def __init__(self): self.hou_conn() print 'Connected to Houdini...' def hou_conn(self): self.hou.connect(('localhost',12000)) return() def send_data(self,command): self.hou.send(command + '\n') return() def get_data(self): delimiter = '\x00' buffer = "" while delimiter not in buffer: buffer = buffer + self.hou.recv(8192) return(buffer[:-2]) def hou_close(self): self.hou.close() return() def command(self,command): self.send_data(command) buffer = self.get_data() print buffer Quote Link to comment Share on other sites More sharing options...
lynch_ppl Posted August 29, 2006 Author Share Posted August 29, 2006 Here is a term class that emulates the textport... #!/usr/bin/python class hou: import socket hou = socket.socket(socket.AF_INET, socket.SOCK_STREAM) def __init__(self): self.hou_conn() print 'Connected to Houdini...' def hou_conn(self): self.hou.connect(('localhost',12000)) return() def send_data(self,command): self.hou.send(command + '\n') return() def get_data(self): delimiter = '\x00' buffer = "" while delimiter not in buffer: buffer = buffer + self.hou.recv(8192) return(buffer[:-2]) def hou_close(self): self.hou.close() return() def command(self,command): self.send_data(command) buffer = self.get_data() print buffer class term(hou): def __init__(self): hou.__init__(self) print 'type "exit" to close connection...' while 1: self.pwd() self.hpwd = "%s%s" % (self.hpwd, '/->') line = raw_input(self.hpwd) if line == 'exit': self.hou.close() print 'Connection closed...' break self.command(line) def pwd(self): self.send_data('pwd') buffer = self.get_data() self.hpwd = buffer Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.