Đôi khi bạn sẽ cần biết vị trí của một địa chỉ IP, cho dù đó là của riêng bạn hay của một trang web bạn đang sử dụng. Một trường hợp sử dụng cho điều này là khi bạn muốn gửi thông tin đăng nhập cho người dùng cho trang web của bạn. Trong bài viết này, chúng tôi sẽ xem làm thế nào bạn có thể tìm thấy vị trí của một địa chỉ IP bằng Python. Để thực hiện mục tiêu này, chúng tôi sẽ sử dụng hai API được đề cập dưới đây: Để tương tác với các API này, chúng tôi sẽ sử dụng thư viện requests
trong Python. Nếu bạn chưa quen với API, hãy đảm bảo bạn xem hướng dẫn này để tìm hiểu về chúng.
Bạn có thể cài đặt thư viện này bằng lệnh pip
như thế này:
$ pip install requests
Khi thư viện được cài đặt, chúng tôi tốt để đi!
Như chúng tôi đã thảo luận, trước tiên chúng tôi sẽ tìm nạp địa chỉ IP của chúng tôi từ API đầu tiên. Sau đó, chúng tôi sẽ sử dụng địa chỉ IP này để tìm nạp thông tin vị trí cho địa chỉ IP cụ thể này. Vì vậy, chúng tôi sẽ có hai chức năng:
import requests
def get_ip[]:
response = requests.get['//api64.ipify.org?format=json'].json[]
return response["ip"]
def get_location[]:
ip_address = get_ip[]
response = requests.get[f'//ipapi.co/{ip_address}/json/'].json[]
location_data = {
"ip": ip_address,
"city": response.get["city"],
"region": response.get["region"],
"country": response.get["country_name"]
}
return location_data
print[get_location[]]
Trong mã trên, chúng tôi có hai chức năng - get_ip[]
và get_location[]
. Hãy thảo luận riêng biệt từng người trong số họ.
Chức năng get_ip[]
Theo tài liệu API của IPify, chúng tôi cần đưa ra yêu cầu nhận trên
import requests
def get_ip[]:
response = requests.get['//api64.ipify.org?format=json'].json[]
return response["ip"]
def get_location[]:
ip_address = get_ip[]
response = requests.get[f'//ipapi.co/{ip_address}/json/'].json[]
location_data = {
"ip": ip_address,
"city": response.get["city"],
"region": response.get["region"],
"country": response.get["country_name"]
}
return location_data
print[get_location[]]
0 để nhận phản hồi JSON trông như thế này:GET request on import requests
def get_ip[]:
response = requests.get['//api64.ipify.org?format=json'].json[]
return response["ip"]
def get_location[]:
ip_address = get_ip[]
response = requests.get[f'//ipapi.co/{ip_address}/json/'].json[]
location_data = {
"ip": ip_address,
"city": response.get["city"],
"region": response.get["region"],
"country": response.get["country_name"]
}
return location_data
print[get_location[]]
0 to get a JSON response that looks like this:{
"ip": "117.214.109.137"
}
Chúng tôi lưu trữ phản hồi này trong một biến
import requests
def get_ip[]:
response = requests.get['//api64.ipify.org?format=json'].json[]
return response["ip"]
def get_location[]:
ip_address = get_ip[]
response = requests.get[f'//ipapi.co/{ip_address}/json/'].json[]
location_data = {
"ip": ip_address,
"city": response.get["city"],
"region": response.get["region"],
"country": response.get["country_name"]
}
return location_data
print[get_location[]]
1 không có gì ngoài một loại từ điển Python với một cặp giá trị khóa. Vì vậy, chúng tôi đã trả lại giá trị của khóa import requests
def get_ip[]:
response = requests.get['//api64.ipify.org?format=json'].json[]
return response["ip"]
def get_location[]:
ip_address = get_ip[]
response = requests.get[f'//ipapi.co/{ip_address}/json/'].json[]
location_data = {
"ip": ip_address,
"city": response.get["city"],
"region": response.get["region"],
"country": response.get["country_name"]
}
return location_data
print[get_location[]]
2 là import requests
def get_ip[]:
response = requests.get['//api64.ipify.org?format=json'].json[]
return response["ip"]
def get_location[]:
ip_address = get_ip[]
response = requests.get[f'//ipapi.co/{ip_address}/json/'].json[]
location_data = {
"ip": ip_address,
"city": response.get["city"],
"region": response.get["region"],
"country": response.get["country_name"]
}
return location_data
print[get_location[]]
3.import requests
def get_ip[]:
response = requests.get['//api64.ipify.org?format=json'].json[]
return response["ip"]
def get_location[]:
ip_address = get_ip[]
response = requests.get[f'//ipapi.co/{ip_address}/json/'].json[]
location_data = {
"ip": ip_address,
"city": response.get["city"],
"region": response.get["region"],
"country": response.get["country_name"]
}
return location_data
print[get_location[]]
1 variable which is nothing but a sort of Python
dictionary with one key-value pair. So we returned the value of the key import requests
def get_ip[]:
response = requests.get['//api64.ipify.org?format=json'].json[]
return response["ip"]
def get_location[]:
ip_address = get_ip[]
response = requests.get[f'//ipapi.co/{ip_address}/json/'].json[]
location_data = {
"ip": ip_address,
"city": response.get["city"],
"region": response.get["region"],
"country": response.get["country_name"]
}
return location_data
print[get_location[]]
2 as import requests
def get_ip[]:
response = requests.get['//api64.ipify.org?format=json'].json[]
return response["ip"]
def get_location[]:
ip_address = get_ip[]
response = requests.get[f'//ipapi.co/{ip_address}/json/'].json[]
location_data = {
"ip": ip_address,
"city": response.get["city"],
"region": response.get["region"],
"country": response.get["country_name"]
}
return location_data
print[get_location[]]
3.Chức năng import requests
def get_ip[]:
response = requests.get['//api64.ipify.org?format=json'].json[]
return response["ip"]
def get_location[]:
ip_address = get_ip[]
response = requests.get[f'//ipapi.co/{ip_address}/json/'].json[]
location_data = {
"ip": ip_address,
"city": response.get["city"],
"region": response.get["region"],
"country": response.get["country_name"]
}
return location_data
print[get_location[]]
4
import requests
def get_ip[]:
response = requests.get['//api64.ipify.org?format=json'].json[]
return response["ip"]
def get_location[]:
ip_address = get_ip[]
response = requests.get[f'//ipapi.co/{ip_address}/json/'].json[]
location_data = {
"ip": ip_address,
"city": response.get["city"],
"region": response.get["region"],
"country": response.get["country_name"]
}
return location_data
print[get_location[]]
Theo tài liệu API của IPAPI, chúng tôi cần đưa ra yêu cầu nhận trên
import requests
def get_ip[]:
response = requests.get['//api64.ipify.org?format=json'].json[]
return response["ip"]
def get_location[]:
ip_address = get_ip[]
response = requests.get[f'//ipapi.co/{ip_address}/json/'].json[]
location_data = {
"ip": ip_address,
"city": response.get["city"],
"region": response.get["region"],
"country": response.get["country_name"]
}
return location_data
print[get_location[]]
5 để nhận thông tin vị trí cho một địa chỉ IP cụ thể. import requests
def get_ip[]:
response = requests.get['//api64.ipify.org?format=json'].json[]
return response["ip"]
def get_location[]:
ip_address = get_ip[]
response = requests.get[f'//ipapi.co/{ip_address}/json/'].json[]
location_data = {
"ip": ip_address,
"city": response.get["city"],
"region": response.get["region"],
"country": response.get["country_name"]
}
return location_data
print[get_location[]]
6 được thay thế bằng địa chỉ IP và import requests
def get_ip[]:
response = requests.get['//api64.ipify.org?format=json'].json[]
return response["ip"]
def get_location[]:
ip_address = get_ip[]
response = requests.get[f'//ipapi.co/{ip_address}/json/'].json[]
location_data = {
"ip": ip_address,
"city": response.get["city"],
"region": response.get["region"],
"country": response.get["country_name"]
}
return location_data
print[get_location[]]
7 có thể được thay thế bằng bất kỳ trong số này - import requests
def get_ip[]:
response = requests.get['//api64.ipify.org?format=json'].json[]
return response["ip"]
def get_location[]:
ip_address = get_ip[]
response = requests.get[f'//ipapi.co/{ip_address}/json/'].json[]
location_data = {
"ip": ip_address,
"city": response.get["city"],
"region": response.get["region"],
"country": response.get["country_name"]
}
return location_data
print[get_location[]]
8, import requests
def get_ip[]:
response = requests.get['//api64.ipify.org?format=json'].json[]
return response["ip"]
def get_location[]:
ip_address = get_ip[]
response = requests.get[f'//ipapi.co/{ip_address}/json/'].json[]
location_data = {
"ip": ip_address,
"city": response.get["city"],
"region": response.get["region"],
"country": response.get["country_name"]
}
return location_data
print[get_location[]]
9, {
"ip": "117.214.109.137"
}
0, {
"ip": "117.214.109.137"
}
1, {
"ip": "117.214.109.137"
}
2.GET request on import requests
def get_ip[]:
response = requests.get['//api64.ipify.org?format=json'].json[]
return response["ip"]
def get_location[]:
ip_address = get_ip[]
response = requests.get[f'//ipapi.co/{ip_address}/json/'].json[]
location_data = {
"ip": ip_address,
"city": response.get["city"],
"region": response.get["region"],
"country": response.get["country_name"]
}
return location_data
print[get_location[]]
5to get location information for a particular IP address. import requests
def get_ip[]:
response = requests.get['//api64.ipify.org?format=json'].json[]
return response["ip"]
def get_location[]:
ip_address = get_ip[]
response = requests.get[f'//ipapi.co/{ip_address}/json/'].json[]
location_data = {
"ip": ip_address,
"city": response.get["city"],
"region": response.get["region"],
"country": response.get["country_name"]
}
return location_data
print[get_location[]]
6 is replaced
by the IP address and import requests
def get_ip[]:
response = requests.get['//api64.ipify.org?format=json'].json[]
return response["ip"]
def get_location[]:
ip_address = get_ip[]
response = requests.get[f'//ipapi.co/{ip_address}/json/'].json[]
location_data = {
"ip": ip_address,
"city": response.get["city"],
"region": response.get["region"],
"country": response.get["country_name"]
}
return location_data
print[get_location[]]
7 can be replaced with any of these – import requests
def get_ip[]:
response = requests.get['//api64.ipify.org?format=json'].json[]
return response["ip"]
def get_location[]:
ip_address = get_ip[]
response = requests.get[f'//ipapi.co/{ip_address}/json/'].json[]
location_data = {
"ip": ip_address,
"city": response.get["city"],
"region": response.get["region"],
"country": response.get["country_name"]
}
return location_data
print[get_location[]]
8, import requests
def get_ip[]:
response = requests.get['//api64.ipify.org?format=json'].json[]
return response["ip"]
def get_location[]:
ip_address = get_ip[]
response = requests.get[f'//ipapi.co/{ip_address}/json/'].json[]
location_data = {
"ip": ip_address,
"city": response.get["city"],
"region": response.get["region"],
"country": response.get["country_name"]
}
return location_data
print[get_location[]]
9, {
"ip": "117.214.109.137"
}
0, {
"ip": "117.214.109.137"
}
1, {
"ip": "117.214.109.137"
}
2. Chức năng này gọi chức năng get_ip[]
để lấy địa chỉ IP và sau đó đưa ra yêu cầu nhận trên URL với địa chỉ IP. API này trả về phản hồi JSON trông như thế này:GET request on the URL with the IP address. This API returns a JSON response that looks like this:
{
"ip": "117.214.109.137",
"version": "IPv4",
"city": "Gaya",
"region": "Bihar",
"region_code": "BR",
"country": "IN",
"country_name": "India",
"country_code": "IN",
"country_code_iso3": "IND",
"country_capital": "New Delhi",
"country_tld": ".in",
"continent_code": "AS",
"in_eu": false,
"postal": "823002",
"latitude": 24.7935,
"longitude": 85.012,
"timezone": "Asia/Kolkata",
"utc_offset": "+0530",
"country_calling_code": "+91",
"currency": "INR",
"currency_name": "Rupee",
"languages": "en-IN,hi,bn,te,mr,ta,ur,gu,kn,ml,or,pa,as,bh,sat,ks,ne,sd,kok,doi,mni,sit,sa,fr,lus,inc",
"country_area": 3287590,
"country_population": 1352617328,
"asn": "AS9829",
"org": "National Internet Backbone"
}
Chúng tôi nhận được rất nhiều dữ liệu trong phản hồi. Bạn có thể sử dụng bất cứ điều gì hoạt động cho bạn. Đối với hướng dẫn này, chúng tôi sẽ chỉ sử dụng & nbsp; ____ ____ 24,
{
"ip": "117.214.109.137"
}
5 và {
"ip": "117.214.109.137"
}
6. Đó là lý do tại sao chúng tôi đã tạo ra một từ điển gọi là {
"ip": "117.214.109.137"
}
7 và lưu trữ tất cả dữ liệu bên trong nó và trả về như nhau.{
"ip": "117.214.109.137"
}
6. That's why we created a dictionary called {
"ip": "117.214.109.137"
}
7 and stored all the data inside it and returned the same.Cuối cùng, chúng tôi gọi hàm get_location[]
và in đầu ra. Đầu ra của chúng tôi sẽ trông như thế này:
{
"ip": "117.214.109.137",
"city": "Gaya",
"region": "Bihar",
"country": "India"
}
Sự kết luận
Trong bài viết này, chúng tôi đã học cách chúng tôi có thể tương tác với các dịch vụ web để lấy thông tin vị trí cho một địa chỉ IP cụ thể.
Cảm ơn vì đã đọc! Để biết thêm các bài viết như vậy, hãy kiểm tra blog của tôi, iead.
Học mã miễn phí. Chương trình giảng dạy nguồn mở của Freecodecamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu