6 Exercises
6 Exercises
Objectives:
Sockets are programming abstraction for network code. These are the endpoints for a communication
channel between two devices. To create a socket in python:
import socket s = socket.socket(addr_family, type)
The HTTP protocol used by the web is based on TCP. Experiment with the protocol by manually
connecting to a web server, issuing a request, and reading the response.
Exercise 1.2
Objectives:
In this exercise, we experiment with setting up a network connection between two different Python
interpreters.
To do this exercise, you need to start a different Python interpreter than the one you are using with
IDLE. On Windows, go to the "Start" menu and run the "Python (command line)" program. For
example:
This should pop up a command window where you can directly interact with a Python interpreter.
2
On Linux and Macintosh, open a new terminal window (e.g., xterm) and run Python at the command
line by typing 'python'.
In the new Python interpreter that you just opened, type the following commands to create a very
simple network server:
These statements create a server that is now waiting for an incoming connection. The accept()
operation blocks forever until a connection arrives---so, you won't be able to do anything else until that
happens.
Using IDLE, type the following statements to make a connection to your server program. Keep in
mind, these statements are being typed into a different Python interpreter than the one that's running in
part (a).
The variable c returned by accept() is a socket object that's connected to the remote client. The
variable a is a tuple containing the IP address and port number of the remote client.
In IDLE (the client), type the following statement to send a "Hello World" message to the server:
Observe that you are sending data back and forth between the two Python interpreters. Try more
send() and recv() operations if you want to send more data.
In IDLE (the client), issue a recv() operation to wait for more data. This should block since the server
hasn't actually sent any additional data yet.
>>> s.recv(1024)
4
When the send() operation completes, the client should display the 'Goodbye' message. For example:
>>> s.recv(1024)
''
>>>
You'll notice that the recv() operation returns with an empty string. This is the indication that the
server has closed the connection and that there is no more data. There is nothing more than the client
can do with the socket at this point.
After the server has closed the client connection, it typically goes back to listening by issuing a new
accept() command. Type this in the server:
Once again, the server now waits until a new connection arrives.
Try connecting to your server program using your internet browser. Simply click on this link (or type it
into the navigation bar):
http://localhost:15000/index.html
In your server program, you should see that the accept() operation has returned. Try reading the
browser request and print it out.
5
Exercise 1.3