Làm cách nào để bạn nhập địa chỉ IP trong python?

Đôi khi, bạn sẽ cần biết vị trí của một địa chỉ IP, cho dù đó là địa chỉ của riêng bạn hay của 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 mình

Trong bài viết này, chúng ta sẽ xem cách bạn có thể tìm vị trí của địa chỉ IP bằng Python

Chuẩn bị dụng cụ của bạn

Để hoàn thành mục tiêu này, chúng tôi sẽ sử dụng hai API được đề cập bên dưới

  1. ipify. API này sẽ giúp chúng tôi biết địa chỉ IP từ nơi yêu cầu đến
  2. ipapi. API này sẽ giúp chúng tôi lấy thông tin vị trí cho một địa chỉ IP cụ thể

Để tương tác với các API này, chúng ta sẽ sử dụng thư việ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[]]
2 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

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 như thế này

$ pip install requests

Sau khi thư viện được cài đặt, chúng ta sẽ bắt đầu

Nhận thông tin vị trí

Như chúng ta đã thảo luận, trước tiên chúng ta sẽ lấy địa chỉ IP của mình 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 ta 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 đoạn mã trên, chúng ta 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[]]
4 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[]]
5. Hãy thảo luận về từng người trong số họ một cách riêng biệt

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[]]
6

Theo tài liệu API của ipify, chúng tôi cần thực hiện yêu cầu GET 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 giống như thế này

{
  "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 gì khác ngoài một loại từ điển Python với một cặp khóa-giá trị. Vì vậy, chúng tôi đã trả về 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

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

Theo tài liệu API của ipapi, chúng tôi cần thực hiện yêu cầu GET 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ỳ địa chỉ nào sau đâ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

Hàm này gọi nội bộ hàm

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 để lấy địa chỉ IP và sau đó thực hiện yêu cầu GET trên URL có địa chỉ IP. API này trả về một phản hồi JSON giống như thế 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[]]
6

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ì làm việc cho bạn. Đối với hướng dẫn này, chúng ta sẽ chỉ sử dụng  

{
  "ip": "117.214.109.137"
}
4,
{
  "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 một từ điển có tên là
{
  "ip": "117.214.109.137"
}
7 và lưu trữ tất cả dữ liệu bên trong nó và trả về cùng một

Cuối cùng, chúng tôi gọi hàm

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 và in đầu ra. Đầu ra của chúng tôi sẽ trông như thế 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[]]
1Kết luận

Trong bài viết này, chúng tôi đã tìm hiểu cách chúng tôi có thể tương tác với các dịch vụ web để nhận 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 xem blog của tôi, iRead

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

Ashutosh Krishna

Nhà phát triển ứng dụng tại Thoughtworks Ấn Độ

Nếu bạn đọc đến đây, hãy tweet cho tác giả để cho họ thấy bạn quan tâm. Tweet một lời cảm ơn

Học cách viết 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ó việc làm với tư cách là nhà phát triển. Bắt đầu

Cú pháp cho một địa chỉ IP là gì?

Địa chỉ IP có hai phần. ID mạng, bao gồm ba số đầu tiên của địa chỉ và ID máy chủ, số thứ tư trong địa chỉ . Vì vậy, trên mạng gia đình của bạn — 192. 168. 1. 1, ví dụ – 192. 168. 1 là ID mạng và số cuối cùng là ID máy chủ.

Địa chỉ IP là kiểu dữ liệu gì?

Các loại dữ liệu địa chỉ mạng IP. IPV4 và IPV6 là các loại dữ liệu trừu tượng lưu trữ địa chỉ máy chủ IPv4 và IPv6 tương ứng ở định dạng nhị phân . IPV4 là địa chỉ máy chủ 4 byte theo ký hiệu thập phân có dấu chấm [bốn số thập phân, mỗi số nằm trong khoảng từ 0 đến 255, được phân tách bằng dấu chấm]. Ví dụ. 172. 16.

IP NUM trong Python là gì?

IP [Giao thức Internet] - Địa chỉ là khái niệm cơ bản cơ bản của mạng máy tính cung cấp khả năng gán địa chỉ cho mạng . Python cung cấp mô-đun ipaddress được sử dụng để xác thực và phân loại địa chỉ IP theo loại của chúng [IPv4 hoặc IPv6].

Chủ Đề