Well done, you've reached the end of the course!
In this section, it's time to translate your Python skills and everything you've learned about RESTful APIs into some real-world projects. Specifically, we'll ask you to create:
You will also have a chance to do the quiz and the final test to see how well you've mastered the material, and check if you're prepared for the certification exam (available soon).
Completing the quiz and the final test concludes the course. Are you ready?
We want you to write a simple CLI (Command Line Interface) tool which can be used in order to diagnose the current status of a particular http server. The tool should accept one or two command line arguments:
We also assume that:
Hints:
Assuming that the tool is placed in a source file name sitechecker.py, here are some real-use cases:
import sys import socket if len(sys.argv) not in [2, 3]: print("Improper number of arguments: at least one is required" + "and not more than two are allowed:") print("- http server's address (required)") print("- port number (defaults to 80 if not specified)") exit(1) addr = sys.argv[1] sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) if len(sys.argv) == 3: try: port = int(sys.argv[2]) if not (1 <= port <= 65535): raise ValueError except ValueError: print("Port number is invalid - exiting.") exit(2) else: port = 80 try: sock.connect((addr, port)) except socket.timeout: print("The server" + addr + "seems to be dead - sorry.") exit(3) except socket.gaierror: print("Server address" + addr + "is invalid or malformed - sorry.") exit(4) request = b"HEAD / HTTP/1.0\r\nHost: " + \ bytes(addr, "utf8") + \ b"\r\nConnection:close\r\n\r\n" sock.send(request) answer = sock.recv(100).decode("utf8") sock.shutdown(socket.SHUT_RDWR) sock.close() print(answer[:answer.find('\r')])
Take a look at these two screenshots. They present two different use cases of the same program:
Your task is to write a code which has exactly the same conversation with the user and:
Of course, some basic data validity checks should be done, too. We're sure you're careful enough to protect your code from reckless users.