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