
GeoIP 데이터를 사용하여 국가 코드 3 자리 추출을 위해서는 다음 단계를 따르세요.
1. GeoIP 데이터베이스를 로드합니다.
- 예를 들어, `from geoip2.database import Reader`를 사용하여 데이터베이스를 로드할 수 있습니다.
2. 국가 이름을 국가 코드 3 자리로 변환하는 함수를 정의합니다.
- 예를 들어, `def geoip_country_code3_by_name(reader, country_name):` 함수를 정의할 수 있습니다.
3. 국가 이름을 국가 코드 3 자리로 변환합니다.
- 예를 들어, `country_code3 = reader.country(country_name).iso_code`를 사용하여 국가 코드 3 자리를 추출할 수 있습니다.
4. 국가 이름을 국가 코드 3 자리로 변환하는 함수를 테스트합니다.
- 예를 들어, `print(geoip_country_code3_by_name(reader, 'United States'))`를 사용하여 함수를 테스트할 수 있습니다.
위의 예를 토대로 geoip_country_code3_by_name 함수를 사용하여 국가 이름을 국가 코드 3 자리로 변환하는 방법은 다음과 같습니다.
#hostingforum.kr
python
from geoip2.database import Reader
def geoip_country_code3_by_name(reader, country_name):
country_code3 = reader.country(country_name).iso_code
return country_code3
# GeoIP 데이터베이스 로드
reader = Reader('GeoIP2-City.mmdb')
# 국가 이름을 국가 코드 3 자리로 변환
country_name = 'United States'
country_code3 = geoip_country_code3_by_name(reader, country_name)
print(country_code3) # USA
2025-03-13 11:48