Pycopia net package

Networking related modules.

Generic server builder for creating networked client-server protocols that are text based. Provides both a thread based and subprocess based handler model. Intended to simplify simple, low-volume client-server applications.

class pycopia.clientserver.ForkingModel(pwent=None)[source]

Fork new handlers as submethod. This is the default process model.

class pycopia.clientserver.ThreadProcessModel[source]

This process model uses threads. TODO This is not complete.

class pycopia.clientserver.SyncronousModel[source]

For simple, synchronous applications requiring only one handler at a time.

class pycopia.clientserver.StreamWorker(sock, addr, protocol)[source]
EOL = '\n'
run(stream)[source]
class pycopia.clientserver.DatagramWorker(sock, addr, protocol)[source]
run(stream)[source]
class pycopia.clientserver.TCPWorker(sock, addr, protocol)[source]
class pycopia.clientserver.UDPWorker(sock, addr, protocol)[source]
class pycopia.clientserver.UnixStreamWorker(sock, addr, protocol)[source]
class pycopia.clientserver.UnixDatagramWorker(sock, addr, protocol)[source]
class pycopia.clientserver.Server[source]

Base class for all servers.

PORT = None
close()[source]
fileno()[source]
run()[source]
class pycopia.clientserver.StreamServer[source]
accept()[source]
class pycopia.clientserver.DatagramServer[source]
accept()[source]
class pycopia.clientserver.TCPServer(workerclass, protocol, port=None, host=None, processmodel=None, debug=False)[source]
PORT = None
class pycopia.clientserver.UDPServer(workerclass, protocol, port=None, host=None, debug=False)[source]
PORT = None
class pycopia.clientserver.UnixStreamServer(workerclass, protocol, path=None, processmodel=None, debug=False)[source]
PATH = '/tmp/_UnixStream'
class pycopia.clientserver.UnixDatagramServer(workerclass, protocol, path=None, debug=False)[source]
PATH = '/tmp/_UnixDatagram'
class pycopia.clientserver.TCPClient(host, protocol, port=None, logfile=None)[source]

A client side of a TCP protocol.

PORT = 9999
class pycopia.clientserver.UnixStreamClient(protocol, path=None, logfile=None)[source]

A client side of a UNIX socket protocol.

PATH = '/tmp/_UnixStream'
class pycopia.clientserver.UDPClient(host, protocol, port=None, logfile=None)[source]

A client side of a UDP protocol.

PORT = 9999
class pycopia.clientserver.UnixDatagramClient(protocol, path=None, logfile=None)[source]

A client side of a UNIX datagram protocol.

PATH = '/tmp/_UnixDatagram'
pycopia.clientserver.get_client(name, dest, protocol, port=None, logfile=None)[source]

Factory function for getting a proper client object. Provide the name of the client class, proper destination address, and protocol object.

pycopia.clientserver.get_server(name, protocol, host=None, port=None, path=None, debug=False)[source]

General factory for server worker. Give the pathname of a worker class object. Returns the appropriate type of server for it.

Asynchronous server core that supports protocol objects.

class pycopia.asyncserver.AsyncServerHandler(sock, workerclass, protocol)[source]

Generic asynchronous server handler. Register an instance of this, with a worker class and a protocol, with the poller.

close()[source]
closed
fileno()[source]
priority()[source]
read_handler()[source]
readable()[source]
writable()[source]
class pycopia.asyncserver.AsyncWorkerHandler(sock, addr, proto)[source]
close()[source]
closed
error_handler()[source]
exception_handler(ex, val, tb)[source]
fileno()[source]
hangup_handler()[source]
initialize()[source]
pri_handler()[source]
priority()[source]
read_handler()[source]
readable()[source]
writable()[source]
write(data)[source]
write_handler()[source]
pycopia.asyncserver.log(*args)[source]

Print to stderr

Asynchronous HTTP server.

exception pycopia.ping.Error[source]
class pycopia.ping.Pinger(cmdline, logfile=None, env=None, callback=None, merge=1, pwent=None, async=False, devnull=None, _pgid=0)[source]

This class is an interface to, and opens a pipe to, the pyntping program. This program actually does a bit more than ping, but pinging is the most common use. The pyntping program is a C program that should be installed in your path, owned by root, with SUID bit set. This is necessary because only root can open RAW sockets for ICMP operations.

