示例#1
0
 def connect(self):
     try:
         # FIXME: The -o option requires knc >= 1.7.
         process = subprocess.Popen([
             self.executable, '-o', 'no-half-close',
             self.service + '@' + self.host,
             str(self.port)
         ],
                                    stdin=subprocess.PIPE,
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
     except OSError, e:
         raise httplib.NotConnected(e)
示例#2
0
def safesend(self, str):
    """Send `str' to the server.

    Shamelessly ripped off from httplib to patch a bad behavior.
    """
    # _broken_pipe_resp is an attribute we set in this function
    # if the socket is closed while we're sending data but
    # the server sent us a response before hanging up.
    # In that case, we want to pretend to send the rest of the
    # outgoing data, and then let the user use getresponse()
    # (which we wrap) to get this last response before
    # opening a new socket.
    if getattr(self, '_broken_pipe_resp', None) is not None:
        return

    if self.sock is None:
        if self.auto_open:
            self.connect()
        else:
            raise httplib.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:
        blocksize = 8192
        read = getattr(str, 'read', None)
        if read is not None:
            if self.debuglevel > 0:
                print "sendIng a read()able"
            data = read(blocksize)
            while data:
                self.sock.sendall(data)
                data = read(blocksize)
        else:
            self.sock.sendall(str)
    except socket.error, v:
        reraise = True
        if v[0] == errno.EPIPE:  # Broken pipe
            if self._HTTPConnection__state == httplib._CS_REQ_SENT:
                self._broken_pipe_resp = None
                self._broken_pipe_resp = self.getresponse()
                reraise = False
            self.close()
        if reraise:
            raise
示例#3
0
        def send(self, str):
            """ Same as httplib.HTTPConnection.send(), except we don't 
            check for sigpipe and close the connection.  If the connection 
            gets closed, getresponse() fails. 
            """

            if self.sock is None:
                if self.auto_open:
                    self.connect()
                else:
                    raise httplib.NotConnected()
            if self.debuglevel > 0:
                print "send:", repr(str)
            self.sock.sendall(str)
示例#4
0
    def _read_chunk(self):
        # This should always occur on a chunk boundry, so we should never
        # need to store a lot of text in the buffer.

        text = ''
        chunk_size = None

        if len(self._buffer) < 32:
            ready, d1, d2 = select.select([self._socket], [], [])
            if len(ready) == 0:
                raise httplib.NotConnected()

            text = self._socket.recv(32)
            if len(text) == 0:
                self._socket.close()
                raise httplib.NotConnected()

        self._buffer += text
        eol = self._buffer.find('\r\n')
        if eol >= 0:
            line = self._buffer[:eol]
            i = line.find(';')
            if i >= 0:
                line = line[:i]  # strip chunk-extensions
            try:
                chunk_size = int(line, 16) + 2
            except ValueError:
                # close the connection as protocol synchronisation is
                # probably lost
                self._socket.close()
                raise httplib.IncompleteRead(line)
            self._buffer = self._buffer[(eol + 2):]

        chunk = self._read_all(chunk_size)
        self._buffer = chunk[chunk_size:]

        return chunk[:(chunk_size - 2)]
示例#5
0
def _incrementalSend(self, data):
    """Send `data' to the server."""
    if self.sock is None:
        if self.auto_open:
            self.connect()
        else:
            raise httplib.NotConnected()
    # if it's not a file object, make it one
    if not hasattr(data, 'read'):
        data = StringIO(data)
    while 1:
        block = data.read(CHUNK_SIZE)
        if not block:
            break
        self.sock.sendall(block)
        runHook("httpSend", len(block))
示例#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.
        """
        # Based on python 2.6's httplib.HTTPConnection.send()
        if self.sock is None:
            if self.auto_open:
                self.connect()
            else:
                raise httplib.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:
            Logger.debug("send:", repr(value))
        try:
            blocksize = 8192
            if hasattr(value, 'read') :
                if hasattr(value, 'seek'):
                    value.seek(0)
                if self.debuglevel > 0:
                    Logger.debug("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:
                    Logger.debug("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:      # Broken pipe
                self.close()
            raise
示例#7
0
def fast_http_send(self, data):
    """Send `data' to the server."""
    if self.sock is None:
        if self.auto_open:
            self.connect()
        else:
            raise httplib.NotConnected()

    if self.debuglevel > 0:
        print "send:", repr(data)
    blocksize = 2**20  # was 8192 originally
    if hasattr(data, 'read') and not isinstance(data, array):
        if self.debuglevel > 0:
            print "sendIng a read()able"
        datablock = data.read(blocksize)
        while datablock:
            self.sock.sendall(datablock)
            datablock = data.read(blocksize)
    else:
        self.sock.sendall(data)
示例#8
0
def get_headers(domain, path, filename):
    '''
    @path is the full path of the file to be downloaded
    @filename is the name of the file to be downloaded
    '''
    try:
        if domain.startswith('http://'):
            domain = domain[7:]
        conn = httplib.HTTPConnection(domain)
        if filename != None:
            url = '%s%s' % (path, filename)
        else:
            url = '%s' % path
        conn.request('HEAD', url)
        res = conn.getresponse()
        conn.close()
        return res

    except httplib.socket.error:
        raise httplib.NotConnected('It seems that %s is temporarily \
            unavailable, please try again later.' % url)
示例#9
0
    def send(self, str):
        'Send str to the server'

        log = self.log
        self = self.conn  # tricky!
        if self.sock is None:
            if self.auto_open:
                self.connect()
            else:
                raise httplib.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.
        log('send: ' + repr(str)[:300] + common.EOL, 2)
        log('send: ' + repr(str)[300:] + common.EOL, 4)
        try:
            self.sock.send(str)
        except socket.error, v:
            if v[0] == 32:  # Broken pipe
                self.close()
            raise
示例#10
0
    def recv(self, len=1024, flags=0):
        if self.process.poll():
            raise httplib.NotConnected()

        return self._stdout.read(len)
示例#11
0
    def send(self, stuff, flags=0):
        if self.process.poll():
            raise httplib.NotConnected()

        return self._stdin.write(stuff)