Skip to content

Instantly share code, notes, and snippets.

@offlinemark
Created July 19, 2018 20:45
Show Gist options
  • Save offlinemark/287eb1e3d3094df138d0541e658e6e18 to your computer and use it in GitHub Desktop.
Save offlinemark/287eb1e3d3094df138d0541e658e6e18 to your computer and use it in GitHub Desktop.
import socket
import os
def child(sock):
print 'child running! echo server starting'
while True:
ret = sock.recv(64)
if ret == '':
print 'child server exiting!'
break
sock.sendall('hi ' + ret )
def parent(sock):
print 'parent running!'
print 'heres your control shell'
while True:
inp = raw_input('>')
sock.sendall(inp)
ret = sock.recv(128)
if ret == '':
print 'parent exiting'
break
print 'got back from child:', ret
a, b = socket.socketpair(socket.AF_UNIX, socket.SOCK_DGRAM)
import IPython; IPython.embed()
print 'init running, make the sockets'
print 'forking'
ret = os.fork()
if ret == 0:
# child
child(b)
else:
# parent
parent(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment