Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: byte_stuffing_demo.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- - This script demonstrates byte stuffing and destuffing techniques in Python.
- - Byte stuffing involves the strategic insertion of special characters into a data stream.
- - This ensures reliable transmission, while byte destuffing removes these inserted characters to recover the original data.
- - The script showcases how these techniques can be implemented to enhance data integrity in network communication protocols.
- Stuffing Characters Used:
- 左: (Left)
- 中: (Center)
- 右: (Right)
- Note:
- - These Chinese characters are strategically inserted into the data stream to ensure synchronization and data integrity during transmission.
- - They serve as markers within the data stream and do not necessarily correspond to left, center, or right boundaries.
- Requirements:
- - Python 3.x
- - colorama module (for colored output)
- Functions:
- - byte_stuffing(data, stuffing_chars):
- Performs byte stuffing on the input data using specified stuffing characters.
- - byte_destuffing(data, stuffing_chars):
- Performs byte destuffing on the input data using specified stuffing characters.
- - main():
- Handles user input, performs byte stuffing and destuffing, and displays the results.
- Usage:
- - Run the script in a Python 3 environment.
- - Follow the prompts to enter the string for byte stuffing and destuffing.
- - View the results of byte stuffing and destuffing.
- Example Output:
- :: BYTE STUFFING AND DESTUFFING DEMONSTRATION ::
- __________________________________________________
- Enter String: This is an example string
- __________________________________________________
- The length of the entered data is: 25
- Stuffed data:
- This左 is中 an右 example左 string
- Destuffed data:
- This is an example string
- __________________________________________________
- Additional Notes:
- - The colorama module is used to display colored output, requiring installation via pip.
- - The script assumes familiarity with byte stuffing and destuffing concepts in networking.
- """
- from colorama import Fore, Style
- def byte_stuffing(data, stuffing_chars=["左", "中", "右"]):
- """
- Perform byte stuffing on the input data.
- Args:
- data (str): The input string to be stuffed.
- stuffing_chars (list, optional): List of stuffing characters. Default is ['左', '中', '右'].
- Returns:
- str: The stuffed string.
- """
- stuffed_data = "" # Initialize stuffed data
- char_index = 0
- for char in data:
- if char == " ":
- stuffed_data += Fore.RED + stuffing_chars[char_index] + Style.RESET_ALL
- char_index = (char_index + 1) % len(stuffing_chars)
- stuffed_data += char
- return stuffed_data
- def byte_destuffing(data, stuffing_chars=["左", "中", "右"]):
- """
- Perform byte destuffing on the input data.
- Args:
- data (str): The stuffed string to be destuffed.
- stuffing_chars (list, optional): List of stuffing characters. Default is ['左', '中', '右'].
- Returns:
- str: The destuffed string.
- """
- destuffed_data = ""
- for char in data:
- if char in stuffing_chars:
- continue
- destuffed_data += char
- return destuffed_data
- def main():
- """
- Main function to demonstrate byte stuffing and destuffing.
- """
- print(
- f"\n{Fore.YELLOW} :: BYTE STUFFING AND DESTUFFING DEMONSTRATION ::{Style.RESET_ALL}"
- )
- print("_" * 50)
- # Input data from the user
- data = input(f"\n{Fore.GREEN} Enter String: {Style.RESET_ALL}")
- print("_" * 50)
- length = len(data)
- print(
- f"\n The length of the entered data is: {Fore.GREEN}{length}{Style.RESET_ALL}"
- )
- # Perform byte stuffing
- stuffed_data = byte_stuffing(data)
- print("\n Stuffed data:\n ", stuffed_data)
- # Perform byte destuffing
- destuffed_data = byte_destuffing(stuffed_data)
- print("\n Destuffed data:\n ", destuffed_data)
- print("_" * 50)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement