src.myserver.cli

A package for learning network programming in Python.

This module (file) manages the command line interface to control the server.

 1######################################################################
 2# Copyright (c) Adrien Luxey-Bitri, Boris Baldassari
 3#
 4# This program and the accompanying materials are made
 5# available under the terms of the Eclipse Public License 2.0
 6# which is available at https://www.eclipse.org/legal/epl-2.0/
 7#
 8# SPDX-License-Identifier: EPL-2.0
 9######################################################################
10
11"""
12A package for learning network programming in Python.
13
14This module (file) manages the command line interface to control the server.
15"""
16
17import argparse
18import sys 
19from myserver.server import serve
20
21def parse_args(argv: list[str]) -> argparse.Namespace:
22    """
23    Parses arguments from command line.
24    
25    Parameters
26    ----------
27    - argv: list[str]
28        The list of arguments to parse.
29
30    Returns
31    -------
32    argparse.Namespace
33        The list of parameters and their values.
34
35    """
36    parser = argparse.ArgumentParser(
37        prog="myserver", 
38        description="My HTTP web server")
39    parser.add_argument('-p', '--port',  
40                        help='TCP port number to listen to',
41                        default=8080, type=int,
42                        required=True)
43    parser.add_argument('-r', '--root',  
44                        help='Root directory of the server',
45                        type=str, required=True)
46    args = parser.parse_args(argv)
47
48    return args
49
50def main():
51    args = parse_args(sys.argv[1:])
52    serve(int(args.port), args.root)
def parse_args(argv: list[str]) -> argparse.Namespace:
22def parse_args(argv: list[str]) -> argparse.Namespace:
23    """
24    Parses arguments from command line.
25    
26    Parameters
27    ----------
28    - argv: list[str]
29        The list of arguments to parse.
30
31    Returns
32    -------
33    argparse.Namespace
34        The list of parameters and their values.
35
36    """
37    parser = argparse.ArgumentParser(
38        prog="myserver", 
39        description="My HTTP web server")
40    parser.add_argument('-p', '--port',  
41                        help='TCP port number to listen to',
42                        default=8080, type=int,
43                        required=True)
44    parser.add_argument('-r', '--root',  
45                        help='Root directory of the server',
46                        type=str, required=True)
47    args = parser.parse_args(argv)
48
49    return args

Parses arguments from command line.

Parameters

  • argv: list[str] The list of arguments to parse.

Returns

argparse.Namespace The list of parameters and their values.

def main():
51def main():
52    args = parse_args(sys.argv[1:])
53    serve(int(args.port), args.root)