본문 바로가기
Programming/Python

[Python] 딕셔너리를 활용한 IP 변환

by goatlab 2022. 10. 19.
728x90
반응형
SMALL

딕셔너리를 활용한 IP 변환

 

https://ip-api.com/

 

도메인이나 IP를 입력하면 ip-api.com로 접속해서 정보를 정리해주는 프로그램을 구현 가능하다.

 

# 웹 요청을 수행하는 라이브러리 설치
!pip install requests
import requests # 웹 요청을 수행하는 라이브러리
import json     # json을 dict으로 변환하는 라이브러리

ip = input("도메인이나 IP를 입력 : ")
res = requests.get(f'http://ip-api.com/json/{ip}') # Get 요청 수행
geo_data = json.loads(res.text) # 바디 데이터를 json에서 dict으로 변경

# print(type(geo_data)) # 데이터 타입 확인

#geo_data # 데이터 확인
print(f'''
ip: {geo_data["query"]}
국가: {geo_data["country"]}
도시: {geo_data["city"]}
경도: {geo_data["lon"]}
위도: {geo_data["lat"]}
''')
ip: 223.130.195.95
국가: South Korea
도시: Seongnam-si
경도: 127.119
위도: 37.3827
728x90
반응형
LIST