Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #42240 > unrolled thread

HTTPConnection.send

Started bydspublic@freemail.hu
First post2013-03-29 05:27 -0700
Last post2013-03-29 09:32 -0700
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  HTTPConnection.send dspublic@freemail.hu - 2013-03-29 05:27 -0700
    Re: HTTPConnection.send dspublic@freemail.hu - 2013-03-29 06:40 -0700
    Re: HTTPConnection.send Peter Otten <__peter__@web.de> - 2013-03-29 15:49 +0100
    Re: HTTPConnection.send Chris Angelico <rosuav@gmail.com> - 2013-03-30 01:58 +1100
    Re: HTTPConnection.send dspublic@freemail.hu - 2013-03-29 09:32 -0700

#42240 — HTTPConnection.send

Fromdspublic@freemail.hu
Date2013-03-29 05:27 -0700
SubjectHTTPConnection.send
Message-ID<64ae9389-ef1b-4852-94a9-7cc5c228238c@googlegroups.com>
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 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?

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))

[toc] | [next] | [standalone]


#42242

Fromdspublic@freemail.hu
Date2013-03-29 06:40 -0700
Message-ID<64ec269f-be78-45bb-a916-bbbbbfcac289@googlegroups.com>
In reply to#42240
Problematical python versions: 3.2+ (2.x, 3.0, 3.1 ok)

[toc] | [prev] | [next] | [standalone]


#42248

FromPeter Otten <__peter__@web.de>
Date2013-03-29 15:49 +0100
Message-ID<mailman.3951.1364568526.2939.python-list@python.org>
In reply to#42240
dspublic@freemail.hu 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))

[toc] | [prev] | [next] | [standalone]


#42251

FromChris Angelico <rosuav@gmail.com>
Date2013-03-30 01:58 +1100
Message-ID<mailman.3952.1364569116.2939.python-list@python.org>
In reply to#42240
On Fri, Mar 29, 2013 at 11:27 PM,  <dspublic@freemail.hu> 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?

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

[toc] | [prev] | [next] | [standalone]


#42254

Fromdspublic@freemail.hu
Date2013-03-29 09:32 -0700
Message-ID<856134d5-1808-4e94-bcd4-592b6acc09af@googlegroups.com>
In reply to#42240
Thanx for confirmations...

I have reported http://bugs.python.org/issue17575 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web