Basic network operation and maintenance questions and answers

Preface

        This article is an answer to some common questions about basic network operation and maintenance skills. I hope it can provide some help to students or professionals who are doing final review or are interested in network operation and maintenance.

Questions and Answers

1. List 3 commonly used character encodings and briefly describe how to encode and decode between str and bytes .

        Answer: Commonly used character encodings include ASCII encoding, UTF-8 encoding and UTF-16 encoding.

        Three commonly used character encodings are:

        ① ASCII

        ASCII is one of the earliest character encodings. It uses 7 bits to represent characters and contains a total of 128 characters, including English letters, numbers, punctuation marks and other commonly used characters. In ASCII encoding, each character has a corresponding numerical representation.

        In Python, you can use the str.encode() method to convert a string to an ASCII-encoded byte sequence, or the bytes.decode() method to convert an ASCII-encoded byte sequence to a string. By default, Python uses UTF-8 encoding for conversion.

        Sample code:

  1. string = "Hello"  
  2. bytes_sequence = string.encode('ascii')  
  3. print(bytes_sequence)  # b'Hello'  
  4.   
  5. decoded_string = bytes_sequence.decode('ascii')  
  6. print(decoded_string)  # Hello  

        ② UTF-8

        UTF-8 is a variable-length encoding that can represent all characters in the Unicode character set. It uses 1 to 4 bytes to represent different characters, for ASCII characters, it uses one byte, and for other characters, it uses multiple bytes. UTF-8 is currently the most commonly used character encoding on the Internet.

        In Python, you can also use the str.encode() method to convert a string to a UTF-8 encoded byte sequence, or use the bytes.decode() method to convert a UTF-8 encoded byte sequence to a string.

        Sample code:

  1. string =  " Hello"  
  2. bytes_sequence = string.encode('utf-8')  
  3. print(bytes_sequence)  # b'\xe4\xbd\xa0\xe5\xa5\xbd'  
  4.   
  5. decoded_string = bytes_sequence.decode('utf-8')  
  6. print(decoded_string)  #Hello  

        ③ UTF-16

        UTF-16 is a fixed-length character encoding that uses 2 bytes to represent a character and supports various characters around the world.

        In Python, you can use the encode method of str to convert a string to UTF-16 bytes, and the decode method of bytes to convert bytes to a string. For example:

# 编码

text = "你好"

encoded_bytes = text.encode("utf-16")  # 编码为UTF-16字节

print(encoded_bytes)  # b'\xff\xfe`\x4f'

# 解码

decoded_text = encoded_bytes.decode("utf-16")  # 解码为UTF-16字符串

print(decoded_text)  # 你好

Summary: In Python, you can use different encoding schemes to encode a string into a sequence of bytes, or to decode a sequence of bytes into a string. Common encoding schemes include ASCII, UTF-8, and UTF-16. Encoding and decoding operations can use the str.encode() and bytes.decode() methods, you can specify the encoding scheme, or you can use the default encoding scheme (usually UTF-8)

2. Briefly describe the process of file operation, and briefly describe 3 commonly used file operation methods.

answer:

        The general process of file operations can be divided into three steps: opening the file, operating the file, and closing the file. Specific steps are as follows:

        ①  Open a file: Use Python's built-in `open()` function to open a file with a specified path, and assign it a variable name for subsequent operations. You can specify parameters such as the opening method of the file (read-only, write, append, etc.) and encoding format.

        ②  Operate files: For files that have been opened, you can perform various operations such as reading, writing, searching, and modifying. You can use the `read()` method or `readline()` method to read a file, you can use the `write()` method to write a file, and some advanced methods of file operations are required for searching or modifying.

        ③Close  the file: After the operation on the file, you need to use the `close()` method to close the file to release related system resources.

