欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用python來(lái)跟蹤ip地址的方法

 更新時(shí)間:2023年06月21日 10:48:43   作者:程序員學(xué)長(zhǎng)  
今天來(lái)介紹一個(gè)流行的 python庫(kù) ip2geotools,使用它可以確定 IP地址 對(duì)應(yīng)的 國(guó)家、地區(qū)、城市、緯度和經(jīng)度等,文中通過(guò)代碼示例介紹了如何使用python來(lái)跟蹤ip地址,需要的朋友可以參考下

根據(jù) IP 地址獲取位置

導(dǎo)入必要的庫(kù)

import socket
import requests
from ip2geotools.databases.noncommercial import DbIpCity
from geopy.distance import distance

下面的函數(shù)用于打印IP地址、城市、國(guó)家、坐標(biāo)等的詳細(xì)信息。

def printDetails(ip):
    res = DbIpCity.get(ip, api_key="free")
    print(f"IP Address: {res.ip_address}")
    print(f"Location: {res.city}, {res.region}, {res.country}")
    print(f"Coordinates: (Lat: {res.latitude}, Lng: {res.longitude})")

從IP地址獲取位置

ip_add = input("Enter IP: ")  # 198.35.26.96
printDetails(ip_add)

輸出:

IP Address: 198.35.26.96
Location: San Jose, California, US
Coordinates: (Lat: 37.3361663, Lng: -121.890591)

從 URL 獲取位置

url = input("Enter URL: ")  # www.youtube.com
ip_add = socket.gethostbyname(url)
printDetails(ip_add)

輸出:

IP Address: 142.250.66.142
Location: Hong Kong, Central and Western, HK
Coordinates: (Lat: 22.2850394, Lng: 114.1583819)

一些常見(jiàn)的用例

在這里,我們討論它的一些用例,

例如阻止某些特定國(guó)家/地區(qū)的 IP 地址或計(jì)算兩個(gè) IP 地址之間的距離等。

1.根據(jù)位置阻止某些 IP 地址

下面的代碼查找 IP 地址的位置,然后檢查該位置的國(guó)家/地區(qū)是否在被阻止國(guó)家/地區(qū)列表中。

def is_country_blocked(ip_address):
    blocked_countries = ["China", "Canada", "India"]
    location = DbIpCity.get(ip_address)
    if location.country in blocked_countries:
        return True
    else:
        return False
ip_add = input("Enter IP: ")  # 198.35.26.96
if is_country_blocked(ip_add) is True:
    print(f"IP Address: {ip_add} is blocked")
else:
    print(f"IP Address: {ip_add} is allowed")

輸出:

Enter the IP Address: 198.35.26.96
IP Address: 198.35.26.96 is allowed

2.計(jì)算兩個(gè)IP地址之間的距離

下面的代碼將計(jì)算兩個(gè) IP 地址位置之間的距離(以公里為單位)。

def calculate_distance(ip1, ip2):
    res1 = DbIpCity.get(ip1)
    res2 = DbIpCity.get(ip2)
    lat1, lon1 = res1.latitude, res1.longitude
    lat2, lon2 = res2.latitude, res2.longitude
    return distance((lat1, lon1), (lat2, lon2)).km
# Input two IP addresses
ip_add_1 = input("1st IP: ")  # 198.35.26.96
ip_add_2 = input("2nd IP: ")  # 220.158.144.59
dist = calculate_distance(ip_add_1, ip_add_2)
print(f"Distance between them is {str(dist)}km")

輸出:

Enter 1st IP Address: 198.35.26.96
Enter 2nd IP Address: 220.158.144.59
Distance between them is 12790.62320788363km

3.計(jì)算你當(dāng)前位置和服務(wù)器之間的距離

下面的代碼將計(jì)算你當(dāng)前位置與給定 IP 地址位置之間的距離(以公里為單位)。

def get_distance_from_location(ip, lat, lon):
    res = DbIpCity.get(ip)
    ip_lat, ip_lon = res.latitude, res.longitude
    return distance((ip_lat, ip_lon), (lat, lon)).km
server_ip = input("Server's IP: ")
lat = float(input("Your Latitude: "))
lng = float(input("Your Longitude: "))
dist = get_distance_from_location(server_ip, lat, lng)
print(f"Distance between the server and your location is {str(dist)}km")

輸出:

