To practice writing programs which use sockets and work with the client/server network model.
For this lab, you will need to run a server on your Google cloud VM. By default, Google blocks communication on ports other than a select few (including 22 for SSH, and 80 for the web if you select it).
So in order to create a server, we must first open a port in the firewall. A firewall is a system that monitors network traffic coming into or out of a machine. It has a list of rules on what sort of traffic is allowed. If we don't tell the firewall to allow traffic on a port, none will get through.
To do this, follow these steps:
If everything was filled out, you should be back at the list of firewall rules. You should see a row for the rule we just added, like this:
Our firewall rule
Once we have the port open, we can run a server which is listening on this port. For this lab you'll make a "time server". This server will listen on port 4040 for connections. Each time a client connects, it will send out the current date and time to the client, and then wait for the next client.
bindto the internal IP address of your VM.
datetimelibrary with an import statement, and then use the following to get this as a string:
datetime.datetime.now().strftime("%A, %B %d, %Y, %I:%M %p\n")
acceptand the sending of the date and time inside of an infinite loop. This way, the server will accept all of the clients that connect, one after the other, rather than just one. An infinite loop in Python looks like this:
while True: # body of the loop
Because this is such a simple server, we do not really need a whole client to test it. Instead we can use the very handy program called telnet.
telnet is sort of like a generic client, you can connect it to any server and port, and send and receive data back and forth from it. Telnet used to come with all major computer systems.
If you're using Windows, you have telnet, but need to enable it following these instructions. If you're using a Mac, you need to install it following this guide instead. If you are using Linux, you already have telnet.
To test that telnet itself is installed, you connect to the server
which will show you an ASCII-art version of Star Wars:
$ telnet towel.blinkenlights.nl
You can quit telnet with Control-]. Then type "quit" at the telnet prompt.
To test your time and date server, just connect telnet to the external IP of your VM, and the port 4040. It should print the date and time and return:
$ telnet 188.8.131.52 4040 Trying 184.108.40.206... Connected to 220.127.116.11. Escape character is '^]'. The day and time is: Thursday, January 31, 2019, 12:32 AM Connection closed by foreign host.
If you see something like this, then you have done it correctly! Note that your VM may not be in the same time zone as us, so the time may be a few hours off. That's OK.
Make sure that you can connect over and over again and that the server will continue to respond to you.
When you're finished, email your server .py file to email@example.com.
Copyright © 2021 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.