Methods are listed below. Most methods take a single host, or multiple hosts when called. If a single host is given, a single value will be returned. If more than one is given, a list of result will be returned.

The following attributes may also be adjusted:
retries timeout delay size hops
echo(*hosts)[source]
mask(*hosts)[source]
ping(*hosts)[source]
reachable(*hosts)[source]

return a boolean value if a host is reachable. If list given, return a list of (host, reachable) tuples.

reachablelist(*hosts)[source]

Return a sublist of only those hosts that are reachable.

timestamp(*hosts)[source]
trace(*hosts)[source]
ttl(*hosts)[source]
class pycopia.ping.PoweroffDetector(target, retries=30, timeout=5, delay=10, size=64, hops=30)[source]

Detect a power off of a remote device using “ping”.

The following algorithm is used. 1. Verify the target is pingable. 2. Call a callback method that may initiate a power off. 2. Loop until target is not pingable.

Call the go method when you are ready to go. You may supply an optional callback that will be called after the target is determined to be reachable. This may initiate the actual reboot.

May raise RebootDetectorError at any phase.

REACHABLE = 1
UNKNOWN = 0
go(callback=None)[source]
class pycopia.ping.RebootDetector(target, retries=30, timeout=5, delay=10, size=64, hops=30)[source]

Detect a reboot of a remote device using “ping”.

The following algorithm is used:

  1. Verify the target is pingable.
  2. Loop until target is not pingable.
  3. While target is not pingable, loop until it is pingable again.

The target may have recently initiated a reboot before this is called, and still be pingable. Timing is important in this case.

Call the go method when you are ready to go. You may supply an optional callback that will be called after the target is determined to be reachable. This may initiate the actual reboot.

May raise RebootDetectorError at any phase.

NOTREACHABLE = 2
REACHABLE = 1
REACHABLE2 = 3
UNKNOWN = 0
go(callback=None)[source]

Start the reboot detection.

If a callback is provided it is called after device is gone through its unreachable phase and is now reachable again.

Returns a boolean value indication success.

May raise RebootDetectorError if something is not right with the reboot process.

verify_reboot()[source]

Simple verify function not requiring exception handling.

Like the go method, but doesn’t raise an excepton, Instead, it only returns a boolean value indication reboot operation success.

exception pycopia.ping.RebootDetectorError[source]

Raised when the RebootDetector cannot verify a reboot.

pycopia.ping.get_pinger(retries=3, timeout=5, delay=0, size=64, hops=30, logfile=None)[source]

Returns a Pinger process that you can call various ICMP methods on.

pycopia.ping.ping(host, retries=3, timeout=5, delay=1, size=64, hops=30)[source]

Perform a typical “ping” to a destinatino host. Prints time statistics until interrupted.

pycopia.ping.reachable(target)[source]
pycopia.ping.reachable_hosts(hostlist)[source]

Given a list of hosts, return a list of booleans indicating if the corresponding host is reachable.

pycopia.ping.scan_net(network)[source]

where <network> is an IPv4 object or list with host and broadcast elements at ends.

pycopia.ping.traceroute(hostip, maxhops=30)[source]

return a list of (ipaddr, time) tuples tracing a path to the given hostip.

slogsink is a module (that is used in conjuctions with the slogsink C program) that receives syslog messages over a network. This is not a full syslog implementation, but a simple syslog protocol receiver that listens on the standard syslog/udp port for incoming messages and forwards them to your callback function.

The slogsink C program is used because the syslog port is a privileged port, and requires root access to open. The slogsink program should have been install SUID to root.

class pycopia.slogsink.SlogDispatcher(callback, addr=('', 514))[source]
error_handler(ex, val, tb)[source]
read()[source]
readable()[source]
writable()[source]
class pycopia.slogsink.Syslog(files=None)[source]

A syslog program object.

addlog(fp)[source]
close()[source]
dispatch(msg)[source]
flush()[source]
logfiles
message(tag, msg, facility=5, priority=6)[source]
openlog(fname)[source]
openlogs(flist)[source]
write(msg)[source]
class pycopia.slogsink.SyslogApp(syslog, ps1='%Isyslog%N> ')[source]
mainloop(debug=False)[source]
class pycopia.slogsink.SyslogCLI(ui, aliases=None)[source]
ctime(argv)[source]

