コード例 #1
0
ファイル: urllib2_file.py プロジェクト: sgricci/digsby
def send(self, str):
    """Send `str' to the server."""
    if self.sock is None:
        if self.auto_open:
            self.connect()
        else:
            raise NotConnected()

    # send the data to the server. if we get a broken pipe, then close
    # the socket. we want to reconnect when somebody tries to send again.
    #
    # NOTE: we DO propagate the error, though, because we cannot simply
    #       ignore the error... the caller will know if they can retry.
    if self.debuglevel > 0:
        print "send:", repr(str)
    try:
        if hasattr(str, 'boundary'):
            boundary = str.boundary
            v_files = str.v_files
            v_vars = str.v_vars
            send_data(v_vars, v_files, boundary, self.sock.sendall)
        else:
            self.sock.sendall(str)
    except socket.error, v:
        if v[0] == 32:  # Broken pipe
            self.close()
        raise
コード例 #2
0
    def send(self, data, is_chunked=False):
        """Send `data' to the server."""
        if self.sock is None:
            if self.auto_open:
                self.connect()
            else:
                raise NotConnected()

        if self.debuglevel > 0:
            print "send:", repr(data)
        blocksize = 8192
        if hasattr(data, 'read') and not isinstance(data, array):
            if self.debuglevel > 0: print "sendIng a read()able"
            datablock = data.read(blocksize)
            while datablock:
                if self.debuglevel > 0:
                    print 'chunked:', is_chunked
                if is_chunked:
                    if self.debuglevel > 0: print 'send: with trunked data'
                    lenstr = string.upper(hex(len(datablock))[2:])
                    self.sock.sendall('%s\r\n%s\r\n' % (lenstr, datablock))
                else:
                    self.sock.sendall(datablock)
                datablock = data.read(blocksize)
            if is_chunked:
                self.sock.sendall('0\r\n\r\n')
        else:
            self.sock.sendall(data)
コード例 #3
0
ファイル: net.py プロジェクト: sgricci/digsby
    def send_file_cb(self, fileobj, progress_cb, blocksize = default_chunksize, progressDelta = 0):
        'Sends the contents of fileobj (a .read-able object) to server.'

        if self.sock is None:
            if self.auto_open:
                self.connect()
            else:
                raise NotConnected()

        if self.debuglevel > 0:
            print "sending contents of", fileobj

        try:
            read    = fileobj.read
            sendall = self.sock.sendall

            chunk   = read(blocksize)
            total   = 0

            while chunk:
                total += len(chunk)
                sendall(chunk)
                progress_cb(total - progressDelta)
                chunk = read(blocksize)

        except socket.error, v:
            if v[0] == 32:      # Broken pipe
                self.close()
            raise
コード例 #4
0
ファイル: streaminghttp.py プロジェクト: hashimmm/KTS
    def send(self, value):
        """Send ``value`` to the server.

        ``value`` can be a string object, a file-like object that supports
        a .read() method, or an iterable object that supports a .next()
        method.
        """
        # Based on python 2.6's httplib.HTTPConnection.send()
        if self.sock is None:
            if self.auto_open:
                self.connect()
            else:
                raise NotConnected()

        # send the data to the server. if we get a broken pipe, then close
        # the socket. we want to reconnect when somebody tries to send again.
        #
        # NOTE: we DO propagate the error, though, because we cannot simply
        #       ignore the error... the caller will know if they can retry.
        if self.debuglevel > 0:
            print ("send:", repr(value))
        try:
            blocksize = 8192
            if hasattr(value, 'read') :
                if hasattr(value, 'seek'):
                    value.seek(0)
                if self.debuglevel > 0:
                    print ("sendIng a read()able")
                data = value.read(blocksize)
                if sys.version_info[0] > 2:
                    data = data.encode('utf8')
                while data:
                    self.sock.sendall(data)
                    data = value.read(blocksize)
            elif hasattr(value, 'next'):
                if hasattr(value, 'reset'):
                    value.reset()
                if self.debuglevel > 0:
                    print ("sendIng an iterable")
                for data in value:
                    if sys.version_info[0] > 2 and hasattr(data, 'encode'):
                        data = data.encode('utf8')
                    self.sock.sendall(data)
            else:
                self.sock.sendall(value)
        except socket.error as v:
        
            if v[0] == 32:      # Broken pipe
                self.close()
            raise
コード例 #5
0
 def request(self, method, body="", header={}, params={}):
     if not self._conn:
         raise NotConnected(
             "Client not connected. Call .connect() before sending requests"
         )
     headers = self.header.copy()
     headers.update(header)
     queries = self.params.copy()
     queries.update(params)
     resource = makeURL(self._path, queries)
     self._conn.request(method, resource, body, headers)
     meta = self._conn.getresponse()
     #data = meta.read()
     return meta
コード例 #6
0
    def send(self, value):
        """Send ``value`` to the server.
        
        ``value`` can be a string object, a file-like object that supports
        a .read() method, or an iterable object that supports a .next()
        method.
        """
        self.debuglevel = 1
        # Based on python 2.6's httplib.HTTPConnection.send()
        if self.sock is None:
            if self.auto_open:
                self.connect()
            else:
                raise NotConnected()

        # send the data to the server. if we get a broken pipe, then close
        # the socket. we want to reconnect when somebody tries to send again.
        #
        # NOTE: we DO propagate the error, though, because we cannot simply
        #       ignore the error... the caller will know if they can retry.
        try:
            blocksize = 8192
            if hasattr(value, 'read'):
                if self.debuglevel > 0: print "sendIng a read()able"
                data = value.read(blocksize)
                while data:
                    #if self.debuglevel > 0 and type(data).__name__ == "str":
                    #    print data
                    self.sock.sendall(data)
                    data = value.read(blocksize)
            elif hasattr(value, 'next'):
                if self.debuglevel > 0: print "sendIng an iterable"
                for data in value:
                    #if hasattr(data,'read') :
                    #    print "binary file data here"
                    #if self.debuglevel > 0 and ord(data[0]) < 128:
                    #    print data
                    #print "Value sent"
                    self.sock.sendall(data)
            else:
                #if self.debuglevel > 0:
                #    print "send data:", repr(value)
                self.sock.sendall(value)
        except socket.error, v:
            if v[0] == 32:  # Broken pipe
                self.close()
            raise
コード例 #7
0
    def send(self, value):
        """Send ``value`` to the server.
        
        ``value`` can be a string object, a file-like object that supports
        a .read() method, or an iterable object that supports a .next()
        method.
        """
        if self.sock is None:
            if self.auto_open:
                self.connect()
            else:
                raise NotConnected()
        if self.debuglevel > 0:
            print 'send:', repr(value)
        try:
            blocksize = 8192
            if hasattr(value, 'read'):
                if hasattr(value, 'seek'):
                    value.seek(0)
                if self.debuglevel > 0:
                    print 'sendIng a read()able'
                data = value.read(blocksize)
                while data:
                    self.sock.sendall(data)
                    data = value.read(blocksize)

            elif hasattr(value, 'next'):
                if hasattr(value, 'reset'):
                    value.reset()
                if self.debuglevel > 0:
                    print 'sendIng an iterable'
                for data in value:
                    self.sock.sendall(data)

            else:
                self.sock.sendall(value)
        except socket.error as v:
            if v[0] == 32:
                self.close()
            raise

        return
コード例 #8
0
    def send(self, value):
        """Send ``value`` to the server.

        ``value`` can be a string object, a file-like object that supports
        a .read() method, or an iterable object that supports a .next()
        method.
        """
        # Based on python 2.6's httplib.HTTPConnection.send()
        if self.sock is None:
            if self.auto_open:
                self.connect()
            else:
                raise NotConnected()

        # send the data to the server. if we get a broken pipe, then close
        # the socket. we want to reconnect when somebody tries to send again.
        #
        # NOTE: we DO propagate the error, though, because we cannot simply
        #       ignore the error... the caller will know if they can retry.
        if self.debuglevel > 0:
            print "send:", repr(value)
        try:
            blocksize = 8192
            if hasattr(value, 'read'):
                if hasattr(value, 'seek'):
                    value.seek(0)
                if self.debuglevel > 0:
                    print "sendIng a read()able"
                data = value.read(blocksize)
                while data:
                    self.sock.sendall(data)
                    data = value.read(blocksize)
            elif hasattr(value, 'next'):
                print value.mierda
                if hasattr(value, 'reset'):
                    value.reset()
                if self.debuglevel > 0:
                    print "sendIng an iterable"
            #---TUCAN (3) : Ability to limit speed and stop upload
                for data in value:
                    first = True  #Just got iterated
                    tmp_size = 0
                    BASE_SIZE = 4
                    max_speed = 20
                    if max_speed > 0:
                        max_size = max_speed / BASE_SIZE
                    else:
                        max_size = 0
                    start_seconds = time.time()
                    while (time.time() - start_seconds) < 1:
                        if max_size == 0 or tmp_size < max_size:
                            #Just got iterated ?
                            if first:
                                first = False
                            #If not, try to iterate
                            else:
                                try:
                                    data = value.next()
                                #The end is reached
                                except StopIteration:
                                    break
                            self.sock.sendall(data)
                            tmp_size += 1
                        else:
                            time.sleep(0.1)
                    print "Speed : ", BASE_SIZE * tmp_size, " kb/s"
            #---TUCAN (3) END
            else:
                self.sock.sendall(value)
        except socket.error, v:
            if v[0] == 32:  # Broken pipe
                self.close()
            raise
コード例 #9
0
    def send(self, value):
        """Send ``value`` to the server.

        ``value`` can be a string object, a file-like object that supports
        a .read() method, or an iterable object that supports a .next()
        method.
        """
        # Based on python 2.6's httplib.HTTPConnection.send()
        if self.sock is None:
            if self.auto_open:
                self.connect()
            else:
                raise NotConnected()

        # send the data to the server. if we get a broken pipe, then close
        # the socket. we want to reconnect when somebody tries to send again.
        #
        # NOTE: we DO propagate the error, though, because we cannot simply
        #       ignore the error... the caller will know if they can retry.
        if self.debuglevel > 0:
            print "send:", repr(value)
        try:
            blocksize = 8192
            if hasattr(value, 'read') :
                if hasattr(value, 'seek'):
                    value.seek(0)
                if self.debuglevel > 0:
                    print "sendIng a read()able"
                data = value.read(blocksize)
                
                #ADDED: By ruuk for progress
                so_far = 0
                file_size = 1
                if hasattr(value,'fileno'):
                    file_size = os.fstat(value.fileno())[6]
                #ADDED----------------------

                while data:
                    self.sock.sendall(data)
                    data = value.read(blocksize)
                    
                    #ADDED: By ruuk for progress
                    if PROGRESS_CALLBACK:
                        so_far += blocksize
                        if not PROGRESS_CALLBACK(so_far,file_size,'Sending...'): break
                    #ADDED----------------------
                    
            elif hasattr(value, 'next'):
                if hasattr(value, 'reset'):
                    value.reset()
                if self.debuglevel > 0:
                    print "sendIng an iterable"
                    
                #ADDED: By ruuk for progress
                so_far = 0
                file_size = 1
                if hasattr(value, 'total'):
                    file_size = value.total
                #ADDED----------------------
                
                for data in value:
                    self.sock.sendall(data)
                    
                    #ADDED: By ruuk for progress
                    if PROGRESS_CALLBACK:
                        so_far += len(data)
                        if not PROGRESS_CALLBACK(so_far,file_size,'Sending...'): break
                    #ADDED----------------------
                    
            else:
                self.sock.sendall(value)
        except socket.error, v:
            if v[0] == 32:      # Broken pipe
                self.close()
            raise