GithubHelp home page GithubHelp logo

sock's Introduction

sock

Small scripts to simplify network communication.

TCP client:

Something like telnetlib http://docs.python.org/library/telnetlib.html, but for clean TCP (no command sequences, \r\n newlines, etc.)

TCP/UDP binders

Simple threaded tcp/udp servers - kind of simplified SocketServer http://docs.python.org/library/socketserver.html.

Usage

###TCP Client

from sock import *

f = Sock("some.cool.servi.ce", 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")
	client.send(client.recv(4096))

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()

About

Author: hellman ( [email protected] )

License: GNU General Public License v2 (http://opensource.org/licenses/gpl-2.0.php)

sock's People

Contributors

comawill avatar hellman avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

m4z3n

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.