Server - Client IPC implementation in Python
Simple Socket
In the following code, the server sends the current time string to the client:
1 # server.py 2 import socket 3 import time 4 5 # create a socket object 6 serversocket = socket.socket( 7 socket.AF_INET, socket.SOCK_STREAM) 8 9 # get local machine name 10 host = socket.gethostname() 11 12 port = 9999 13 14 # bind to the port 15 serversocket.bind((host, port)) 16 17 # queue up to 5 requests 18 serversocket.listen(5) 19 20 while True: 21 # establish a connection 22 clientsocket,addr = serversocket.accept() 23 24 print("Got a connection from %s" % str(addr)) 25 currentTime = time.ctime(time.time()) + " " 26 clientsocket.send(currentTime.encode('ascii')) 27 clientsocket.close()
Here is the summary of the key functions from socket - Low-level networking interface:
socket.socket(): Create a new socket using the given address family, socket type and protocol number.
socket.bind(address): Bind the socket to address.
socket.listen(backlog): Listen for connections made to the socket. The backlog argument specifies the maximum number of queued connections and should be at least 0; the maximum value is system-dependent (usually 5), the minimum value is forced to 0.
socket.accept(): The return value is a pair (conn, address) where conn is a new socket object usable to send and receive data on the connection, and address is the address bound to the socket on the other end of the connection. At accept(), a new socket is created that is distinct from the named socket. This new socket is used solely for communication with this particular client. For TCP servers, the socket object used to receive connections is not the same socket used to perform subsequent communication with the client. In particular, the accept() system call returns a new socket object that's actually used for the connection. This allows a server to manage connections from a large number of clients simultaneously.
socket.send(bytes[, flags]): Send data to the socket. The socket must be connected to a remote socket. Returns the number of bytes sent. Applications are responsible for checking that all data has been sent; if only some of the data was transmitted, the application needs to attempt delivery of the remaining data.
socket.close(): Mark the socket closed. all future operations on the socket object will fail. The remote end will receive no more data (after queued data is flushed). Sockets are automatically closed when they are garbage-collected, but it is recommended to close() them explicitly.
Note that the server socket doesn't receive any data. It just produces client sockets. Each clientsocket is created in response to some other client socket doing a connect() to the host and port we're bound to. As soon as we've created that clientsocket, we go back to listening for more connections.
1 # client.py 2 import socket 3 4 # create a socket object 5 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 6 7 # get local machine name 8 host = socket.gethostname() 9 10 port = 9999 11 12 # connection to hostname on the port. 13 s.connect((host, port)) 14 15 # Receive no more than 1024 bytes 16 tm = s.recv(1024) 17 18 s.close() 19 20 print("The time got from the server is %s" % tm.decode('ascii'))
The output from the run should look like this:
1 $ python server.py & 2 Got a connection from ('127.0.0.1', 54597) 3 4 $ python client.py 5 The time got from the server is Wed Jan 29 19:14:15 2014