The following are three commonly used file operation methods:

        ① `read()` method: used to read the specified number of characters from the file. For example, the following code reads the first 5 characters from a file:

  1. with open('example.txt''r') as f:  
  2.     data = f.read(5)  
  3.     print(data) #Output  the first 5 characters in the file  

        ② `write()` method: used to write content to the file. For example, the following code writes a line of text to a file:

  1. with open('example.txt''a') as f:  
  2.     f.write( ' This is a line of text\n' )  

        Note that if the file does not exist, it will be created automatically. In the above example, the `a` parameter is used to open the file, which means to append to the file.

        ③ `seek()` method: used to move the file pointer to the specified position. For example, the following code moves the file pointer to the 10th character position in the file:

  1. with open('example.txt''r') as f:  
  2.     f.seek(9)  
  3.     data = f.read(5)  
  4.     print(data) #Output  the 10th character to the 14th character in the file  

3. What does the mode parameter of the open function mean? Briefly describe the four common values ​​​​of the mode parameter.

        The `mode` parameter of the `open()` function indicates the mode of opening the file, that is, operations such as reading, writing, and appending. The `mode` parameter is optional, and the default value is `r`, which means read-only mode. The following are common values ​​for the `mode` parameter:

        1. `r`: Read-only mode . After opening the file, it can only be read, but not written. The file pointer points to the head of the file. If the file does not exist, a `FileNotFoundError` exception will be thrown.

        2. `w`: Write mode , after opening the file, it can only be written, but not read, and the contents of the file will be cleared. If the file does not exist, it will be created automatically.

        3. `a`: Append mode , read and append operations can be performed after opening the file, and the file pointer points to the end of the file. If the file does not exist, it will be created automatically.

        4. `b`: Binary mode , read or write in binary mode after opening the file. For example, you can use `rb` mode to read binary files (such as pictures, audio, etc.), and use `wb` mode to write binary files.

        In addition to these four commonly used modes, you can also use other modes, such as `x` (exclusive mode, if the file already exists, a `FileExistsError` exception will be thrown), `+` (readable and writable mode), `t` ( text mode, default), etc. It should be noted that the `mode` parameter can combine multiple modes, for example `wb+` indicates the readable and writable file mode in binary mode.

4. Explain the format of the configuration file, which class of which module is mainly used to parse the configuration file? Briefly describe the three common methods of this class.

        Configuration files are files used to store application or system configuration parameters, including key-value pairs, and common file formats include INI, JSON, YAML, etc.

        INI is a behavior-based configuration file format that contains some configuration options, sections, and key-value pairs. The following is an example of a configuration file in INI format:

  1. [logging]  
  2. level = info  
  3. path = /var/log/test.log  
  4.   
  5. [database]  
  6. host = 127.0.0.1  
  7. port = 3306  
  8. username = test  
  9. password = test123  

        The commonly used module for parsing INI format configuration files in Python is `configparser`, which provides the `ConfigParser` class. Common methods include:

1. `read(filename)`: Read the configuration file.

  1. import configparser  
  2.   
  3. #Create  ConfigParser object  
  4. config = configparser.ConfigParser()  
  5. #Read  configuration file  
  6. config.read('example.ini')  

2. `get(section, option)`: Get the value of the specified configuration item.

  1. #Get  the value of the level option under the logging section  
  2. level = config.get('logging''level')  
  3. print(level) #Output  ' info'  

3. `set(section, option, value)`: Modify the value of the specified configuration item.

  1. #Modify  the value of the port option under the database section  
  2. config.set('database''port''3307')  

        It should be noted that the `ConfigParser` class can support multiple sections and multiple key-value pairs, and different configuration information can be accessed by specifying section names and option names. At the same time, the `ConfigParser` class also provides methods such as `getfloat()`, `getint()`, `getboolean()`, which are convenient for obtaining different types of values.

