]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blobdiff - src/allmydata/scripts/common_http.py
Flesh out "tahoe magic-folder status" command
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / scripts / common_http.py
index 1cf76d021a3af53810266656f8118f904774781b..1964672bd2b416973592fb26ca85c317b62cae3c 100644 (file)
@@ -1,11 +1,12 @@
 
+import os
 from cStringIO import StringIO
 import urlparse, httplib
 import allmydata # for __full_version__
 
-from allmydata.util.stringutils import quote_output
+from allmydata.util.encodingutil import quote_output
 from allmydata.scripts.common import TahoeError
-
+from socket import error as socket_error
 
 # copied from twisted/web/client.py
 def parse_url(url, defaultPort=None):
@@ -26,6 +27,14 @@ def parse_url(url, defaultPort=None):
         path = "/"
     return scheme, host, port, path
 
+class BadResponse(object):
+    def __init__(self, url, err):
+        self.status = -1
+        self.reason = "Error trying to connect to %s: %s" % (url, err)
+        self.error = err
+    def read(self):
+        return ""
+
 
 def do_http(method, url, body=""):
     if isinstance(body, str):
@@ -53,11 +62,15 @@ def do_http(method, url, body=""):
     c.putheader("Connection", "close")
 
     old = body.tell()
-    body.seek(0, 2)
+    body.seek(0, os.SEEK_END)
     length = body.tell()
     body.seek(old)
     c.putheader("Content-Length", str(length))
-    c.endheaders()
+
+    try:
+        c.endheaders()
+    except socket_error, err:
+        return BadResponse(url, err)
 
     while True:
         data = body.read(8192)