Python's parent-child interprocess communication __python

Source: Internet
Author: User

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 ()




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.