Package nukescripts :: Module clientserver
[hide private]
[frames] | no frames]

Source Code for Module nukescripts.clientserver

 1  # Copyright (c) 2009 The Foundry Visionmongers Ltd.  All Rights Reserved. 
 2   
 3  import socket, threading 
 4  import nuke 
 5  from nukescripts import utils 
 6   
 7  HOST='localhost' 
 8  PORT=50007 
 9   
10   
11 -class client():
12 """ 13 Example of running an IPV6 socket client to create nodes in Nuke. 14 15 from nukescripts import clientserver 16 c = clientserver.client() 17 c.send("Blur") 18 """ 19
20 - def __init__(self, host = HOST, port = PORT):
21 for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE): 22 af, socktype, proto, canonname, sa = res 23 try: 24 self.s = socket.socket(af, socktype, proto) 25 except socket.error, msg: 26 self.s = None 27 continue 28 try: 29 self.s.connect(sa) 30 except socket.error, msg: 31 self.s.close() 32 self.s = None 33 continue 34 break 35 36 if not self.s: raise RuntimeError, "Unable to initialise client."
37
38 - def send(self, msg):
39 self.s.send(msg)
40
41 - def close(self):
42 self.s.close()
43 44
45 -class server():
46 """ 47 Example of running an IPV6 socket server on a separate thread inside Nuke. 48 The default command is to create the named node. 49 50 from nukescripts import clientserver 51 clientserver.threaded_server() 52 """ 53
54 - def __init__(self, host = HOST, port = PORT):
55 for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE): 56 af, socktype, proto, canonname, sa = res 57 try: 58 self.s = socket.socket(af, socktype, proto) 59 except socket.error, msg: 60 self.s = None 61 continue 62 try: 63 self.s.bind(sa) 64 self.s.listen(1) 65 except socket.error, msg: 66 self.s.close() 67 self.s = None 68 continue 69 break 70 71 if not self.s: raise RuntimeError, "Unable to initialise server."
72
73 - def start(self):
74 (conn, addr) = self.s.accept() 75 print "Connection from ", addr 76 while 1: 77 data = conn.recv(1024) 78 if not data: break 79 print "Command ", data 80 utils.executeInMainThread(nuke.createNode, (data,)) 81 conn.close()
82
83 -def start_server(host = HOST, port = PORT):
84 s = server(host, port) 85 s.start()
86
87 -def threaded_server():
88 t = threading.Thread(None, start_server) 89 t.start()
90