ctime <timeval> Expand <timeval> (a float) to a readable form.

flush(argv)[source]

Flush all of the log files.

message(argv)[source]

message <text> Place a manual entry in the log file.

class pycopia.slogsink.SyslogMessage(msg, fac, pri, host=None, timestamp=None, tag='')[source]
encode()[source]
class pycopia.slogsink.UserSlogDispatcher(callback, addr=('', 10514))[source]
error_handler(ex, val, tb)[source]
read()[source]
readable()[source]
writable()[source]
pycopia.slogsink.default_logger(message)[source]
pycopia.slogsink.get_dispatcher(addr, callback=<function default_logger at 0x9f07b90>)[source]
pycopia.slogsink.nmslog(argv)[source]
Usage:
nmslog [-p port] [-d] [<logfile> ...]

Record external syslog events. An optional list of file names may be supplied on the command line. The received event will be written to each file given, including stdout. The user may also type in a message at anytime which will also be added to the log files.

The -d flag enables debugging mode. The -p flag specifies a non-standard UDP port to listen to.

pycopia.slogsink.parse_message(timestamp, srcip, rawmsg)[source]
pycopia.slogsink.slog(argv)[source]
pycopia.slogsink.start_slogsink(port=514)[source]

Protocol modules

Modules that use the protocol engine to implement low-level protocols. Usually used for testing servers and clients of these protocols for functionality and vulnerabilites.

Clients

HTTP Test protocols, server side.

Clients used to test various SMTP related exploits.

Servers

Collection of HTTP test protocols, server side.

class pycopia.clientservers.servers.http_protocols.BasicHTTPServerProto(eol=None)[source]

A simple HTTP protocol implementation, server side.

Measurement package

Modules that provide network parameter measurements.

Averaging counters. Computes a running average and an exponentially weighted running average.

class pycopia.measure.Counters.RunningAverage(N=5)[source]

Maintains the current average.

Call the update() method with a new value periodically.

Properties:
ExponentialWeightedRunningAverage RunningAverage

These may be referenced at any time to get the current value.

EWRA
ExponentialWeightedRunningAverage
RA
RunningAverage
append(val)

Call this to update the average and weighted average.

update(val)[source]

Call this to update the average and weighted average.

Reporting

Report on pcap files.

pycopia.pcap.report.pcap_report(argv)[source]

pcapinfo [-h?] [-m] [-c] <pcap file>...

Report information about pcap files. Where:

-h (?)      Print this help.
-m          Report the set of unique MAC addresses contained in the files.
-c          Print Cisco style MAC addesses. Otherwise, hex strings.
-C <fname>  Write Cisco config file for static mac entries.
-v <id>     Supply VLAN ID for destination MAC when writing Cisco config.
-i <intf>   Supply Cisco destination interface when writing Cisco config.

SSL

SSL certificate management, including code to manage a certificate authority.

Certificate Authority functionality.

Implemented as a wrapper for openSSL and openssl binary tool. Basically a port to Python of the CA.pl script. Therefore, it’s pretty ugly, and limited in what your certs can contain.

It tries to automate the CA and other cert operations by using a custom openssl configuration file and environment variables. But it’s still limited and tedious.

The pyOpenSSL based modules are better. Use those if you can.

class pycopia.ssl.CA.CAManager(conf=None)[source]
new_ca()[source]
pkcs12(certname='newcert', keyname='newkey', comment='My Certificate')[source]
sign(out='newcert.pem', infile='newreq.pem')[source]
sign_ca(out='newcert.pem', infile='newreq.pem')[source]
sign_cert(out='newcert.pem', infile='newreq.pem')[source]
verify(cert='newcert.pem')[source]
xsign(infiles)[source]
exception pycopia.ssl.CA.Error[source]
exception pycopia.ssl.CA.SSLConfigError[source]
exception pycopia.ssl.CA.SSLExecError[source]
pycopia.ssl.CA.get_manager(conf=None)[source]
pycopia.ssl.CA.run_openssl(config, app, *args, **kwargs)[source]
pycopia.ssl.CA.write_env_file()[source]

Table Of Contents

Previous topic

Pycopia storage package

Next topic

Pycopia audio package