python之父子處理序間通訊__python

來源:互聯網
上載者:User

當進行fork的時候,父父進程的資訊會複製到子進程,這本身已經是一種通訊方式了,即子進程複製父進程資源,除此之外,還想讓這兩個進程進行通訊,有什麼方法呢。可以使用socketpair的方式。


我的疑惑是socketpair返回的描述符有沒有服務端與用戶端的區別。

# -*- coding:utf-8 -*-import socketimport osimport timeparent, child = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM)# fork的一刹那父子是相同的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()    print 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 = child.recv(1024)    print message    time.sleep(0.1)    child.close()    


一個基於udp的server,結合nc使用,可以構建一個簡單的shell

# -*- coding:utf-8 -*-import socketimport sysimport commandsimport os# 最原始的基於udp協議的shellserver_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():            # 假如這裡的data是從web來的,那麼這樣我就實現了一個基於python的webshell,目前這個shell還不能處理互動式            sock.sendto(commands.getoutput(data.strip()) + os.linesep, client)        else:            pass    else:        breaksys.exit()




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.