First version

This commit is contained in:
Marcel Nijenhof
2019-07-24 22:33:00 +02:00
commit 88567fd0a6
2 changed files with 99 additions and 0 deletions

36
README.md Normal file
View File

@@ -0,0 +1,36 @@
# bwlcat
## Introduction
A small cat like program with a bandwidth limit.
This program sends every period T N bytes.
This is useful for transfering large files over slow links without causing
a congestion.
## Usage
```
bwlcat [-h] [-b <bytes>] [-t <time>] [<files>]
-h: Help
-b <bytes>: Sent each time N Bytes
-h <time>: Wait T seconds after each packet
```
## Example
### Simple example
```bash
bwlcat -b 8 -t 2 /etc/hosts
```
Sent's /etc/hosts woth a speed of 4 bytes a second.
### Usage to transfer a file over ssh
```bash
ssh remote bwlcat -b 1400 -t 1.5 /bin/bash >/tmp/bash
```
Sent over bash with 1KB/sec.

63
bwlcat Executable file
View File

@@ -0,0 +1,63 @@
#!/usr/bin/python
import getopt
import sys
import time
def usage():
print("bwlcat [-h] [-b <bytes>] [-t <time>] [<file>]")
return(0)
def bwlcat(fd, config):
try:
data = fd.read(config["bytes"])
while data != '':
sys.stdout.write(data)
data = fd.read(config["bytes"])
time.sleep(config["time"])
except Exception as err:
print str(err) # will print something like "option -a not recognized"
return(1)
return(0)
def bwlcatfile(filename, config):
try:
fd = open(filename)
r = bwlcat(fd, config)
fd.close()
except Exception as err:
print str(err) # will print something like "option -a not recognized"
return(1)
return(r)
def main():
config = {"bytes":1024, "time":1}
try:
opts, args = getopt.getopt(sys.argv[1:], "hb:t:")
except getopt.GetoptError as err:
print str(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
for o, a in opts:
if o == "-b":
config["bytes"] = int(a)
elif o == "-t":
config["time"] = float(a)
elif o in ("-h"):
usage()
sys.exit()
else:
assert False, "unhandled option"
if (len(args) == 0):
r = bwlcat(sys.stdin, config)
else:
r = 0
for filename in args:
r1 = bwlcatfile(filename, config)
if (r1 != 0):
r = r1
return(r)
if __name__ == "__main__":
main()