Sockets are the most widely used form of Inter
Process Communication (IPC).
Sockets are used for cross-platform
Sockets were invented in Berkeley as part of
the BSD flavor of UNIX.
Sockets are now available on all major
Sockets are a basic component of network
A socket can be thought of as an information
channel with a program on both ends.
The module that implements sockets is called
There are two types of sockets: client sockets
and server sockets.
Clients and Servers
A client is a program that requests information
from a server.
A server is a program that provides information
Client programs use client sockets.
Server programs use server sockets.
A server socket object must be bound to a host
(www.usu.edu) and a port (an integer, e.g.
1234) on that host.
The socket object specifies how many
connecting requests can be queued up before
the connecting requests are rejected.
After that the server program goes into an
infinite loop that works as follows:
Do something with that connection
A client socket object connects to a host and a
The client socket object must then send and/or
receive information from the socket object.
Blocking (synchronous) servers process one
client connection at a time.
Non-blocking (asynchronous) servers process
multiple clients at a time.
Transmission Control Protocol
TCP stands for Transmission Control Protocol.
TCP is one of the two original core protocols of
the Internet Protocol Suite.
TCP/IP is a frequent abbreviation used in the
It is connection oriented.
User Datagram Protocol
UDP stands for User Datagram Protocol.
UDP is another member of the original Internet
Under UDP, processes can send messages
(called datagrams) to each other without prior
special communications that set up channels or
It is connectioless.
Minimal Server and Minimal Client
Write a blocking server that sets a server socket on the local
computer on port 1234 and goes into an infinite loop that
accepts client connections, gets the address of each
connecting client, and sends the string 'Thank you for
connecting' to each client.
Write a client that connects to a given host on a given port and
prints 1024 bytes of information that it receives from the server.
Source code: minimal_server.py and minimal_client.py
The SocketServer module is used for more
advanced server programming.
SocketServer contains for classes:
TCPServer, UDPServer, UnixStreamServer,
TCPServer is used to program TCP socket
UDPServer for UDP datagram sockets.
The SocketServer framework provides
Each time a server gets a request (a
connection from a client), a request
handler object is constructed and its methods
are called to handle the request.
The BasicRequestHandler class places all
of its action in the handle method.
TCPServer and StreamRequestHandler
The TCPServer class allows you to create
To handle requests on a TCPServer, you can
use the StreamRequestHandler class.
StreamRequestHandler objects have two
attributes: self.rfile (stream for reading)
and self.wfile (stream for writing).
These file-like objects are used to communicate
Minimal TCP Server
Write a blocking TCP server that sets a server socket on
the local computer on port 1234 and goes into an infinite
loop that processes client connections with custom
A custom stream request handler inherits from
StreamRequestHandler and overrides the handle
method to print the address of the connection client and
send 'Thank you for connecting' to the client.
Handling Multiple Connections
There are three ways of handling multiple connections:
forking, threading, and asynchronous I/O.
Forking is a UNIX term that loosely means duplication.
When a parent process (running program) forks a child
process, both processes start running in parallel with
their own separate memories.
In a forking server, a child process is forked for every
connection while the parent process (server) keeps
listening for new connections.
Handling Multiple Connections
Forking is resource-intensive, because each forked
process requires its own memory.
Threading is an alternative to forking.
A thread is a lightweight process that exists within the
same parent process and shares the same memory.
The upside is efficiency.
The downside is the necessity of thread
synchronization, because threads share the same
Handling Multiple Connections
Some operating systems (Windows) do not support
If your system supports forking and you do not want to
bother with synchronization issues, forking is a
Minimal Forking Server
Minimal Threading Server
Python has two modules for processing URLs: urllib
The urllib module is more basic and more
The urllib2 module is more advanced (and less
straightforward): handles HTTP authentication, cookies,
protocol extensions, etc.
Opening Remote and Local Files
The urllib object has the function urlopen that allows
you to open remote and local files.
The function returns a file-like object that supports the
close, read, readline, and readlines methods
Once you have a file-like object to a remote webpage,
you can use regular expressions to extract specific
information from the text of that webpage.
Getting HTML Source
The module urllib enables you to retrieve the html
source of a URL.
The retrieved html source can be parsed for links or
indexed in a local database.
html_source.py prints the html source of a URL.and
saves it in a local file.
PIL is available at www.pythonware.org
There are PIL versions for various Python releases
2.7, 2.6, etc.
We will use PIL 1.1.7 for Python 2.7
PIL on 64bit Win
There may still be issues with 64bit versions of PIL on Windows
Here is a forum that suggest links to 64bit versions of PIL as well
as other possible workarounds on 64bit version of Python
The simplest way to handle 64bit issues is to:
Uninstall any previously installed PIL version
Install Python 32bit Version for Windows (Python 2.7)
Install PIL 32 bit Version (for Python 2.7)
PIL on Mac
In order to install PIL on a Mac it needs to be built from the
Source is at http://www.pythonware.com/products/pil/
Inside tar.gz there is README that describes which
commands need to be ran to install PIL
There are dependencies for the build as well
These dependencies are also described in README
Checking PIL Availability
Run this program interactively in the interpreter or
or as a program:
im = Image.new(“RGB”, (32, 32))
If this works, PIL is installed
Ch. 14 in Beginning Python by Hetland.