When the fork is carried out, the information of the parent process is copied to the child process, which itself is already a means of communication, that is, the child processes replicate the parent process resources, and in addition to the two processes that want to communicate, what is the method. You can use the Socketpair method.
My suspicion is that the descriptor returned by Socketpair has no difference between the server and the client.
#-*-Coding:utf-8-*-
Import socket
import OS
import time
parent, child = Socket.socketpair (socket.af_ UNIX, Socket. SOCK_STREAM)
# Fork's instant father and son are the same
pid = Os.fork ()
if PID:
# in parent
child.close ()
Parent.sendall (' Hello, I am parent ')
response = PARENT.RECV (1024)
print "I am in parent", Os.getpid ()
pri NT Response
Time.sleep (0.1)
parent.close ()
# Print Parent.fileno ()
else:
# child process
parent.close ()
print "I am in Child", Os.getpid ()
child.sendall (' Hello, I am child ') message
= Chil D.RECV (1024)
print message
time.sleep (0.1)
child.close ()
A UDP based server, combined with the use of NC, can build a simple shell
#-*-Coding:utf-8-*-
Import socket
import/sys
import commands
import OS
# The original UDP-based shell
server_address = (' localhost ', 8000)
sock = Socket.socket (socket.af_inet, socket. SOCK_DGRAM)
sock.bind (server_address)
while True:
data, client = Sock.recvfrom (1024)
if "quit" not In data: If
data.strip ():
# If the data here is from the web, then I have implemented a Python based Webshell, which is not yet able to handle interactive
Sock.sendto (Commands.getoutput (Data.strip ()) + os.linesep, client)
else:
pass
else:
break
Sys.exit ()