HTTPConnection.send

Discussion in 'Python' started by dspublic@freemail.hu, Mar 29, 2013.

  1. Guest

    Hi!

    I have a problem with HTTPConnection object send() method (pyver3.3.1). I want to send data from file-like object with HTTPConnection.send( f ), and Iget a "data should be a bytes-like object or an iterable, ..." exception. I have investigated a send method, and discovered a problem: if data has a read attribute, send it , and try send it again with self.sock.sendall(data). My opinion is need an "else" after the "if hasattr(data, "read")"

    Please, somebody help me. Does it a real BUG or my mistake?

    http.client.py >

    if hasattr(data, "read") :
    if self.debuglevel > 0:
    print("sendIng a read()able")
    encode = False
    try:
    mode = data.mode
    except AttributeError:
    # io.BytesIO and other file-like objects don't have a `mode`
    # attribute.
    pass
    else:
    if "b" not in mode:
    encode = True
    if self.debuglevel > 0:
    print("encoding file using iso-8859-1")
    while 1:
    datablock = data.read(blocksize)
    if not datablock:
    break
    if encode:
    datablock = datablock.encode("iso-8859-1")
    self.sock.sendall(datablock)
    ELSE: #!!!! i guess missing !!!!
    try:
    self.sock.sendall(data)
    except TypeError:
    if isinstance(data, collections.Iterable):
    for d in data:
    self.sock.sendall(d)
    else:
    raise TypeError("data should be a bytes-like object "
    "or an iterable, got %r" % type(data))
     
    , Mar 29, 2013
    #1
    1. Advertising

  2. Guest

    Problematical python versions: 3.2+ (2.x, 3.0, 3.1 ok)
     
    , Mar 29, 2013
    #2
    1. Advertising

  3. Peter Otten Guest

    wrote:

    > I have a problem with HTTPConnection object send() method (pyver3.3.1). I
    > want to send data from file-like object with HTTPConnection.send( f ), and
    > I get a "data should be a bytes-like object or an iterable, ..."
    > exception. I have investigated a send method, and discovered a problem: if
    > data has a read attribute, send it , and try send it again with
    > self.sock.sendall(data). My opinion is need an "else" after the "if
    > hasattr(data, "read")"
    >
    > Please, somebody help me. Does it a real BUG or my mistake?


    I think your analysis is correct. Please file a bug report on
    <http://bugs.python.org>.

    > http.client.py >
    >
    > if hasattr(data, "read") :
    > if self.debuglevel > 0:
    > print("sendIng a read()able")
    > encode = False
    > try:
    > mode = data.mode
    > except AttributeError:
    > # io.BytesIO and other file-like objects don't have a
    > # `mode` attribute.
    > pass
    > else:
    > if "b" not in mode:
    > encode = True
    > if self.debuglevel > 0:
    > print("encoding file using iso-8859-1")
    > while 1:
    > datablock = data.read(blocksize)
    > if not datablock:
    > break
    > if encode:
    > datablock = datablock.encode("iso-8859-1")
    > self.sock.sendall(datablock)
    > ELSE: #!!!! i guess missing !!!!
    > try:
    > self.sock.sendall(data)
    > except TypeError:
    > if isinstance(data, collections.Iterable):
    > for d in data:
    > self.sock.sendall(d)
    > else:
    > raise TypeError("data should be a bytes-like object "
    > "or an iterable, got %r" % type(data))
     
    Peter Otten, Mar 29, 2013
    #3
  4. On Fri, Mar 29, 2013 at 11:27 PM, <> wrote:
    > I have a problem with HTTPConnection object send() method (pyver3.3.1). Iwant to send data from file-like object with HTTPConnection.send( f ), andI get a "data should be a bytes-like object or an iterable, ..." exception.. I have investigated a send method, and discovered a problem: if data has a read attribute, send it , and try send it again with self.sock.sendall(data). My opinion is need an "else" after the "if hasattr(data, "read")"
    >
    > Please, somebody help me. Does it a real BUG or my mistake?


    Yeah, I think you may be right on that. Changeset 67046 added the try
    block, and removed the else. I'd raise this on the tracker; I'd say
    the removal of else was purely accidental.

    ChrisA
     
    Chris Angelico, Mar 29, 2013
    #4
  5. Guest

    , Mar 29, 2013
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Molz
    Replies:
    3
    Views:
    3,614
    Darryl L. Pierce
    Sep 19, 2003
  2. Marko
    Replies:
    11
    Views:
    18,554
    Marko
    Oct 19, 2004
  3. Itamar Lev

    exception in HttpConnection.run

    Itamar Lev, Oct 24, 2004, in forum: Java
    Replies:
    0
    Views:
    1,748
    Itamar Lev
    Oct 24, 2004
  4. Ravi Ambros Wallau
    Replies:
    0
    Views:
    1,245
    Ravi Ambros Wallau
    Oct 25, 2004
  5. Michael Kremser

    HttpConnection on 7650 and so on

    Michael Kremser, Oct 26, 2004, in forum: Java
    Replies:
    0
    Views:
    404
    Michael Kremser
    Oct 26, 2004
Loading...

Share This Page