pysap.SAPNI module

class pysap.SAPNI.SAPNI(_pkt='', post_transform=None, _internal=0, _underlayer=None, **fields)[source]

Bases: scapy.packet.Packet

SAP NI (Network Interface) packet

This packet is used for craft Network Interface packets. It serves only as a container for packets in the different protocols. As this protocol is used by different protocols and the only way to differentiate each one is by the TCP port used, each script using the NI protocol must bind the respective layer with the respective protocol.

For example, a script using the SAP Diag protocol must include the following binds:

bind_layers(SAPNI,      SAPDiag, )
bind_layers(SAPNI,      SAPDiagDP, )
bind_layers(SAPDiagDP,  SAPDiag, )
bind_layers(SAPDiag,    SAPDiagItem, )
bind_layers(SAPDiagItem,SAPDiagItem, )
Cvar:Constant for keep-alive request messages (NI_PING)
Cvar:Constant for keep-alive response messages (NI_PONG)
aliastypes = [<class 'pysap.SAPNI.SAPNI'>, <class 'scapy.packet.Packet'>]
fields_desc = [<Field (SAPNI).length>]
class pysap.SAPNI.SAPNIClient[source]

Bases: object

Stub class for a client connecting to the SAP NI server.

class pysap.SAPNI.SAPNIProxy(bind_address, bind_port, remote_address, remote_port, handler, backlog=5, keep_alive=True, options=None)[source]

Bases: object

SAP NI Proxy

It works by setting a listener SAPNIStreamSocket and dispatching client’s requests to a given handler class.

Example usage::
proxy = SAPNIProxy(local_host, local_port, remote_host, remote_port, handler_class) proxy.handle_connection()

Block until a connection is received from the listener and handle that client using the provided handler class.

Returns:the handler instance handling the request
Return type:SAPNIProxyHandler

Stop the proxy by closing the listener socket.

class pysap.SAPNI.SAPNIProxyHandler(client, server, options=None)[source]

Bases: object

SAP NI Proxy Handler

Handles NI packets. Works spawning one thread for processing data coming from each pair of client/server.


This method is called each time a packet arrives from the client. It must return a packet in the same layer (SAPNI). Stub method to be overloaded in subclasses.

Parameters:packet (Packet) – the packet to be processed

This method is called each time a packet arrives from the server. It must return a packet in the same layer (SAPNI). Stub method to be overloaded in subclasses.

Parameters:packet (Packet) – the packet to be processed
recv_send(local, remote, process)[source]

Receives data from one socket connection, process it and send to the remote connection.


Stop the processor workers

class pysap.SAPNI.SAPNIServer(server_address, RequestHandlerClass, bind_and_activate=True, socket_cls=None, keep_alive=True, base_cls=None)[source]

Bases: SocketServer.TCPServer

Base SAP NI Server class.

Subclasses must define a client class for keeping state information on the connected clients.

Example usage::
server = SAPNIServer((local_host, local_port), handler_class) server.client_cls = client_class server.serve_forever()
Cvar:Client class for storing data about new clients
Type:SAPNIClient class

alias of SAPNIClient


Wrap the socket object with a SAPNIStreamSocket after accepting a connection.

handle_error(request, client_address)[source]

Called to handle an error or exception occurred with the server.

options = None
Ivar:Options to pass to the request handler

Called to shutdown and close an individual request.

class pysap.SAPNI.SAPNIServerHandler(request, client_address, server)[source]

Bases: SocketServer.BaseRequestHandler

SAP NI Server Handler

Handles SAPNI packets coming from a SAPNIServer.


Close a client connection and deletes the client from the state information on the server.


Handle a client connection. The handler assumes the client connection is a SAPNIStreamSocket object. After received a SAPNI packet, it stores it on the ‘packet’ instance variable and pass the control to the handle_data method.


Handle the data coming from the client. The SAPNI packet is stored on data and client information on client_address instance variables. Stub method to be overloaded in subclasses.


Setup a new client connection. Creates a new client object for keeping state information of each client on the server instance.

class pysap.SAPNI.SAPNIServerThreaded(server_address, RequestHandlerClass, bind_and_activate=True, socket_cls=None, keep_alive=True, base_cls=None)[source]

Bases: SocketServer.ThreadingMixIn, pysap.SAPNI.SAPNIServer

A SAP NI Server implementation using threading

class pysap.SAPNI.SAPNIStreamSocket(sock, keep_alive=True, base_cls=None)[source]

Bases: scapy.supersocket.StreamSocket

Stream socket implementation of the SAP Network Interface (NI) layer.

desc = 'NI Stream socket'
classmethod get_nisocket(host, port, **kwargs)[source]

Helper function to obtain a SAPNIStreamSocket.

  • host (C{string}) – host to connect to
  • port (int) – port to connect to
  • kwargs – arguments to pass to SAPNIStreamSocket constructor

connected socket

Return type:



socket.error – if the connection to the target host/port failed


Receive a packet at the NI layer, first reading the length field and the reading the data. If the stream is waiting for a new packet and the remote peer sends a keep-alive request (NI_PING), the receive method will respond with a keep-alive response (NI_PONG) to keep the communication stable.

Returns:received SAPNI packet
Return type:SAPNI
Raises:socket.error – if the connection was close

Send a packet at the NI layer, prepending the length field.

Parameters:packet (Packet) – packet to send

Send a given packet and receive the response. Wrapper around the send and receive methods. The response packet is build in the SAPNI layer.

Parameters:packet (Packet) – packet to send
Returns:packet received
Return type:Packet