osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

python 2 urlopen vs python 3 urlopen


On Tue, Aug 28, 2018 at 3:25 AM, Sean Darcy <seandarcy2 at gmail.com> wrote:
> python 2 :
>
> python
> Python 2.7.15 (default, May 15 2018, 15:37:31)
> .....
>>>> import urllib2
>>>> res = urllib2.urlopen('https://api.ipify.org').read()
>>>> print res
> www.xxx.yyy.zzz
>
> python3
>
> python3
> Python 3.6.6 (default, Jul 19 2018, 16:29:00)
> ...
>>>> from urllib.request import urlopen
>>>> res = urlopen('https://api.ipify.org').read()
>>>> print(res)
> b'ww.xxx.yyy.zzz'
>
> I'm expecting the python 2 result, just the ip address. How can I get
> python 3 just to give the address, and not include  b'      '  ? I
> know I can mess with string manipulation, but I want to keep it
> simple.

You got back the address as a series of bytes. If you want it as text,
decode it:

print(res.decode("ascii"))

ChrisA