Advertisement
Python253

byte_stuffing_demo

May 28th, 2024
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.28 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: byte_stuffing_demo.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    - This script demonstrates byte stuffing and destuffing techniques in Python.
  10.    - Byte stuffing involves the strategic insertion of special characters into a data stream.
  11.    - This ensures reliable transmission, while byte destuffing removes these inserted characters to recover the original data.
  12.    - The script showcases how these techniques can be implemented to enhance data integrity in network communication protocols.
  13.  
  14. Stuffing Characters Used:
  15.  
  16.    左: (Left)
  17.    中: (Center)
  18.    右: (Right)
  19.    
  20.    Note:
  21.    - These Chinese characters are strategically inserted into the data stream to ensure synchronization and data integrity during transmission.
  22.    - They serve as markers within the data stream and do not necessarily correspond to left, center, or right boundaries.
  23.  
  24. Requirements:
  25.    - Python 3.x
  26.    - colorama module (for colored output)
  27.  
  28. Functions:
  29.    - byte_stuffing(data, stuffing_chars):
  30.        Performs byte stuffing on the input data using specified stuffing characters.
  31.    - byte_destuffing(data, stuffing_chars):
  32.        Performs byte destuffing on the input data using specified stuffing characters.
  33.    - main():
  34.        Handles user input, performs byte stuffing and destuffing, and displays the results.
  35.  
  36. Usage:
  37.    - Run the script in a Python 3 environment.
  38.    - Follow the prompts to enter the string for byte stuffing and destuffing.
  39.    - View the results of byte stuffing and destuffing.
  40.  
  41. Example Output:
  42.  
  43.     :: BYTE STUFFING AND DESTUFFING DEMONSTRATION ::
  44.    __________________________________________________
  45.  
  46.        Enter String: This is an example string
  47.    __________________________________________________
  48.  
  49.        The length of the entered data is: 25
  50.  
  51.        Stuffed data:
  52.        This左 is中 an右 example左 string
  53.  
  54.        Destuffed data:
  55.        This is an example string
  56.    __________________________________________________
  57.  
  58. Additional Notes:
  59.    - The colorama module is used to display colored output, requiring installation via pip.
  60.    - The script assumes familiarity with byte stuffing and destuffing concepts in networking.
  61. """
  62.  
  63. from colorama import Fore, Style
  64.  
  65. def byte_stuffing(data, stuffing_chars=["左", "中", "右"]):
  66.     """
  67.    Perform byte stuffing on the input data.
  68.  
  69.    Args:
  70.        data (str): The input string to be stuffed.
  71.        stuffing_chars (list, optional): List of stuffing characters. Default is ['左', '中', '右'].
  72.  
  73.    Returns:
  74.        str: The stuffed string.
  75.  
  76.    """
  77.     stuffed_data = ""  # Initialize stuffed data
  78.     char_index = 0
  79.  
  80.     for char in data:
  81.         if char == " ":
  82.             stuffed_data += Fore.RED + stuffing_chars[char_index] + Style.RESET_ALL
  83.             char_index = (char_index + 1) % len(stuffing_chars)
  84.         stuffed_data += char
  85.  
  86.     return stuffed_data
  87.  
  88. def byte_destuffing(data, stuffing_chars=["左", "中", "右"]):
  89.     """
  90.    Perform byte destuffing on the input data.
  91.  
  92.    Args:
  93.        data (str): The stuffed string to be destuffed.
  94.        stuffing_chars (list, optional): List of stuffing characters. Default is ['左', '中', '右'].
  95.  
  96.    Returns:
  97.        str: The destuffed string.
  98.  
  99.    """
  100.     destuffed_data = ""
  101.     for char in data:
  102.         if char in stuffing_chars:
  103.             continue
  104.         destuffed_data += char
  105.  
  106.     return destuffed_data
  107.  
  108. def main():
  109.     """
  110.    Main function to demonstrate byte stuffing and destuffing.
  111.    """
  112.     print(
  113.         f"\n{Fore.YELLOW} :: BYTE STUFFING AND DESTUFFING DEMONSTRATION ::{Style.RESET_ALL}"
  114.     )
  115.     print("_" * 50)
  116.  
  117.     # Input data from the user
  118.     data = input(f"\n{Fore.GREEN}    Enter String: {Style.RESET_ALL}")
  119.     print("_" * 50)
  120.     length = len(data)
  121.     print(
  122.         f"\n    The length of the entered data is: {Fore.GREEN}{length}{Style.RESET_ALL}"
  123.     )
  124.  
  125.     # Perform byte stuffing
  126.     stuffed_data = byte_stuffing(data)
  127.     print("\n    Stuffed data:\n   ", stuffed_data)
  128.  
  129.     # Perform byte destuffing
  130.     destuffed_data = byte_destuffing(stuffed_data)
  131.     print("\n    Destuffed data:\n   ", destuffed_data)
  132.     print("_" * 50)
  133.  
  134. if __name__ == "__main__":
  135.     main()
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement