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,

질문됨 2년 전230회 조회
2개 답변
0
수락된 답변

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)
답변함 2년 전
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.

전문가
답변함 2년 전
  • 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,

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