Connections#

class pync.NetcatConnection(net, C=None, d=None, i=None, q=None, w=None, **kwargs)[source]#

Wraps a socket object to provide Netcat-like functionality.

Parameters:

q (int, optional) – Quit the readwrite loop after EOF on stdin and delay of secs.

You can use sub-classes of this class as a context manager using the “with” statement:

with NetcatConnection(...) as nc:
    nc.readwrite()

If you choose not to use the “with” statement, please make sure to use the close() method after use:

nc = NetcatConnection(...)
nc.readwrite()
nc.close()
close()[source]#

Override to add any cleanup code.

classmethod connect(dest, port, **kwargs)[source]#

Factory method to connect to a server and return a NetcatConnection instance. This method should be implemented by a sub-class.

Parameters:
  • dest (str) – The destination hostname or IP address to connect to.

  • port (int) – The port number to connect to.

  • kwargs – Any other keyword arguments get passed to __init__.

Returns:

Returns a subclass of pync.NetcatConnection once a connection has been established.

Return type:

pync.NetcatConnection

Example:

with NetcatConnection.connect('localhost', 8000) as conn:
    conn.readwrite()
classmethod listen(dest, port, **kwargs)[source]#

Factory method to listen for a connection and return a NetcatConnection instance. This method should be implemented by a sub-class.

Parameters:
  • dest (str) – The hostname or IP address to bind to.

  • port (int) – The port number to bind to.

  • kwargs – Any other keyword arguments get passed to __init__.

Returns:

Returns a subclass of pync.NetcatConnection once a connection has been established.

Return type:

pync.NetcatConnection

Example:

with NetcatConnection.listen('localhost', 8000) as conn:
    conn.readwrite()
readwrite()[source]#

The main loop to read and write i_o. Read from stdin and send to network. Receive from network and write to stdout. Write verbose/debug/error messages to stderr.

This loop is based on the netcat-openbsd 1.105-7 ubuntu version.

Example:

with NetcatConnection(sock) as nc:
    nc.readwrite()
class pync.NetcatTCPConnection(net, C=None, d=None, i=None, q=None, w=None, **kwargs)[source]#

Bases: NetcatConnection

Wraps a TCP socket to provide Netcat-like functionality.

classmethod connect(dest, port, **kwargs)[source]#

Factory method to connect to a TCP server and return a pync.NetcatTCPConnection object.

Parameters:
  • dest (str) – The destination hostname or IP address to connect to.

  • port (int) – The port number to connect to.

  • kwargs – Any other keyword arguments get passed to __init__.

Return type:

pync.NetcatTCPConnection

Example:

from pync import NetcatTCPConnection
with NetcatTCPConnection.connect('localhost', 8000) as conn:
    conn.readwrite()
classmethod listen(dest, port, **kwargs)[source]#

Factory method to listen for an incoming TCP connection and return a pync.NetcatTCPConnection object.

Parameters:
  • dest (str) – The destination hostname or IP address to bind to.

  • port (int) – The port number to bind to.

  • kwargs – Any other keyword arguments get passed to __init__.

Return type:

pync.NetcatTCPConnection

Example:

from pync import NetcatTCPConnection
with NetcatTCPConnection.listen('localhost', 8000) as conn:
    conn.readwrite()
class pync.NetcatUDPConnection(net, C=None, d=None, i=None, q=None, w=None, **kwargs)[source]#

Bases: NetcatConnection

Wraps a UDP socket object to provide Netcat-like functionality.

classmethod connect(dest, port, **kwargs)[source]#
TODO:

classmethod listen(dest, port, **kwargs)[source]#
TODO: