Skip to content

Instantly share code, notes, and snippets.

@nickva
Last active February 16, 2018 21:16
Show Gist options
  • Save nickva/c1d695274685be3778102aac9c481c53 to your computer and use it in GitHub Desktop.
Save nickva/c1d695274685be3778102aac9c481c53 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
import socket
import itertools
import time
import requests
import couchdb
DEBUG = True
MIN_TIME = 0
CHUNK_SIZE = 16
# These are hard-coded don't change them
PORT = 15984
DB = 'cdyno-0000002'
DOCID = 'cdyno-0000001'
USER = 'adm'
PASS = 'pass'
PORT = 15984
MP_REQUEST='''PUT /cdyno-0000002/cdyno-0000001?new_edits=false HTTP/1.1
User-Agent: CouchDB-Replicator/2.2.0-a0252996f
Content-Type: multipart/related; boundary="b1a650274ad3e445cf7a987e1170c693"
Content-Length: 997
Accept: application/json
Cookie: YWRtOjVBODc0NjM2Olinubzl-X1PJO4y0cr-uKloq2kI
Authorization: Basic YWRtOnBhc3M=
Host: localhost:15984
--b1a650274ad3e445cf7a987e1170c693
Content-Type: application/json
{"_id":"cdyno-0000001","_rev":"7-94f7a7bad6139306859cc210aa466700","some_data":"780916c98843434a82221fab82a74360","_revisions":{"start":7,"ids":["94f7a7bad6139306859cc210aa466700","04b03d8ccaa7890d9d249d40d8d9f052","4a12ab6ee6a4d0b6ebdc8aa8c56c3949","3f46d9b0221eae9eb148aa11b34b259c","be578d58f51409218cd6d91af46b91a0","682ccaf01ffa1716ac9de28bbca49159","32cb4a2e217db34a5007f79e8bb107c5"]},"_attachments":{"att1":{"content_type":"","revpos":7,"digest":"md5-iqmbH0Of9xKT6VNXusb9lA==","length":9,"follows":true},"att2":{"content_type":"","revpos":6,"digest":"md5-4xb54XRJBwFTgaNyJ+90qA==","length":11,"follows":true}}}
--b1a650274ad3e445cf7a987e1170c693
Content-Disposition: attachment; filename="att1"
Content-Type:
Content-Length: 9
abcdefghi
--b1a650274ad3e445cf7a987e1170c693
Content-Disposition: attachment; filename="att2"
Content-Type:
Content-Length: 11
qwertypqzwy
--b1a650274ad3e445cf7a987e1170c693--'''
def lg(*args):
if DEBUG:
msg = ' '.join([str(a) for a in args])
sys.stderr.write('> ' + msg + '\n')
def setup_db(dbname, user, password, port):
url = 'http://%s:%[email protected]:%d'% (user, password, port)
lg("db url:", url)
srv=couchdb.Server(url)
if dbname in srv:
lg("Deleting previous database:", dbname)
srv.delete(dbname)
db = srv.create(dbname)
db[DOCID] = {}
lg("Created DB:", dbname)
return db
def get_mp_request(req):
return '\r\n'.join(req.splitlines())
def batch(it, batchsize):
while True:
batch = [x for (_, x) in itertools.izip(xrange(batchsize), it)]
if not batch:
raise StopIteration
yield ''.join(batch)
def send_paced(s, data, min_time, chunk_size):
dlen = len(data)
chunk_count = dlen / chunk_size
dt = min_time / chunk_count
lg("chunk size:", chunk_size, "chunk count:", chunk_count)
i = 0
for chunk in batch(iter(data), chunk_size):
s.sendall(chunk)
lg("sent data chunk:", len(chunk))
time.sleep(dt)
i += 1
def main(min_time, chunk_size):
db = setup_db(DB, USER, PASS, PORT)
req = get_mp_request(MP_REQUEST)
lg("-------------------")
sys.stdout.write(req)
lg("-------------------")
s = socket.socket()
s.connect(('127.0.0.1', PORT))
lg("Connected to localhost:",PORT, "sending", len(MP_REQUEST)," bytes")
lg("Min Time:", min_time, "Chunk size:", chunk_size)
send_paced(s, req, min_time, chunk_size)
s.shutdown(1)
lg("------------receiving -------------")
s.settimeout(2)
rdata = s.recv(8192)
sys.stdout.write(rdata)
lg("-----------------------------------")
if __name__=='__main__':
import argparse
p = argparse.ArgumentParser()
p.add_argument('--debug', action="store_true")
p.add_argument('--min_time', type=float, default=MIN_TIME)
p.add_argument('--chunk_size', type=int, default=CHUNK_SIZE)
args = p.parse_args()
if args.debug:
DEBUG = True
sys.exit(main(args.min_time, args.chunk_size))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment