Introduction to
Networking in Python

Modified

Overview

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.

 

Network programming

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
  1. Copy, paste and save the above files as:
    • client.py
    • server.py
  2. Open two Command Prompt windows to location of the files.
  3. Enter:
    • python server.py
    • python client.py

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
  1. Copy, paste and save the above files as:
    • echoclient.py
    • echoserver.py
  2. Open two Command Prompt windows to location of the files.
  3. Enter:
    • python echoserver.py
    • python echoclient.py localhost
  4. 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'

The output is: 1, 2, division by 0, 4, 5