import urllib
import urllib3
# print ('------------------------error 404 ---------------------------------')
url = 'https://api.ordnancesurvey.co.uk/opennames/v1/find?'
values = {'query': '''9 Louisa Oakes Close,
          London,
          Greater London,
          E4 8AS England''',
          'key': 'wLFnRmX8mmjwob5ptbnkjaZfuNVKN3WA'}
data = urllib.urlencode(values)
req = urllib3.Request(url, data)
http = urllib3.PoolManager()

try:
    f = http.request('GET', req)

except urllib3.HTTPError as e:
            if e.code == 401:
                print('401 not authorized')
            elif e.code == 404:
                print('404 not found')
            elif e.code == 503:
                print('service unavailable')
            else:
                print('unknown error: ')
else:
            print('success')

            response=f.read()
            print('RESPONSE:', response)

            for line in response.splitlines():

                word_lst = line.split(':')
                for word in word_lst:
                    if '"NAME1" ' in word: print(line)
                    if 'GEOMETRY' in word: print(line)
                    if 'ID' in word: print(line)
            f.close()
# print ('------------------------error 401 ------------------------------------------')

url = 'https://api.ordnancesurvey.co.uk/opennames/v1/find?'
values = {'query':'''E4 8AS england''',
          'key':'wLFnRmX8mmjwob5ptbnkjaZfuNVKN3WA'}
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
http = urllib3.PoolManager()

try:
    f = http.request('GET', req)
except urllib3.HTTPError as e:
            if e.code == 401:
                print('401 not authorized')
            elif e.code == 404:
                print('404 not found')
            elif e.code == 503:
                print('service unavailable')
            else:
                print('unknown error: ')
else:
            print('success')

            response=f.read()
            print('RESPONSE:', response)

            for line in response.splitlines():

                word_lst = line.split(':')
                for word in word_lst:
                    if '"NAME1" ' in word: print(line)
                    if 'GEOMETRY' in word: print(line)
                    if 'ID' in word: print(line)
            f.close()

# print ('------------------------second try ------------------------------------------')
url = 'https://api.ordnancesurvey.co.uk/opennames/v1/find'
values = {'query':'''E4 8AS england''',
          'key':'wLFnRmX8mmjwob5ptbnkjaZfuNVKN3WA'}
full_url = url + '?' + data
print(full_url)
http = urllib3.PoolManager()

try:
    f = http.request('GET', full_url)
except urllib3.HTTPError as e:
            if e.code == 401:
                print('401 not authorized')
            elif e.code == 404:
                print('404 not found')
            elif e.code == 503:
                print('service unavailable')
            else:
                print('unknown error: ')
else:
            print('success')

            response=f.read()
            print('RESPONSE:', response)
            response_count = 0
            for line in response.splitlines():
                word_lst = line.split(':')
                for word in word_lst:

                    if response_count < 10:

                        if 'ID' in word:
                            response_count = response_count + 1
                            print('-'*80)
                            print(line)
                        if '"NAME1" ' in word: print(line)
                        if 'GEOMETRY' in word: print(line)

            f.close()
