Skip to content

Instantly share code, notes, and snippets.

@splitline
Created October 14, 2017 15:45
Show Gist options
  • Save splitline/e59eb17aceede3dcde2d51724796f97f to your computer and use it in GitHub Desktop.
Save splitline/e59eb17aceede3dcde2d51724796f97f to your computer and use it in GitHub Desktop.
import socket
import sys
import subprocess
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('localhost', 11112)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)
sock.listen(1)
while True:
print >>sys.stderr, 'waiting for a connection'
connection, client_address = sock.accept()
try:
print >>sys.stderr, 'connection from', client_address
connection.sendall("> ")
while True:
data = connection.recv(16)
print >> sys.stderr, 'received "%s"' % data.strip()
if data:
print >> sys.stderr, 'sending data back to the client'
p = subprocess.Popen(data, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = p.communicate()
if out:
connection.sendall(out + "\n> ")
if err:
connection.sendall(err + "\n> ")
else:
print >>sys.stderr, 'no more data from', client_address
break
finally:
# Clean up the connection
connection.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment