Lab1 Handout
Lab1 Handout
Introduction
In this lab, we will practice Python socket programming. We will first try out the example
client and server programs in the lecture, and then implement a simple file transfer
application.
To use sockets in your Python programs, you must include the Socket Module in your programs,
which contains Low-level networking interface (close to the BSD API):
or
import socket
The following is a list of often used Python socket APIs for your reference (see more at
https://docs.python.org/3/library/socket.html ):
s = socket.socket (socket_family,
create a socket:
socket_type, protocol)
• socket_family : AF_INET (IPV4) or AF_INET6
(IPV6) (commonly used two; see others at
https://docs.python.org/3/library/socket.html )
• socket_type: SOCK_STREAM (TCP),
SOCK_DGRAM (UDP) (commonly used two;
more at
https://docs.python.org/3/library/socket.html )
• protocol: This is defaulting to 0
Example 1 (TCPSocket-1): This is the TCP server (sequential) and client example in the lecture
slides.
• Launch one terminal and switch to the directory of TCPSocket-1. Run the server
program as follows:
python3 TCPServer.py
You will see the prompt in the terminal “The server is ready to receive”, which means the
server is listening.
• Launch another terminal and switch to the directory of TCPSocket-1. Run the client
program as follows:
python3 TCPClient.py
You will see the prompt “Input a lowercase sentence:”. You can enter such a sentence and
press “enter”. Then you will see an uppercase sentence returned from the server.
Example 2 (TCPSocket-2): This is equivalent implementation of the TCP server (sequential) and
client example in the lecture slides, where instead of using “from socket import *” to import
APIs in the socket module, we use “import socket”. Compare the difference between the
programs in Example 1 and Example 2.
Run the example following the same steps as given in Example 1. (You can use control+C to kill
a running server.)
Example 3 (TCPSocket-3): This is another implementation of the TCP server (sequential) and
client example in the lecture slides, where we add a number of exception handling codes. Read
more about Python error and exception handling at
https://docs.python.org/3/tutorial/errors.html, exception socket.error at
https://docs.python.org/3/library/socket.html#socket.error, Python sys module at
https://docs.python.org/3/library/sys.html .
Run the example following the steps given in Example 1. When you see the prompt “Input a
lowercase sentence:”, try using “control+c” instead of entering a sentence; then you will see
error prompt on both terminals running client and server programs.
Example 4 (TCPSocket-4): This is the TCP server (concurrent) and client example in the lecture
slides, where the server can handle concurrent connections through threading.
• Launch one terminal and switch to the directory of TCPSocket-4. Run the server
program as follows:
python3 TCPServer.py
You will see the prompt in the terminal “The server is ready to receive”, which means the
server is listening.
• Launch the second terminal and switch to the directory of TCPSocket-4. Run the
client program as follows:
python3 TCPClient.py
• Launch the third terminal and switch to the directory of TCPSocket-4. Run the client
program as follows:
python3 TCPClient.py
Now two connections are set up with the same server. You can enter sentences on the
second and third terminals and communicate with the server concurrently.
Besides, the server program runs starting from the following code:
if __name__ == '__main__':
server = ServerMain()
server.server_run()
The purpose of the line of code if __name__ == '__main__' is to tell whether the current
module is read directly by the Python interpreter, i.e., whether your program is run as the main
program (read more at https://stackoverflow.com/questions/419163/what-does-if-name-
main-do). ServerMain() is to create an instance of the ServerMain class, and then we run the
method server_run() defined in the ServerMain class.
Example 5 (UDPSocket): This is the UDP server and client example in the lecture slides.
• Launch one terminal and switch to the directory of UDPSocket. Run the server
program as follows:
python3 UDPServer.py
You will see the prompt in the terminal “The server is ready to receive”.
• Launch another terminal and switch to the directory of UDPSocket. Run the client
program as follows:
python3 UDPClient.py
You will see the prompt “Input a lowercase sentence:”. You can enter such a sentence and
press “enter”. Then you will see an uppercase sentence returned from the server.
We now implement a simple client/server application for file transfer, where the server
receives a file that the client sends.
Step 1: Download lab1_materials.zip from Moodle. Unzip it and you will find two files
provided: server/FTServer.py and client/FTClient.py. Copy a file into the client folder which
your client program will send to the server.
Step 2: Open FTClient.py using a text editor, which contains the complete implementation of
the client program. Study the client program carefully and you will learn from its code to
complete the server program.
Check out the following code at the end of the client program first :
if __name__ == '__main__':
if len(sys.argv) != 4:
print("Usage: python3 FTClient.py <Server_addr> <Server_port> <filename>")
sys.exit(1)
main(sys.argv)
The purpose of the line of code if __name__ == '__main__' is to tell whether the current
module is read directly by the Python interpreter, i.e., whether your program is run as the
main program (read more at https://stackoverflow.com/questions/419163/what-does-if-
name-main-do). sys.argv is the list of command-line arguments, i.e., FTClient.py, 127.0.0.1,
12345, and socketprogramming.pdf following python3 in the command you use to run the
client program. sys.argv is passed to the main function as the argument.
b. In the main function, the client program first sends the server the name and size of the file
to be sent to the server; then upon “OK” acknowledgement from the server, it reads the file
contents from the file system and sends them to the server.
• the getsize function in the os.path module checks whether a file exists and gets its file
size (https://docs.python.org/3.8/library/os.path.html).
• The Python built-in function open() is used to open a file for reading. Check out the
meaning of the second argument, mode, at
https://docs.python.org/3/library/functions.html#open.
• In line 21, the Python built-in function int() is used to convert a string to an integer.
• In line 38, b"OK" is a Python bytes literal ; using the prefix b the string "OK" becomes
a bytes object (https://docs.python.org/3/reference/lexical_analysis.html#literals).
Step 3: Open FTServer.py using a text editor and you will find that it provides a sketch of
server program. Implement the server program following the hints given as “#....”, to achieve
the following service: when the server receives the message containing file name and size
from the client, it sends “OK” acknowledgement back to the client; then it creates a file (you
can create the file in the server folder) using the received file name; after that, it receives
contents of the file from the client and writes received contents into the file.
Implement exception handling for socket APIs bind, accept, recv, and file open and write calls.
Read more about using open to create a file and write to write into a file at
https://docs.python.org/3/tutorial/inputoutput.html.
• Launch the second terminal and switch to the directory of Lab1/client. Run the client
program as follows:
python3 FTClient.py 127.0.0.1 12345 filename
Here, filename is the name of the file which you copy into the client folder.
Here is a sample output when running the application on the same machine:
Server program:
Client program:
After successfully sending a file from the client to the server, you should see the new file
created in the server folder.
Submission:
You should submit the following files in the specified folder structure:
(1) server/FTServer.py
(2) client/FTClient.py
Please compress the above files/folders in a lab1-yourstudentid.zip file and submit it on
Moodle before 23:59 S u n d a y O c t 08, 2023: