Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
processing bad JSON
#1
i am using json.loads().   the data looks bad to me Wall , but potentially usable Think    is it possible in python or with python' json module? Pray  here is where i am getting it: https://ipapi.co/json
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
It not wrong is valid JSON(RFC 4627) jsonformatter,and use Requests.
>>> import requests

>>> url_get = requests.get('https://ipapi.co/json')
>>> url_get.json()
{'city': None,
 'country': 'SE',
 'ip': '95.143.193.193',
 'latitude': 59.3247,
 'longitude': 18.056,
 'postal': None,
 'region': None,
 'timezone': 'Europe/Stockholm'}

>>> data = url_get.json()
>>> data['ip']
'95.143.193.193'
>>> data['timezone']
'Europe/Stockholm'
Reply
#3
json module works fine with the example.
Reply
#4
my code seems to have trouble with it.  this programs runs a process for each site in parallel,  the child process for ipapi.co just disappears.  the exception handling never happens.  i just did strace to a file and have yet to deeply study it.

here is the source with some debugging message code still in place.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import division, print_function, unicode_literals
"""
file          getmyip.py
purpose       Get the public IP address of this host
command       python getmyip.py
function      get_this_ip()
aruments      none
note          this script accesses multiple web sites in parallel by
              means of the multiprocessing module.
email         10054452614123394844460370234029112340408691

The intent is that this command works correctly under both Python 2 and
Python 3.  Please report failures or code improvement to the author.
"""

__license__ = """
Copyright (C) 2017, by Phil D. Howard - all other rights reserved

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

The author may be contacted by decoding the number
10054452614123394844460370234029112340408691
(provu igi la numeron al duuma)
"""

import os, sys
env = os.environ
from collections import Counter
from multiprocessing import Pipe, Process
from sys import argv, exc_info, stderr, stdout, version_info
from time import sleep

if version_info.major < 3:
    from urllib2 import URLError, urlopen
else:
    from urllib.request import urlopen
    from urllib.error import URLError


http_timeout = 30

site_list = [
    ( 'https://ipapi.co/json', 'json', 'ip' ),                              # special case must stay at index #0
    ( 'https://tools.keycdn.com/geo.json', 'json', ('data','geo','ip') ),   # special case must stay at index #1
    ( 'http://icanhazip.com/', None ),
    ( 'http://jsonip.com/', 'json', 'ip' ),
    ( 'http://ipinfo.io/json', 'json', 'ip' ),
    ( 'https://ifconfig.co/json', 'json', 'ip' ),
    ( 'http://ipecho.net/plain', None ),
    ( 'http://whatismyip.akamai.com/', None ),
    ( 'http://v4.ipv6-test.com/api/myip.php', None ),
    ( 'https://api.ipify.org?format=json', 'json', 'ip' ),
    ( 'http://ip-api.com/json', 'json', 'query' ),
    ( 'https://freegeoip.net/json/', 'json', 'ip' ),
    ( 'https://ifcfg.me/json', 'json', 'ip' ),
    ( 'http://api.ipinfodb.com/v3/ip-country/?format=json', 'json', 'ipAddress' ),
    ( 'http://ip.jsontest.com/', 'json', 'ip' ),
    ( 'https://wtfismyip.com/json', 'json', 'YourFuckingIPAddress' ),
]


