Small scripts to simplify network communication
Pull request Compare This branch is 3 commits ahead, 35 commits behind hellman:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Small scripts to simplify network communication.

TCP client:

Something like telnetlib, but for clean TCP (no command sequences, \r\n newlines, etc.)

TCP/UDP binders

Simple threaded tcp/udp servers - kind of simplified SocketServer


TCP Client

from sock import *

f = Sock("", 3123, timeout=10)
# or IPv6
f = Sock6("::1", 3123, 3)
# or already existing socket
f = toSock(some_socket)

# wait for prompt (skip banner for example)
# the prompt itself will be skipped too
f.read_until("> ", timeout=3)  # read_until_re also exists

f.send("flip coin\n")

# wait for regexp
# 'wait' means that the match won't be skipped
f.wait_for_re(r"You've got (heads|tails)")    # wait_for also exists

# read specific number of bytes
result = f.read_nbytes(11+5)[11:16]

f.send("random please\n")

# read one packet and flush buffers
print f.read_one()

# non-blocking read (flush buffers)
print f.read_one(0)

# read until disconnect
print f.read_all()

TCP binder

from server import *

def handler(client, addr):
    client.send("Hello, I'm server!\n")

tcp(3123, handler).listen()
# or IPv6
tcp6(3123, handler).listen()

UDP binder

from server import *

def handler(sock, data, addr):
    sock.sendto("Hello, I'm server!\n" + data, addr)

udp(3123, handler).listen()
# or IPv6
udp6(3123, handler).listen()


Author: hellman ( )

License: GNU General Public License v2 (