Low-level socket abstraction layer

Different types of stream-like classes (sockets, files, pipes, …) have very different interfaces and behaviors under exceptional conditions. The goal of this “simple socket” module is to provide a unified interface for a variety of stream types. The Netcat class then uses this interface to provide all the convenient functionality you love.

All Netcat methods should automatically wrap any stream objects you provide with the appropriate wrapper.

class nclib.simplesock.Simple[source]

The base class for implementing a simple, unified interface for socket-like objects. Instances of this type should act like a simple unbuffered blocking socket.

  • can_send – Whether this stream supports send operations

  • can_recv – Whether this stream supports recv operations


Close the stream.

property closed

Whether the stream has been closed.


The file descriptor associated with the stream. Should raise NetcatError if there is not a single file descriptor to return.


Receive at most size bytes, blocking until some data is available. Returns an empty bytestring as an EOF condition.


Send as much of the given data as possible, returning the number of bytes sent.


Indicate somehow that there will be no more reading/writing/both to this stream. Takes the socket.SHUT_* constants.


A helper function to automatically pick the correct wrapper class for a sock-like object.

class nclib.simplesock.SimpleSocket(sock)[source]

A wrapper for sockets.


sock – A python socket.socket object.

class nclib.simplesock.SimpleFile(fp)[source]

A wrapper for files from the python io module, including sys.stdin, subprocess pipes, etc. If the file has an encoding, it will be discarded.


fp – An io.IOBase object

class nclib.simplesock.SimpleDuplex(child_recv=None, child_send=None)[source]

A wrapper which splits recv and send operations across two different streams.

  • child_recv (Simple) – The stream to use for recving

  • child_send (Simple) – The stream to use for sending

If either of these parameters are None, that operation will be disabled and generate exceptions.

class nclib.simplesock.SimpleMerge(children)[source]

A stream that merges the output of many readable streams into one.


children (list of Simple) – A list of streams from which to read

class nclib.simplesock.SimpleNetcat(nc)[source]

A wrapper for a Netcat object! Why? Just in case you want to do Netcat-level instrumentation [logging] at a finer granularity than the top-level.


sock – A Netcat object.

class nclib.simplesock.SimpleLogger(logger='nclib.logs', level='INFO', encoding=None)[source]

A socket-like interface for dumping data to a python logging endpoint.

  • logger – The dotted name for the endpoint for the logs to go to

  • level – The string or numeric severity level for the logging messages

  • encoding – simplesock objects are fed bytestrings. Loggers consume unicode strings. How should we translate?