def child( site_index, this_pipe ):
    """
function      child
purpose       run in each of many child processes to get the IP address
              as this IP address server site sees it.
argument      1 (int) index (starting at 0) of which site to access.
              2 (int) fd of pipe to send result back to parent
outputs       IP address
returns       (int) return status code
"""
    if site_index >= len( site_list ):
        return -1
    url, method, extra = (site_list[site_index]+(0,))[:3]
    os.close(this_pipe[0])
    os.dup2(this_pipe[1],1)
    os.close(this_pipe[1])
    try:
        print(repr(100+site_index),'   url =',repr(url)[:152],file=stderr)
        conn = urlopen( url, timeout = http_timeout )
        data = conn.read().strip()
        print(repr(100+site_index),'no exception',file=stderr)
    except URLError:
        print(repr(100+site_index),'URLError',file=stderr)
        os._exit(1)
    if version_info.major > 2:
        if 'decode' in dir(data):
            data = data.decode()
    addr = data = data.strip()
    print(repr(100+site_index),'  data =',repr(data)[:152],file=stderr)
    if method == 'json':
        import json
        if site_index==1:
            addr = json.loads(data)
            for x in extra:
                addr = addr[x]
        if isinstance(extra,str):
            addr = json.loads(data)[extra]
    if method == 'html':
        data = ' '.join('>'.join(data.split('<')).split('>')).split()
        addr = data[extra]
    if 'verbose' in env:
        print(addr,'from',url,file=stderr)
    if addr in (None,False,True):
        os._exit(1)
    if len(addr) > 0:
        print( addr )
    exit(0)



def get_many_ips( number_of_sites ):
    """
function      get_many_ips
purpose       run many child processes, with a pipe from each, to get
              the IP address this host appears to have as seen by each
              of a list of IP address sources.
arguments     -none-
returns       (list) of IP addresses
"""
    pipes = []
    procs = []
    addrs = []
    sleepy = 3.0 / 256.0
    site_range = range( number_of_sites )
    for site_index in site_range:
        this_pipe = os.pipe()
        this_proc = Process( target = child, args = (site_index,this_pipe) )
        pipes.append( this_pipe )
        procs.append( this_proc )
        this_proc.start()
        print(repr(100+site_index),'  site started',file=stderr)
        os.close(this_pipe[1])
    for site_index in site_range:
        addr = ''
        if version_info.major > 2:
            addr = addr.encode()
        while True:
            data = os.read(pipes[site_index][0],4096)
            if len(data) > 0:
                addr += data
            if len(data) < 1:
                break
        if version_info.major > 2:
            addr = addr.decode()
        addrs.append( addr.strip() )
        sleep( sleepy )
    for site_index in site_range:
        procs[site_index].join( 360 )
    return addrs


def get_this_ip():
    """
function      get_my_ip
purpose       gets a list of IP addresses from get_many_ips() and
              one most common IP address from that list.
arguments     -none-
returns       (str) most common IP address, (int) agree count, (int)
              total sources count
"""
    tutaj = Counter()
    addrs = get_many_ips( len( site_list ) )
    for addr in addrs:
        tutaj[addr] += 1
    addr = tutaj.most_common(1)[0][0]
    agree = tutaj.most_common(1)[0][1]
    sources = len(addrs)
    return (addr,agree,sources)


def get_ip():
    return get_this_ip()[0]


def main( args ):
    if version_info.major > 2:
        typestr = str
    else:
        typestr = basestring
    addr,agree,sources = get_this_ip()
    if addr == None:
        print( 'failed to get IP address, got:', repr(addr),  file=stderr )
        return 1
    if not isinstance( addr, typestr ):
        print( 'failed to get an IP address, got:', repr(addr),  file=stderr )
        return 2
    if len( addr ) < 9:
        print( 'failed to get a valid IP address, got:', repr(addr),  file=stderr )
        return 3
    if '.' not in addr:
        print( 'failed to get any valid IP address, got:', repr(addr),  file=stderr )
        return 4
    print( '{} of {} sources agree that your IP address is:'.format(repr(agree),repr(sources)), file=stderr )
    stderr.flush()
    print( addr )
    return 0

if __name__ == '__main__':
    try:
        result = main( argv )
        stdout.flush()
    except KeyboardInterrupt:
        result = 141
        print( '' )
    except IOError:
        result = 142
    try:
        exit( int( result ) )
    except ValueError:
        print( str( result ), file=stderr )
        exit( 1 )
    except TypeError:
        if result == None:
            exit( 0 )
        exit( 255 )

# EOF
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020