Diferències
Ací es mostren les diferències entre la revisió seleccionada i la versió actual de la pàgina.
Següent revisió | Revisió prèvia | ||
info:cursos:pue:python-pcpp1:m4:2.1 [19/01/2024 03:24] – creat mate | info:cursos:pue:python-pcpp1:m4:2.1 [19/01/2024 09:59] (actual) – [solution] mate | ||
---|---|---|---|
Línia 15: | Línia 15: | ||
== Lab 1 | == Lab 1 | ||
+ | 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: | ||
+ | * (obligatory) the address (IP or qualified domain name) of the server to be diagnosed (the diagnosis will be extremely simple, we just want to know if the server is dead or alive) | ||
+ | * (optional) the server' | ||
+ | * use the HEAD method instead of GET — it forces the server to send the full response header but without any content; it's enough to check if the server is working properly; the rest of the request remains the same as for GET. | ||
+ | We also assume that: | ||
+ | * the tool checks if it is invoked properly, and when the invocation lacks any arguments, the tool prints an error message and returns an exit code equal to 1; | ||
+ | * if there are two arguments in the invocation line and the second one is not an integer number in the range 1..65535, the tool prints an error message and returns an exit code equal to 2; | ||
+ | * if the tool experiences a timeout during connection, an error message is printed and 3 is returned as the exit code; | ||
+ | * if the connection fails due to any other reason, an error message appears and 4 is returned as the exit code; | ||
+ | * if the connection succeeds, the very first line of the server’s response is printed. | ||
+ | Hints: | ||
+ | * to access command line arguments, use the argv variable from the sys module; its length is always one more than the actual number of arguments, as argv[0] stores your script' | ||
+ | * returning an exit code equal to n can be achieved by invoking the exit(n) function. | ||
+ | Assuming that the tool is placed in a source file name sitechecker.py, | ||
+ | {{ : | ||
+ | === solution | ||
+ | <code python> | ||
+ | import sys | ||
+ | import socket | ||
+ | |||
+ | if len(sys.argv) not in [2, 3]: | ||
+ | print(" | ||
+ | "and not more than two are allowed:" | ||
+ | print(" | ||
+ | print(" | ||
+ | exit(1) | ||
+ | |||
+ | addr = sys.argv[1] | ||
+ | sock = socket.socket(socket.AF_INET, | ||
+ | if len(sys.argv) == 3: | ||
+ | try: | ||
+ | port = int(sys.argv[2]) | ||
+ | if not (1 <= port <= 65535): | ||
+ | raise ValueError | ||
+ | except ValueError: | ||
+ | print(" | ||
+ | exit(2) | ||
+ | else: | ||
+ | port = 80 | ||
+ | |||
+ | try: | ||
+ | sock.connect((addr, | ||
+ | except socket.timeout: | ||
+ | print(" | ||
+ | exit(3) | ||
+ | except socket.gaierror: | ||
+ | print(" | ||
+ | exit(4) | ||
+ | |||
+ | request = b"HEAD / HTTP/ | ||
+ | bytes(addr, " | ||
+ | b" | ||
+ | |||
+ | sock.send(request) | ||
+ | answer = sock.recv(100).decode(" | ||
+ | sock.shutdown(socket.SHUT_RDWR) | ||
+ | sock.close() | ||
+ | print(answer[: | ||
+ | </ | ||
+ | |||
+ | == Lab 2 | ||
+ | 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: | ||
+ | |||
+ | - defines a class named **Vehicle**, | ||
+ | - defines a class able to encode the Vehicle object into an equivalent JSON string; | ||
+ | - defines a class able to decode the JSON string into the newly created Vehicle object. | ||
+ | |||
+ | 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. |