Enter your server's IP Address: 208.80.152.201
Enter your current location (Latitude): 26.4710
Enter your current location (Longitude): 73.1134
Distance between the server and your location is 12183.275099919923km

結(jié)論

跟蹤 IP 地址的位置有幾個(gè)好處,

例如企業(yè)可以根據(jù)用戶的位置向用戶投放有針對(duì)性的廣告。這可以帶來(lái)更有效的營(yíng)銷活動(dòng)、更高的轉(zhuǎn)化率和個(gè)性化的用戶體驗(yàn)。

此外,它還有助于欺詐檢測(cè),例如阻止來(lái)自某些特定國(guó)家/地區(qū)的 IP 地址,還可以驗(yàn)證 IP 地址以確保它們的格式正確。

到此這篇關(guān)于利用python來(lái)跟蹤ip地址的方法的文章就介紹到這了,更多相關(guān)python 跟蹤ip地址內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python?數(shù)據(jù)庫(kù)操作SQL基礎(chǔ)

    Python?數(shù)據(jù)庫(kù)操作SQL基礎(chǔ)

    在本章節(jié)中,我們將討論?Python?數(shù)據(jù)庫(kù)操作的基礎(chǔ)知識(shí),重點(diǎn)關(guān)注?SQL即Structured?Query?Language,結(jié)構(gòu)化查詢語(yǔ)言,SQL?是用于管理關(guān)系型數(shù)據(jù)庫(kù)的標(biāo)準(zhǔn)編程語(yǔ)言,可以用來(lái)執(zhí)行數(shù)據(jù)定義、數(shù)據(jù)操作和數(shù)據(jù)控制等任務(wù)
    2023-06-06
  • E: 無(wú)法定位軟件包 python3-pip問(wèn)題及解決

    E: 無(wú)法定位軟件包 python3-pip問(wèn)題及解決

    這篇文章主要介紹了E: 無(wú)法定位軟件包 python3-pip問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 編寫Python爬蟲抓取暴走漫畫上gif圖片的實(shí)例分享

    編寫Python爬蟲抓取暴走漫畫上gif圖片的實(shí)例分享

    這篇文章主要介紹了編寫Python爬蟲抓取暴走漫畫上gif圖片的實(shí)例分享,示例代碼為Python3,利用到了urllib模塊、request模塊和BeautifulSoup模塊,需要的朋友可以參考下
    2016-04-04
  • Python實(shí)現(xiàn)將一段文字復(fù)制到所選的文件當(dāng)中

    Python實(shí)現(xiàn)將一段文字復(fù)制到所選的文件當(dāng)中

    這篇文章主要為大家詳細(xì)介紹了Python如何將一段文字復(fù)制到所選的文件當(dāng)中,文中的示例代碼講解詳細(xì), 感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-02-02
  • 對(duì)Pytorch中Tensor的各種池化操作解析

    對(duì)Pytorch中Tensor的各種池化操作解析

    今天小編就為大家一篇對(duì)Pytorch中Tensor的各種池化操作解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-01-01
  • 使用Python打造一款間諜程序的流程分析

    使用Python打造一款間諜程序的流程分析

    這篇文章主要介紹了使用Python打造一款間諜程序,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-02-02
  • python+numpy+matplotalib實(shí)現(xiàn)梯度下降法

    python+numpy+matplotalib實(shí)現(xiàn)梯度下降法

    這篇文章主要為大家詳細(xì)介紹了python+numpy+matplotalib實(shí)現(xiàn)梯度下降法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • 如何用python編寫一個(gè)生成春聯(lián)軟件

    如何用python編寫一個(gè)生成春聯(lián)軟件

    大家好,本篇文章主要講的是如何用python編寫一個(gè)生成春聯(lián)軟件,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01
  • python 爬取小說(shuō)并下載的示例

    python 爬取小說(shuō)并下載的示例

    這篇文章主要介紹了python 爬取小說(shuō)并下載的示例,幫助大家更好的理解和學(xué)習(xí)python爬蟲,感興趣的朋友可以了解下
    2020-12-12
  • 使用python-pptx創(chuàng)建PPT演示文檔功能實(shí)踐

    使用python-pptx創(chuàng)建PPT演示文檔功能實(shí)踐

    這篇文章主要介紹了使用python-pptx創(chuàng)建PPT演示文檔功能實(shí)踐,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06

最新評(píng)論