scapy.compat

Python 2 and 3 link classes.

class scapy.compat.AddressFamily(value)

Bases: enum.IntEnum

An enumeration.

AF_ALG = 38
AF_APPLETALK = 5
AF_ASH = 18
AF_ATMPVC = 8
AF_ATMSVC = 20
AF_AX25 = 3
AF_BRIDGE = 7
AF_CAN = 29
AF_ECONET = 19
AF_INET = 2
AF_INET6 = 10
AF_IPX = 4
AF_IRDA = 23
AF_KEY = 15
AF_LLC = 26
AF_NETBEUI = 13
AF_NETROM = 6
AF_PACKET = 17
AF_PPPOX = 24
AF_RDS = 21
AF_ROSE = 11
AF_ROUTE = 16
AF_SECURITY = 14
AF_SNA = 22
AF_TIPC = 30
AF_UNIX = 1
AF_UNSPEC = 0
AF_VSOCK = 40
AF_WANPIPE = 25
AF_X25 = 9
scapy.compat.Callable

Callable type; Callable[[int], str] is a function of (int) -> str.

The subscription syntax must always be used with exactly two values: the argument list and the return type. The argument list must be a list of types or ellipsis; the return type must be a single type.

There is no syntax to indicate optional or keyword arguments, such function types are rarely used as callback types.

alias of Callable

scapy.compat.DefaultDict

The central part of internal API.

This represents a generic version of type ‘origin’ with type arguments ‘params’. There are two kind of these aliases: user defined and special. The special ones are wrappers around builtin collections and ABCs in collections.abc. These must have ‘name’ always set. If ‘inst’ is False, then the alias can’t be instantiated, this is used by e.g. typing.List and typing.Dict.

alias of DefaultDict

scapy.compat.Dict

The central part of internal API.

This represents a generic version of type ‘origin’ with type arguments ‘params’. There are two kind of these aliases: user defined and special. The special ones are wrappers around builtin collections and ABCs in collections.abc. These must have ‘name’ always set. If ‘inst’ is False, then the alias can’t be instantiated, this is used by e.g. typing.List and typing.Dict.

alias of Dict

class scapy.compat.Generic(*args, **kwds)[source]

Bases: object

Abstract base class for generic types.

A generic type is typically declared by inheriting from this class parameterized with one or more type variables. For example, a generic mapping type might be defined as:

class Mapping(Generic[KT, VT]):
    def __getitem__(self, key: KT) -> VT:
        ...
    # Etc.

This class can then be used as follows:

def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
    try:
        return mapping[key]
    except KeyError:
        return default
class scapy.compat.IO(*args, **kwds)[source]

Bases: typing.Generic

Generic base class for TextIO and BinaryIO.

This is an abstract, generic version of the return of open().

NOTE: This does not distinguish between the different possible classes (text vs. binary, read vs. write vs. read/write, append-only, unbuffered). The TextIO and BinaryIO subclasses below capture the distinctions between text vs. binary, which is pervasive in the interface; however we currently do not offer a way to track the other distinctions in the type system.

abstract close() → None[source]
abstract property closed
abstract fileno() → int[source]
abstract flush() → None[source]
abstract isatty() → bool[source]
abstract property mode
abstract property name
abstract read(n: int = - 1) → AnyStr[source]
abstract readable() → bool[source]
abstract readline(limit: int = - 1) → AnyStr[source]
abstract readlines(hint: int = - 1)List[source]
abstract seek(offset: int, whence: int = 0) → int[source]
abstract seekable() → bool[source]
abstract tell() → int[source]
abstract truncate(size: int = None) → int[source]
abstract writable() → bool[source]
abstract write(s: AnyStr) → int[source]
abstract writelines(lines: List) → None[source]
scapy.compat.Iterator

The central part of internal API.

This represents a generic version of type ‘origin’ with type arguments ‘params’. There are two kind of these aliases: user defined and special. The special ones are wrappers around builtin collections and ABCs in collections.abc. These must have ‘name’ always set. If ‘inst’ is False, then the alias can’t be instantiated, this is used by e.g. typing.List and typing.Dict.

alias of Iterator

scapy.compat.List

The central part of internal API.

This represents a generic version of type ‘origin’ with type arguments ‘params’. There are two kind of these aliases: user defined and special. The special ones are wrappers around builtin collections and ABCs in collections.abc. These must have ‘name’ always set. If ‘inst’ is False, then the alias can’t be instantiated, this is used by e.g. typing.List and typing.Dict.

alias of List

scapy.compat.Pattern

The central part of internal API.

This represents a generic version of type ‘origin’ with type arguments ‘params’. There are two kind of these aliases: user defined and special. The special ones are wrappers around builtin collections and ABCs in collections.abc. These must have ‘name’ always set. If ‘inst’ is False, then the alias can’t be instantiated, this is used by e.g. typing.List and typing.Dict.

alias of Pattern