5. Briefly describe the four commonly used functions for parsing json in the json  module .

        In Python's `json` module, there are some commonly used functions for parsing JSON data. Here is a brief description of these commonly used functions:

        1. `json.loads()`: This function is used to parse JSON strings into Python objects. It takes a JSON string as input and returns a corresponding Python data structure (usually a dictionary, list, etc.). For example, a JSON string can be parsed into a Python object using `json.loads(json_str)`.

        2. `json.load()`: This function is used to read JSON data from a file and parse it into a Python object. It takes a file object as input and parses the JSON data in the file into the corresponding Python data structure. For example, JSON data can be read from a file and parsed into a Python object using `json.load(file_object)`.

        3. `json.dumps()`: This function is used to convert Python objects into JSON strings. It accepts a Python object as input and returns a corresponding JSON string. For example, you can use `json.dumps(python_obj)` to convert a Python object into a JSON string.

        4. `json.dump()`: This function is used to convert Python objects into JSON format and write them to a file. It accepts a Python object and a file object as input, converts the Python object into a JSON string and writes it to the file. For example, you can use `json.dump(python_obj, file_object)` to convert a Python object to JSON and write it to a file.

        These functions provide convenient ways to parse and process JSON data. JSON data can be parsed into Python objects using `json.loads()` and `json.load()`, while `json.dumps()` and `json.dump()` can be used to convert Python objects into JSON format. Through these functions, you can easily process JSON data in Python and realize data parsing, reading and writing.

6. Briefly describe how xmltodict processes xml data.

        xmltodict is a Python library that can convert XML data into the form of a Python dictionary, making XML data more convenient to process.

        Specifically, xmltodict can convert XML data into a combination of nested Python dictionaries and lists. In the dictionary, the tags and attributes of the XML element are converted into the keys and values ​​of the dictionary, and the text content of the XML element becomes the dictionary's value. If child elements exist in the XML, they will be converted into a new dictionary and assigned the keys of their parent element.

7. How to install the psutil module, and briefly describe the information that can be monitored by using the psutil module.

        Install the psutil module:

The following information of the system can be monitored using the psutil module:

        1. CPU usage

        2. Memory usage

        3. Disk partitions and disk usage

        4. System process information, such as process ID, process startup time, process status, process memory usage, etc.

        5. Network connection information, such as local IP address, remote IP address, connection status, etc.

        6. System user information, such as user name, user ID, user group, etc.

8. Explain the working principle of using watchdog to monitor changes in specified directories / files .

        watchdog is a Python library used to monitor file changes on the file system, so that it can listen for changes in specified directories or files and issue notifications. It uses the file monitoring mechanism of the operating system, commonly used ones include inotify (under Linux systems), FSEvents (under Apple macOS systems) and ReadDirectoryChangesW (under Windows systems).

        The watchdog library works as follows:

        1. Create monitoring objects

        watchdog creates monitoring objects through the watchdog.observers.Observer object, which is used to monitor changes in specified directories or files. When creating an Observer object, we need to specify the callback function and register the directory or file to be monitored.

        2. Start monitoring

        Call `observer.start()` to start monitoring directory or file changes. Once started, the Observer object will run uninterrupted until we stop it.

        3. Handle events

        When changes are detected in the directory or file, the callback function overloaded in the MyHandler class will be called.

        4. End monitoring

        If we want to stop monitoring, we can call observer.stop() to stop and use observer.join() to wait for the task to complete.

        Note: watchdog can only monitor any events after it has been started and relies on the file system monitoring mechanism. Therefore, if a monitored directory or file is created or modified while the program is running, the monitoring will need to be re-created or restarted.

9. Among the parameters of the subprocess.Popen class constructor, what are the default values stdin , stdout , and stderr ? What does it mean? How to pass the output of the child process to the parent process for processing?

        Among the parameters of the subprocess.Popen class constructor, the default values ​​of stdin, stdout, and stderr are all `None`, which means that the standard input, standard output, and standard error streams of the child process will inherit the standard I/O streams of the parent process.

        If we want to pass the child process output to the parent process for processing, we can use the `communicate()` method of the Popen object. This method will wait for the child process to complete and return a tuple containing the stdout output and stderr output of the child process.

        In this example, we use `Popen()` to open a UNIX command named "ls" and capture and store its stdout and stderr output into the variables output and error, using `communicate()` to wait for the command to complete.

        It should be noted that when using `Popen()`, we can specify the stdout and stderr parameters to capture the output of the child process. If they are set to `PIPE`, output can be piped to the current process via the child process's standard output and standard error streams.

