Exemplo n.º 1
0
def wput(data, URL):
    """Upload a file across the network
    data: content of the file to upload.
    URL: e.g. "//id14timing3.cars.aps.anl.gov:2001/tmp/test.txt"
    """
    ##debug("%s, %d bytes %r " % (URL,len(data),data[0:21]))
    with lock:  # Allow only one thread at a time inside this function.
        import socket
        url = URL
        default_port = 80 if url.startswith("http:") else default_port_number
        url = url.replace("http:", "")
        if url.startswith("//"): url = url[2:]
        ip_address_and_port = url.split("/")[0].split("@")[-1]
        if not ":" in ip_address_and_port:
            ip_address_and_port += ":" + str(default_port)
        pathname = "/" + "/".join(url.split("/")[1:])
        s = "PUT %s\n" % pathname
        s += "Content-Length: %d\n" % len(data)
        s += "\n"
        s += data
        for attempt in range(0, 2):
            try:
                c = connection(ip_address_and_port)
                if c is None: break
                c.sendall(s)
            except socket.error:
                continue
            break
Exemplo n.º 2
0
def wsize(URL):
    """Download a file from the network
    URL: e.g. "//id14timing3.cars.aps.anl.gov:2001/tmp/test.txt"
    """
    with lock:  # Allow only one thread at a time inside this function.
        import socket
        url = URL
        default_port = 80 if url.startswith("http:") else default_port_number
        url = url.replace("http:", "")
        if url.startswith("//"): url = url[2:]
        ip_address_and_port = url.split("/")[0]
        if not ":" in ip_address_and_port:
            ip_address_and_port += ":" + str(default_port)
        pathname = "/" + "/".join(url.split("/")[1:])
        s = "SIZE %s\n" % pathname
        s += "\n"
        data = ""
        for attempt in range(0, 2):
            try:
                c = connection(ip_address_and_port)
                if c is None: break
                c.sendall(s)
                reply = ""
                while not "\n\n" in reply:
                    r = c.recv(65536)
                    if len(r) == 0: break
                    reply += r
                if len(r) == 0: continue
                header_size = reply.find("\n\n") + 2
                keyword = "Content-Length: "
                if not keyword in reply: return ""
                start = reply.find(keyword) + len(keyword)
                end = start + reply[start:].find("\n")
                file_size = int(reply[start:end])
                while len(reply) < header_size + file_size:
                    r = c.recv(65536)
                    if len(r) == 0: break
                    reply += r
                if len(r) == 0: continue
                data = reply[header_size:]
                if len(data) != file_size:
                    warn("file server %s: expecting %d,got %d bytes" %
                         (ip_address_and_port, file_size, len(data)))
            except socket.error:
                continue
            break
        data = data.strip()
        try:
            size = int(data)
        except:
            warn("file server %s: expecting integer, got %r" %
                 (ip_address_and_port, data))
            size = 0
        return size
Exemplo n.º 3
0
def wdel(URL):
    """Download a file from the network
    URL: e.g. "//id14timing3.cars.aps.anl.gov:2001/tmp/test.txt"
    """
    with lock:  # Allow only one thread at a time inside this function.
        import socket
        url = URL
        default_port = 80 if url.startswith("http:") else default_port_number
        url = url.replace("http:", "")
        if url.startswith("//"): url = url[2:]
        ip_address_and_port = url.split("/")[0]
        if not ":" in ip_address_and_port:
            ip_address_and_port += ":" + str(default_port)
        pathname = "/" + "/".join(url.split("/")[1:])
        s = "DEL %s\n" % pathname
        s += "\n"
        for attempt in range(0, 2):
            try:
                c = connection(ip_address_and_port)
                if c is None: break
                c.sendall(s)
            except socket.error:
                continue
            break