【以下的问题经过翻译处理】 你好,
我需要通过GeoLite2 Country网络服务从IP地址获取国家信息。
需要使用以下'curl'命令。curl -u "{account_id}:{license_key}" "https://geolite.info/geoip/v2.1/country/{ip_address}?pretty"
*参考URL https://dev.maxmind.com/geoip/geolocate-an-ip/web-services?lang=en#command-line-curl-examples
但是Python没有'curl'命令,所以我需要使用'requests.get'命令。我测试了以下代码。(3.112.189.172是日本的IP地址。)
<<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()
<<运行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"
}
}
如果我将变量'url'用作'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)
<<code2的结果(错误)>>
"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:这个可以正常工作>>
url = "https://www.yahoo.com"
response = requests.get(url)
为什么code2不起作用?
谢谢,