the return of urllib.request.urlopen("http://www.example.com/", params)

tunpishuang tunpishuang at gmail.com
Wed Apr 1 06:59:00 CEST 2009


On Apr 1, 12:49 pm, Steven D'Aprano
<ste... at REMOVE.THIS.cybersource.com.au> wrote:
> On Tue, 31 Mar 2009 21:17:29 -0700, tunpishuang wrote:
> > hey guys , i'm new in python ...here i got a little problem that get me
> > confused...
> > i wanna do an uthentication of an login page , here is the example from
> > the python lib ref. :
>
> >>>> import urllib.request
> >>>> import urllib.parse
> >>>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) f
> >>>> = urllib.request.urlopen("http://www.musi-cal.com/cgi-bin/query?%s" %
> >>>> params) print(f.read())
>
> > i wanted to know what the the return value of f ,,if it's an instance of
> > http.client.HTTPResponse
> > and why the return source of the web page is quote like this :
>
> > b'<html></html>'
>
> Looks like you are using Python 3.0.
>
> In Python 2.x, the native string type is bytes (commonly called "ASCII").
> So a string of bytes is displayed like this:
>
> 'abcdef...'
>
> and a string of Unicode characters is displayed like this:
>
> u'abcdef...'
>
> In Python 3.0, the native string type is unicode, but HTTP responses are
> bytes. Bytes are displayed like this:
>
> b'abcdef...'
>
> and strings of characters like this:
>
> 'abcdef...'
>
> Notice that the B on the outside of the quotes is not part of the string,
> it is part of the display format, just like the quotes themselves.
>
> You can convert the bytes into a string by just called str() on the
> output:
>
> # untested>>> print(str(f.read()))
>
> '<html></html>'
>
> This should work for the simple case, although once you start getting
> more complicated strings, with international (non-English) characters,
> you will need to supply an encoding.
>
> Does this help?
>
> --
> Steven


so much thanks Steven ,,,
one day u came to travel in China ,i'll be you guide~ :)



More information about the Python-list mailing list