Last active
November 22, 2016 22:53
-
-
Save nickva/9cb035cd1e3ef38ec493fd6cb122435d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys, socket, hashlib | |
import couchdb | |
import base64 | |
def setup_db(dbname, user, password, port): | |
url = 'http://%s:%[email protected]:%d'% (user, password, port) | |
print "url:", url | |
srv=couchdb.Server(url) | |
print "srv:", srv.resource.url | |
if dbname in srv: | |
print "Deleting previous db", dbname | |
srv.delete(dbname) | |
srv.create(dbname) | |
print "Created DB:", dbname | |
srv | |
def get_body(att_size): | |
att = 'x' * att_size | |
att_md5 = hashlib.md5(att).digest().encode('base64').strip() | |
return '\r\n'.join([ | |
'--195044bdf7abd8b23cce7b30d39a19dd', | |
'Content-Type: application/json', | |
'', | |
'''{"_id":"doc2","_rev":"1-2df9eed63e6f4df24c6a7b593adfc195","_revisions":{"start":1,"ids":["2df9eed63e6f4df24c6a7b593adfc195"]}, | |
"_attachments":{"att2":{"content_type":"app/binary","revpos":1, "digest":"md5-%s", "length":%s, "follows":true}}} | |
''' % (att_md5, att_size), | |
'--195044bdf7abd8b23cce7b30d39a19dd', | |
'Content-Disposition: attachment; filename="att2"', | |
'Content-Type: app/binary', | |
'Content-Length: %s' % att_size, | |
'', | |
att, | |
'--195044bdf7abd8b23cce7b30d39a19dd--', | |
]) | |
def get_mp_request(dbname, port, att_size, user, pwd): | |
body = get_body(att_size) | |
b64 = base64.b64encode("%s:%s" % (user, pwd)) | |
return '\r\n'.join([ | |
'PUT /%s/doc2?new_edits=false HTTP/1.1' % dbname, | |
'Authorization : Basic %s' % b64, | |
'Content-Type: multipart/related; boundary="195044bdf7abd8b23cce7b30d39a19dd"', | |
'Content-Length: %d' % (len(body)), | |
'Accept: application/json', | |
'Host: 127.0.0.1:%d' % port, | |
'', | |
body, | |
]) | |
def send_recv(s,data): | |
err = None | |
try: | |
s.sendall(data) | |
except socket.error, e: | |
err = e | |
print "!!! Socket Error:",e | |
print "Sent data:",len(data),"bytes" | |
rdata = s.recv(4096) | |
print "Received:",len(rdata),"bytes" | |
print >>sys.stderr,rdata | |
return (rdata,err) | |
def main(user='adm',password='pass',port=15984, att_size=1): | |
dbname = 'db' | |
print "Settings DB:", dbname, "Port:", port, "User:", user, "Pass:", password, "Size:",att_size | |
srv = setup_db(dbname, user, password, port) | |
req = get_mp_request(dbname, port, att_size, user, password) | |
s = socket.socket() | |
s.connect(('127.0.0.1',port)) | |
print "Connected to localhost:",port | |
rdata,err = send_recv(s,req) | |
if len(rdata)==0: | |
print "received 0 bytes, socket closed" | |
return 0 | |
if err: | |
print "error sending data", err | |
if 'Connection: close' in rdata: | |
return 1 | |
if __name__=='__main__': | |
import argparse | |
p = argparse.ArgumentParser() | |
p.add_argument('--port',type=int, default=15984) | |
p.add_argument('--user', default='adm') | |
p.add_argument('--password', default='pass') | |
p.add_argument('--size',type=int, default=1) | |
args = p.parse_args() | |
sys.exit(main(args.user, args.password, args.port, args.size)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment