Warning: You can use only the packages that were used in the lecture
notes.
- Don't use any fancy packages not covered in the course.
In this lab, we will write simple programs using sockets.
[10pts] Part 1: Netcat chats
Finish Activity 1 in Class on sockets. In particular, bring up two terminals. Have
a necat chat between the two terminals. Try a TCP connection, and then a UDP
connection. In this case,
- the server is the localhost. So, you can use IP address 127.0.0.1.
- For the port number, you can use 9000 or a different port number that you choose.
Deliverables:
Two screeenshots in your lab report: one for a TCP chat, and the other
for a UDP chat. Throught the semester, if you add screenshots, please add them
in the lab report (along with explanations).
[25pts] Part 2: UDP netcat server
Finish Activity in Class on sockets. In particular, create a UDP netcat program
(server part) called serv_udp.py. Your program should work with
the following command: nc -u ip_addr 9000
Refer to the socket
manual page.
- You need to give an additional parameter when calling
socket() to create a UDP socket.
- You still need to bind the socket to the service specific port (in our
case, it's 9000).
- However, UDP doesn't need to have the 3-way handshake as in TCP. So, you
don't need to call
listen() or accept().
- For UDP,
sendto() and recvfrom() should be used.
- Use
select() appropriately.
- Have the program quit when the user types in "quit". Be sure to use
strip() function for the input string to get rid of white space.
A sample run:
~$ python3 serv_udp.py
From ('127.0.0.1', 34001) : b'hello\n'
How are you?
quit
|
~$ nc -u 127.0.0.1 9000
hello
How are you?
^C
|
Note
- For incoming packets, specify where it came from using the following
format:
From ('ip', port) :
Deliverables:
- serv_udp.py
- A screenshot in your lab report that shows your code works correctly.
[25pts] Part 3: HTTP Client
Write a TCP client program httpc.py that will successfully
communicate with (i.e. send data to and receive data from) a remote server
on port 80 (http).
Note
-
You can use only the packages that were used in the lecture
notes. Don't use any fancy packages if they are not covered in the course.
-
Your understanding of how the HTTP protocol works is the big part of lab
work.
-
Your program must connect to the remote server and then send a "GET" request
for the main index web page.
- Read this page
to figure out what kind of data your program should send through a socket. In
particular, read the "HTTP Flow" section carefully.
Warning
- Most webservers would reject your HTTP request if it does not contain
a Host header.
- Use b"\r\n" (instead of b"\n") for each new line.
- You should give two new lines at the end of the request.
You may want to see how a web-browser sends a HTTP request (with the Host
header) using Wireshark. (If you want to use Wireshark in your VM, see the
settings page.)
- After the GET request
is sent, your program should read the response from the server and print it
out to the screen.
Sample runs:
~$ python3 httpc.py info.cern.ch
HTTP/1.1 200 OK
Date: Mon, 07 Jun 2021 21:40:35 GMT
Server: Apache
Last-Modified: Wed, 05 Feb 2014 16:00:31 GMT
ETag: "286-4f1aadb3105c0"
Accept-Ranges: bytes
Content-Length: 646
Connection: close
Content-Type: text/html
<html><head></head><body><header>
<title>http://info.cern.ch</title>
</header>
<h1>http://info.cern.ch - home of the first website</h1>
<p>From here you can:</p>
<ul>
<li><a href="http://info.cern.ch/hypertext/WWW/TheProject.html">Browse the first website</a></li>
<li><a href="http://line-mode.cern.ch/www/hypertext/WWW/TheProject.html">Browse the first website using the line-mode browser simulator</a></li>
<li><a href="http://home.web.cern.ch/topics/birth-web">Learn about the birth of the web</a></li>
<li><a href="http://home.web.cern.ch/about">Learn about CERN, the physics laboratory where the web was born</a></li>
</ul>
</body></html>
~$ python3 httpc.py http-textarea.badssl.com
HTTP/1.1 200 OK
Server: nginx/1.10.3 (Ubuntu)
Date: Fri, 20 Jan 2023 16:21:46 GMT
Content-Type: text/html
Content-Length: 713
Last-Modified: Thu, 27 Oct 2022 19:09:20 GMT
Connection: keep-alive
ETag: "635ad760-2c9"
Cache-Control: no-store
Accept-Ranges: bytes
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="/icons/favicon-red.ico"/>
<link rel="apple-touch-icon" href="/icons/icon-red.png"/>
<title>textarea.badssl.com</title>
<link rel="stylesheet" href="/style.css">
<style>body { background: red; }</style>
</head>
<body>
<style>
#content textarea {
font-size: 4vw;
}
</style>
<div id="content">
<h1 style="font-size: 5vw;">
textarea.<br>badssl.com
</h1>
<br><br><br>
<textarea placeholder="Type here." autofocus></textarea>
</div>
<div id="footer">
This page contains a <code><textarea></code> input.
</div>
</body>
</html>
Deliverables:
- httpc.py
- In the lab report, give sample runs (like shown above) of your code for
sites info.cern.ch and http-textarea.badssl.com
[30pts] Part 4: HTTP Server
Write a simple HTTP server program https.py. It should listen at
port 8000, and supports two following web pages:
- index.html (Copy the contents and store them as index.html)
- hobby.html (Copy the contents and store them as hobby.html)
When https.py is running on the localhost, the Chrome web browser should show
something like the following:
index.html
hobby.html
| xxx.html
|
Tips
- You may want to use
split and splitlines functions; see the documentation
for more detail (search for the words "split" and "splitlines").
- You don't need to send optional headers. Only the following items are mandatory:
- HTTP response (The first line).
- Content-Type
- Content-Length
Again, don't use any fancy package; your understanding of how the HTTP protocol works is the big part of
lab work.
- You will probably need to convert strings to bytes (and vice versa).
- You may want to refer to the class notes on how to open a file and read the contents.
- Make sure that Chrome navigates your site freely (e.g., clicking a link
should work well).
- Note that your server should perform the following task repeatedly:
accept(), recv(), send(), close().
Use an infinite loop that does the above task.
Deliverables
- https.py
- Briefly change index.html and hobby.html so that they are associated with you (add your name or something).
- In your lab report, give screenhosts of the Chrome browser that show that your server works correctly.
[10pts] Lab Report and Submission
Write a lab report by using the provided
template.
The writing quality of the lab report matters.
~/bin/submit -c=IT430 -p=lab02 *.py *.html lab02.docx