10. Briefly describe the role of logs. When recording events through logs, what aspects of the events usually need to be recorded?

        Logging is an important tool for recording events that occur while a system or application is running. By logging events, we can understand what is happening to our application while it is running and quickly identify and resolve potential issues.

Logs can typically be used for the following purposes:

1. Troubleshooting: When an application fails, you can use logs to find the source of the problem and troubleshoot.

2. Performance analysis: Analyzing logs can find application bottlenecks and performance problems, and then optimize them.

3. Security monitoring: Logs can record system security events, such as login attempts, attacks, etc.

4. Business analysis: Logs can record user behavior, business activities, etc. of the system, helping business analysts understand the usage of the system and user needs.

When logging an event, you typically need to log the following aspects:

1. Timestamp: Record the time when an event occurs for subsequent analysis and tracking.

2. Event level: Record the severity of the event. Common event levels include DEBUG, INFO, WARNING, ERROR, CRITICAL, etc.

3. Event content: Record a detailed description of the event, including the type, ID, location, source, etc. of the event.

4. Context information: Record the context information when the event occurs, including user information, operation information, request information, response information, etc.

5. Others: According to actual needs, other additional information can be recorded, such as machine IP address, thread ID, etc.

        Properly recording logs can help strengthen system monitoring, operation and maintenance, and debugging, and help comprehensively understand the operating status and problems of applications.

11. In the logging module, what are the log levels? How are they sorted?

        In the logging module, the log levels are:

1. CRITICAL (50) indicates a fatal error that will prevent the program from continuing to execute.

2. ERROR (40) indicates an error event, but the application can continue to run.

3. WARNING (30) indicates a warning event that may affect the normal operation of the application.

4. INFO (20) indicates logging of important events, which can be used to track application state and behavior.

5. DEBUG (10) indicates detailed debugging records for diagnosing problems.

6. NOTSET (0) is the lowest level, indicating that the log level is not set.

        Log levels are sorted numerically, that is:

CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET

        When setting the log level, records lower than the set level will be ignored. For example, when setting the level to `ERROR`, only records with a log level of `ERROR` or `CRITICAL` will be logged, while records below the level of `ERROR` will be ignored.

        A common logging application is to set all logging to `WARNING` or `ERROR`, and only consider lowering the logging level when a problem occurs.

12. In the logging module, what are loggers, processors and formatters, and what is the relationship between them? Master the relevant setting statements.

In the logging module, there are three main concepts: logger, processor and formatter.

1.Logger _

The logger is the interface that sends log events to the logging system and provides access to the logging system configuration and status. In the logging module, use the `getLogger()` function to create a logger.

```

import logging

logger = logging.getLogger('mylogger')

```

2. Processor

The processor determines where the log records are sent. For example, writing a record to a file, sending an email, or logging it to another service, the processor plays this role. In the logging module, handlers can be added through the `addHandler()` method.

```

import logging

logger = logging.getLogger('mylogger')

#Create processor

handler = logging.FileHandler('mylog.txt')

#Add processor

logger.addHandler(handler)

```

3.Formatter _

The formatter determines the style in which log events are logged. In the logging module, use the `setFormatter()` method in the processor to add a formatter.

```

import logging

logger = logging.getLogger('mylogger')

#Create processor

handler = logging.FileHandler('mylog.txt')

#Create formatter

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

#Add formatter

handler.setFormatter(formatter)

#Add processor

logger.addHandler(handler)

```

The relationship between these three components is as follows:

        A logger can have multiple handlers, each handler can have a formatter, and each formatter can have a format string. Call the `logging.getLogger('mylogger')` function in the logger to create a logger. A handler can be added to a logger via the `logger.addHandler(handler)` method and a formatter can be added to a processor via the `handler.setFormatter(formatter)` method.

        When a logger logs an event, it passes the event to the handlers it adds. The handler formats the event and passes it to the configured output destination, for example, writing to a file or sending an email. When the application needs to record logs, it only needs to pass the log information to the logger.

        In addition to the above basic settings, you can also use different settings in different environments through configuration files or dictionaries.

