Introduction to
|
Modified: |
Nearly any language can be used for client/server programming; all that is required are a few simple functions for the communications services.
Python is a simple but powerful language that comes with many of the fundamental tools (TCP for connection-oriented and UDP for datagram services) needed for quickly programming networked applications. It is one of the simpler languages useful for course assignments.
TCP
Because TCP is a connection-oriented protocol, one process acts as a server accepting connections, another process acts as the client requesting the connection. Below are basic client and server scripts; the client connects to the server on port 8888, sends 'Hello world'; the server accepts the connection, receives and prints 'Hello world':
Client
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('localhost', 8888))
s.send('Hello world')
s.close()Server
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 8888))
s.listen(1)
conn, addr = s.accept()
data = conn.recv(1024)
print "Received: ", data
conn.close()
Try
- Copy, paste and save the above files as:
- client.py
- server.py
- Open two Command Prompt windows to location of the files.
- Enter:
- python server.py
- python client.py
- import socket - the TCP/IP socket library containing all the necessary function definitions.
- s=socket.socket(socket.AF_INET, socket.SOCK_STREAM) - Constructs a socket and assigns to variable s.
- s.connect(('localhost', 8888)) - Client requests a connection with host 'localhost' on port 8888.
- s.bind(('', 8888)) - Server binds the script on host '' to port 8888.
- s.listen(1) - Server listens to accept 1 connection at a time.
- conn, addr = s.accept() - Server accepts connection request from a client; conn is the socket used for communication with client and addr the client's address.
- s.send('Hello world') - Send a string through the socket s.
- data = s.recv(1024) - Receive data through socket s, assign to variable data; a maximum of 1024 characters received at a time.
- s.close() - Close the connection on socket s.
Example: Echo client and server
The following server echoes the input typed on the client.
# echoclient.py import socket, sys HOST = sys.argv[1] # The remote host PORT = 8888 # Server port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) while True : message = raw_input('Send:') if not message : break s.send(message) data = s.recv(1024) print 'Received', `data` s.close()# echoserver.py import socket HOST = '' # Symbolic name PORT = 8888 # port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(1) conn, addr = s.accept() print 'Connected by', addr while True: data = conn.recv(1024) if not data: break conn.send(data) conn.close()
Try
- Copy, paste and save the above files as:
- echoclient.py
- echoserver.py
- Open two Command Prompt windows to location of the files.
- Enter:
- python echoserver.py
- python echoclient.py localhost
- A blank line to echoclient terminates.
Example: Echo client and server
The same echoclient and echoserver as functions.
# echoclient.py import socket, sys def client( host, port) : s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host, port)) while True : message = raw_input('Send:') if not message : return s.send(message) data = s.recv(1024) print 'Received', `data` s.close()client(sys.argv[1], 8888)# echoserver.py import socket def server( s ) : conn, addr = s.accept() print 'Connected by', addr while True: data = conn.recv(1024) if not data: break conn.send(data) conn.close()s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(('', 8888)) s.listen(1) server( s )
Exceptions
Exceptions allow a formal means of handling non-standard situations. For example, the following handles division by 0.
print '1'
try:
print '2'
x=9/0
print '3'
print x
except Exception:
print 'division by 0'
print '4'
print '5'
- try - Encloses the statements to be tried. No statements in the try are executed after an exception is raised.
- except - Encloses the statements executed only if an exception (in this case, any Exception) occurs in the statements immediately tried. After except statements are completed, execution continues with statements following the except.
The output is: 1, 2, division by 0, 4, 5