12 def receiveExactly(connection
, size
):
15 dataReceived
= connection
.recv(size
)
19 size
-= len(dataReceived
)
23 def handleConnection(listenerSocket
):
24 (controlConnection
, controlAddress
) = listenerSocket
.accept()
25 (stdioConnection
, stdioAddress
) = listenerSocket
.accept()
26 (stderrConnection
, stderrAddress
) = listenerSocket
.accept()
28 print 'accepted client connections'
31 commandLength
= receiveExactly(controlConnection
, 8)
32 commandToRun
= receiveExactly(controlConnection
, int(commandLength
))
34 print 'received command: ' + commandToRun
36 exitCode
= subprocess
.call(commandToRun
, stdin
=stdioConnection
,
37 stdout
=stdioConnection
, stderr
=stderrConnection
, shell
=True)
39 controlConnection
.send(str(exitCode
))
41 controlConnection
.close()
42 stdioConnection
.close()
43 stderrConnection
.close()
45 print 'client connections closed'
48 listenerSocket
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
49 listenerSocket
.setsockopt(socket
.SOL_SOCKET
, socket
.SO_REUSEADDR
, 1)
52 listenerSocket
.bind((address
, port
))
53 except socket
.error
, msg
:
54 sys
.exit('Failed to bind to %s port %d: %s' % (address
, port
, msg
[1]))
56 listenerSocket
.listen(3)
58 print 'started listening on adddress %s port %s' % (address
, port
)
61 handleConnection(listenerSocket
)