Python implements a simple instance of interprocess communication _python

Source: Internet
Author: User

This article illustrates how Python implements communication between two programs, in the following ways:

This example uses socket realization, and socket network programming is not the same as Socket.socket (Socket.af_unix, socket. SOCK_STREAM) is the first parameter of the Socket.af_unix
And not socket.af_inet.
Example of two Python program s.py/c.py to run first s.py
Based on fedora13/python2.6 test, successful implementation!

The s.py code is as follows:

#!/usr/bin/env python
Import socket
import OS

if __name__ = = ' __main__ ':
  sock = socket.socket (socket. Af_unix, Socket. SOCK_STREAM)
  conn = '/tmp/conn '
  if not os.path.exists (conn):
  Os.mknod (conn)
  if os.path.exists (conn) :
  Os.unlink (conn)
  Sock.bind (conn)
  Sock.listen (5) while
  True:
  connection,address = Sock.accept ()
  data = CONNECTION.RECV (1024)
  if data = "Hello,server":
    print "The client said:%s!\n"% Data
      connection.send ("hello,client")
  Connection.close () 

The c.py code is as follows:

 #!/usr/bin/env python import socket import time if __name__ = ' __main__ ': sock = Socke T.socket (Socket.af_unix, socket. 
  SOCK_STREAM) conn = '/tmp/conn ' sock.connect (conn) time.sleep (1) sock.send (' Hello,server ') print SOCK.RECV (1024) Sock.close () 

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.