scapy.compat.Sequence

The central part of internal API.

This represents a generic version of type ‘origin’ with type arguments ‘params’. There are two kind of these aliases: user defined and special. The special ones are wrappers around builtin collections and ABCs in collections.abc. These must have ‘name’ always set. If ‘inst’ is False, then the alias can’t be instantiated, this is used by e.g. typing.List and typing.Dict.

alias of Sequence

scapy.compat.Set

The central part of internal API.

This represents a generic version of type ‘origin’ with type arguments ‘params’. There are two kind of these aliases: user defined and special. The special ones are wrappers around builtin collections and ABCs in collections.abc. These must have ‘name’ always set. If ‘inst’ is False, then the alias can’t be instantiated, this is used by e.g. typing.List and typing.Dict.

alias of Set

scapy.compat.Sized

The central part of internal API.

This represents a generic version of type ‘origin’ with type arguments ‘params’. There are two kind of these aliases: user defined and special. The special ones are wrappers around builtin collections and ABCs in collections.abc. These must have ‘name’ always set. If ‘inst’ is False, then the alias can’t be instantiated, this is used by e.g. typing.List and typing.Dict.

alias of Sized

scapy.compat.Tuple

Tuple type; Tuple[X, Y] is the cross-product type of X and Y.

Example: Tuple[T1, T2] is a tuple of two elements corresponding to type variables T1 and T2. Tuple[int, float, str] is a tuple of an int, a float and a string.

To specify a variable-length tuple of homogeneous type, use Tuple[T, …].

alias of Tuple

scapy.compat.Type

A special construct usable to annotate class objects.

For example, suppose we have the following classes:

class User: ...  # Abstract base for User classes
class BasicUser(User): ...
class ProUser(User): ...
class TeamUser(User): ...

And a function that takes a class argument that’s a subclass of User and returns an instance of the corresponding class:

U = TypeVar('U', bound=User)
def new_user(user_class: Type[U]) -> U:
    user = user_class()
    # (Here we could write the user object to a database)
    return user

joe = new_user(BasicUser)

At this point the type checker knows that joe has type BasicUser.

alias of Type

class scapy.compat.TypeVar(name, *constraints, bound=None, covariant=False, contravariant=False)[source]

Bases: typing._Final, typing._Immutable

Type variable.

Usage:

T = TypeVar('T')  # Can be anything
A = TypeVar('A', str, bytes)  # Must be str or bytes

Type variables exist primarily for the benefit of static type checkers. They serve as the parameters for generic types as well as for generic function definitions. See class Generic for more information on generic types. Generic functions work as follows:

def repeat(x: T, n: int) -> List[T]:

‘’’Return a list containing n references to x.’’’ return [x]*n

def longest(x: A, y: A) -> A:

‘’’Return the longest of two strings.’’’ return x if len(x) >= len(y) else y

The latter example’s signature is essentially the overloading of (str, str) -> str and (bytes, bytes) -> bytes. Also note that if the arguments are instances of some subclass of str, the return type is still plain str.

At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.

Type variables defined with covariant=True or contravariant=True can be used to declare covariant or contravariant generic types. See PEP 484 for more details. By default generic types are invariant in all type variables.

Type variables can be introspected. e.g.:

T.__name__ == ‘T’ T.__constraints__ == () T.__covariant__ == False T.__contravariant__ = False A.__constraints__ == (str, bytes)

Note that only type variables defined in global scope can be pickled.

scapy.compat.base64_bytes(x)[source]

Turn base64 into bytes

scapy.compat.bytes_base64(x)[source]

Turn bytes into base64

scapy.compat.bytes_encode(x)[source]

Ensure that the given object is bytes. If the parameter is a packet, raw() should be preferred.

scapy.compat.bytes_hex(x)[source]

Hexify a str or a bytes object

scapy.compat.cast(typ, val)[source]

Cast a value to a type.

This returns the value unchanged. To the type checker this signals that the return value has the designated type, but at runtime we intentionally don’t check anything (we want this to be as fast as possible).

scapy.compat.chb(x)[source]

Same than chr() but encode as bytes.

scapy.compat.gzip_compress(data, compresslevel=9)

Compress data in one shot and return the compressed string. Optional argument is the compression level, in range of 0-9.

scapy.compat.gzip_decompress(data)

Decompress a gzip compressed string in one shot. Return the decompressed string.

scapy.compat.hex_bytes(x)[source]

De-hexify a str or a byte object

scapy.compat.lambda_tuple_converter(func)[source]
Converts a Python 2 function as

lambda (x,y): x + y

In the Python 3 format:

lambda x,y : x + y

scapy.compat.orb(x)[source]

Return ord(x) when not already an int.

scapy.compat.plain_str(x)[source]

Convert basic byte objects to str

scapy.compat.raw(x)[source]

Builds a packet and returns its bytes representation. This function is and will always be cross-version compatible