How to make a Python 3.9 code 'requests.get' in Lambda.

0

Hello,

I need to get the country information from IP address by GeoLite2 Country web service.

It is necessary to use 'curl' command as following. curl -u "{account_id}:{license_key}" "https://geolite.info/geoip/v2.1/country/{ip_address}?pretty"

*reference URL https://dev.maxmind.com/geoip/geolocate-an-ip/web-services?lang=en#command-line-curl-examples

But python doesn't have 'curl' command, so I need to use 'requests.get' command. I tested the code as following. (3.112.189.172 is the IP address in Japan.)

<<code1>>

import requests import os

def lambda_handler(event, context): response = requests.get('https://geolite.info/geoip/v2.1/country/3.112.189.172?pretty', auth=('account_id', 'license_key')) return response.json()

<<This is the result when I run code1.>>

{ "continent": { "code": "AS", "geoname_id": 6255147, "names": { "zh-CN": "亚洲", "de": "Asien", "en": "Asia", "es": "Asia", "fr": "Asie", "ja": "アジア", "pt-BR": "Ásia", "ru": "Азия" } }, "country": { "iso_code": "JP", "geoname_id": 1861060, "names": { "ja": "日本", "pt-BR": "Japão", "ru": "Япония", "zh-CN": "日本", "de": "Japan", "en": "Japan", "es": "Japón", "fr": "Japon" } }, "registered_country": { "iso_code": "US", "geoname_id": 6252001, "names": { "ru": "США", "zh-CN": "美国", "de": "Vereinigte Staaten", "en": "United States", "es": "Estados Unidos", "fr": "États Unis", "ja": "アメリカ", "pt-BR": "EUA" } }, "traits": { "ip_address": "3.112.189.172", "network": "3.112.0.0/14" } }

If I use variable 'url' to the argument of 'requests.get',

<<code2>>

import requests import os

def lambda_handler(event, context): url = "'https://geolite.info/geoip/v2.1/country/3.112.189.172?pretty', auth=('account_id', 'license_key')" response = requests.get(url)

<<result of code2 (erroe) >>

"errorMessage": "No connection adapters were found for "'https://geolite.info/geoip/v2.1/country/3.112.189.172?pretty', auth=('account_id', 'license_key')"", "errorType": "InvalidSchema",

<<code3: This works well>>

url = "https://www.yahoo.com" response = requests.get(url)

Why doesn't code2 work??

Thanks,

gefragt vor 2 Jahren242 Aufrufe
2 Antworten
0
Akzeptierte Antwort

I solved this problem.

I found that this API used Basic authentication. Username is 'account_id', password is 'license_key'. So I use 'requests.auth.HTTPBasicAuth'.

This is the code.

url1 = "https://geolite.info/geoip/v2.1/country/"
ip = os.environ['ip']
url2 ="?pretty"
url3 = url1 + ip +url2
account_id = os.environ['account_id']
license_key = os.environ['license_key']

r = requests.get(url3, auth=requests.auth.HTTPBasicAuth(account_id, license_key))

country = r.json()["country"]["names"]["ja"]
return(country)
beantwortet vor 2 Jahren
0

re:Post has messed up your formatting so it's a bit hard to understand, but from what I can see your "code1" is calling requests.get() like this:

requests.get(String, auth=('account_id', 'license_key'))

whereas your "code2" is calling it like this:

request.get(String)

So they are quite different.

EXPERTE
beantwortet vor 2 Jahren
  • I'm sorry that the format is broken and it's hard to understand.

    <<code1>> response = requests.get('https://geolite.info/geoip/v2.1/country/3.112.189.172?pretty', auth=('account_id', 'license_key')) return response.json()

    
    <<code2>>
        url = "'https://geolite.info/geoip/v2.1/country/3.112.189.172?pretty', auth=('account_id', 'license_key')"
        response = requests.get(url)
        return response.json()
    
    
    

    Thanks,

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen