2007-08-16 19:15:38 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2007-08-17 23:29:50 +00:00
|
|
|
import re, socket
|
2007-08-16 19:15:38 +00:00
|
|
|
|
2007-08-17 19:54:47 +00:00
|
|
|
NODEURL_RE=re.compile("http://([^:]*)(:([1-9][0-9]*))?")
|
2007-08-16 19:15:38 +00:00
|
|
|
|
2007-08-23 20:27:00 +00:00
|
|
|
def put(nodeurl, local_fname, vdrive_fname, verbosity):
|
2007-08-16 19:15:38 +00:00
|
|
|
"""
|
|
|
|
@param verbosity: 0, 1, or 2, meaning quiet, verbose, or very verbose
|
|
|
|
|
|
|
|
@return: a Deferred which eventually fires with the exit code
|
|
|
|
"""
|
2007-08-17 19:54:47 +00:00
|
|
|
mo = NODEURL_RE.match(nodeurl)
|
2007-08-16 19:15:38 +00:00
|
|
|
host = mo.group(1)
|
|
|
|
port = int(mo.group(3))
|
|
|
|
|
2007-08-23 20:27:00 +00:00
|
|
|
url = "/vdrive/global/"
|
2007-08-16 19:15:38 +00:00
|
|
|
if vdrive_fname:
|
|
|
|
url += vdrive_fname
|
|
|
|
|
2007-08-17 21:59:49 +00:00
|
|
|
infileobj = open(local_fname, "rb")
|
|
|
|
infileobj.seek(0, 2)
|
|
|
|
infilelen = infileobj.tell()
|
|
|
|
infileobj.seek(0, 0)
|
2007-08-16 19:15:38 +00:00
|
|
|
|
|
|
|
so = socket.socket()
|
|
|
|
so.connect((host, port,))
|
|
|
|
|
|
|
|
CHUNKSIZE=2**16
|
2007-08-17 21:59:49 +00:00
|
|
|
data = "PUT %s HTTP/1.1\r\nConnection: close\r\nContent-Length: %s\r\nHostname: %s\r\n\r\n" % (url, infilelen, host,)
|
2007-08-16 19:15:38 +00:00
|
|
|
while data:
|
|
|
|
try:
|
|
|
|
sent = so.send(data)
|
|
|
|
except Exception, le:
|
2007-08-16 23:01:01 +00:00
|
|
|
print "got socket error: %s" % (le,)
|
2007-08-16 19:15:38 +00:00
|
|
|
return -1
|
|
|
|
|
|
|
|
if sent == len(data):
|
|
|
|
data = infileobj.read(CHUNKSIZE)
|
|
|
|
else:
|
|
|
|
data = data[sent:]
|
|
|
|
|
|
|
|
respbuf = []
|
|
|
|
data = so.recv(CHUNKSIZE)
|
|
|
|
while data:
|
|
|
|
respbuf.append(data)
|
|
|
|
data = so.recv(CHUNKSIZE)
|
|
|
|
|
|
|
|
so.shutdown(socket.SHUT_WR)
|
2007-08-16 23:30:39 +00:00
|
|
|
|
2007-08-16 19:15:38 +00:00
|
|
|
data = so.recv(CHUNKSIZE)
|
|
|
|
while data:
|
|
|
|
respbuf.append(data)
|
|
|
|
data = so.recv(CHUNKSIZE)
|
|
|
|
|
2007-08-16 23:30:39 +00:00
|
|
|
respstr = ''.join(respbuf)
|
|
|
|
|
2007-08-17 19:06:41 +00:00
|
|
|
headerend = respstr.find('\r\n\r\n')
|
|
|
|
if headerend == -1:
|
|
|
|
headerend = len(respstr)
|
|
|
|
header = respstr[:headerend]
|
|
|
|
RESP_RE=re.compile("^HTTP/[0-9]\.[0-9] ([0-9]*) *([A-Za-z_ ]*)") # This regex is soooo ad hoc... --Zooko 2007-08-16
|
|
|
|
mo = RESP_RE.match(header)
|
2007-08-16 23:30:39 +00:00
|
|
|
if mo:
|
|
|
|
code = int(mo.group(1))
|
|
|
|
word = mo.group(2)
|
|
|
|
|
|
|
|
if code in (200, 201,):
|
|
|
|
print "%s %s" % (code, word,)
|
|
|
|
return 0
|
|
|
|
|
2007-08-17 19:06:41 +00:00
|
|
|
print respstr[headerend:]
|
2007-08-16 23:30:39 +00:00
|
|
|
return 1
|
|
|
|
|
2007-08-16 19:15:38 +00:00
|
|
|
def main():
|
2007-08-17 19:54:47 +00:00
|
|
|
import optparse, re
|
2007-08-16 19:15:38 +00:00
|
|
|
parser = optparse.OptionParser()
|
2007-08-17 19:54:47 +00:00
|
|
|
parser.add_option("-u", "--node-url", dest="nodeurl")
|
2007-08-16 19:15:38 +00:00
|
|
|
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2007-08-17 19:54:47 +00:00
|
|
|
NODEURL_RE=re.compile("http://([^:]*)(:([1-9][0-9]*))?")
|
|
|
|
if not isinstance(options.nodeurl, basestring) or not NODEURL_RE.match(options.nodeurl):
|
|
|
|
raise ValueError("--node-url is required to be a string and look like \"http://HOSTNAMEORADDR:PORT\", not: %r" % (options.nodeurl,))
|
|
|
|
|
2007-08-16 19:15:38 +00:00
|
|
|
local_file = args[0]
|
2007-08-17 20:23:16 +00:00
|
|
|
vdrive_fname = None
|
2007-08-16 19:15:38 +00:00
|
|
|
if len(args) > 1:
|
2007-08-17 20:23:16 +00:00
|
|
|
vdrive_fname = args[1]
|
2007-08-16 19:15:38 +00:00
|
|
|
|
2007-08-23 20:27:00 +00:00
|
|
|
return put(options.nodeurl, vdrive_fname, local_file)
|
2007-08-16 19:15:38 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|