13. What settings can be made to quickly build an FTP server using one-line commands of the pyftpdlib module? Please list three features of programming using pyftpdlibAPI that are richer than one-line commands.

Using the pyftpdlib module, you can quickly set up an FTP server with the following one-line command:

```

python -m pyftpdlib

```

This will start a default FTP server with a listening IP address of 0.0.0.0 and port number 21.

The FTP server can be configured with the following settings:

1. IP address and port number: You can use the -h and -p options to specify the IP address and port number that the FTP server listens to, for example:

```

python -m pyftpdlib -p 2121 -i 192.168.0.100

```

2. Username and password: You can use the -a option to specify the username and password, for example:

```

python -m pyftpdlib -a user:password

```

3. Directory setting: You can use the -w option to set the working directory of the FTP user, for example:

```

python -m pyftpdlib -w /home/user

```

In addition to the above basic settings, richer features can also be used in pyftpdlib API programming, such as:

1. Event processing of file upload and download: File upload and download events can be processed through custom event handlers, such as implementing file upload/download progress bars.

2. Authority and user management: User management and authority control can be customized through the FTPHandler class, such as restricting users to access specific directories or files.

3. Custom commands and responses: FTP commands and command responses can be customized through the FTPHandler class. For example, customize the FTP server's welcome message or respond to specific commands.

14. Briefly describe the steps of programming to implement email sending, and specify which classes or methods are used.

The basic steps for programming to send emails are as follows:

1. Import relevant modules. In Python, we can use the smtplib module to operate the SMTP protocol to send emails. Use `from smtplib import SMTP` to import the SMTP classes.

2. Create an SMTP object. Create an SMTP object using the constructor of the SMTP class.

```

smtp = SMTP('smtp.example.com', port=25)

```

Specify the hostname and port number of the SMTP server using the two parameters of the constructor.

3. Connect to the SMTP server. Use the `connect()` method of the SMTP object to connect to the SMTP server.

```

smtp.connect('smtp.example.com', port=25)

```

4. Log in to the SMTP server. Log in to the SMTP server using the `login()` method of the SMTP object.

```

smtp.login('username', 'password')

```

You need to provide your login username and password.

5. Create a mail object. Use the email module to create email message objects. You can use MIMEText, MIMEImage, MIMEMultipart and other classes. For example, use the MIMEText class to create a text-only message.

```

from email.mime.text import MIMEText

msg = MIMEText('This is a test email')

msg['Subject'] = 'Test Email'

msg['From'] = '[email protected]'

msg['To'] = '[email protected]'

```

In this example, we create a text email, specifying the subject, sender, and recipients of the email.

6. Send an email. Send mail using the `sendmail()` method of the SMTP object.

```

smtp.sendmail('[email protected]', '[email protected]', msg.as_string())

```

The sender, recipient and email content are required.

7. Exit the SMTP server. Use the `quit()` method of the SMTP object to exit the SMTP server.

```python

smtp.quit()

```

The above are the basic steps for sending emails. The classes and methods involved in common smtplib modules are:

1. SMTP class: The constructor is used to create SMTP objects, and the methods include `connect()`, `login()`, `sendmail()` and `quit()`.

2. Email module: used to create email objects, including MIMEText, MIMEImage, MIMEMultipart and other classes.

3. MIMEText class: used to create email messages containing text.

4. sendmail() method: used to send emails. The sender, recipient and email content are required.

By adding information such as headers and body to the message object, you can create complex messages containing attachments of any type. In addition, you can also use SSL or TLS encrypted connections, custom SMTP commands, exception handling and other technologies to implement more complex email sending logic.

Guess you like

Origin blog.csdn.net/as12138/article/details/132006311