# Path: conpot/utils/networking.py
# def str_to_bytes(x):
#     return x if isinstance(x, bytes) else str(x).encode("ascii")


from struct import pack, unpack
from optparse import OptionGroup
from conpot.utils.networking import str_to_bytes
import struct
import socket
import string
__FILTER = "".join(
    [" "]
    + [
        " " if chr(x) not in string.printable or chr(x) in string.whitespace else chr(x)
        for x in range(1, 256)
    ]
)


_bytes_to_str = lambda items: (
    value.decode("ascii") if isinstance(value, bytes) else value for value in items
)


def StripUnprintable(msg):
    return msg.decode("ascii").translate(__FILTER)


class TPKTPacket:
    """TPKT packet. RFC 1006"""

    def __init__(self, data=""):
        self.data = data

    def pack(self):
        return pack(
            "!BBH",
            3,  # version
            0,  # reserved
            len(bytes(self.data)) + 4,  # packet size

# Path: conpot/utils/networking.py
# def str_to_bytes(x):
#     return x if isinstance(x, bytes) else str(x).encode("ascii")


# Path: conpot/tests/helpers/s7comm_client.py
from struct import pack, unpack
from optparse import OptionGroup
from conpot.utils.networking import str_to_bytes
import struct
import socket
import string

__FILTER = "".join(
    [" "]
    + [
        " " if chr(x) not in string.printable or chr(x) in string.whitespace else chr(x)
        for x in range(1, 256)
    ]
)


_bytes_to_str = lambda items: (
    value.decode("ascii") if isinstance(value, bytes) else value for value in items
)


def StripUnprintable(msg):
    return msg.decode("ascii").translate(__FILTER)


class TPKTPacket:
    """TPKT packet. RFC 1006"""

    def __init__(self, data=""):
        self.data = data

    def pack(self):
        return pack(
            "!BBH",
            3,  # version
            0,  # reserved
            len(bytes(self.data)) + 4,  # packet size

        ) + str_to_bytes(bytes(self.data))