7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pythonでアクセス元IPのwhois情報を取得する

Last updated at Posted at 2019-05-22

#概要
サーバ側でアクセス元のIPアドレスを取得して、そのIPアドレスのwhois情報を取得する方法。
Flaskフレームワークでの例を紹介します。

#アクセス元IPアドレスを取得
request.remote_addrでリクエストの送信元IPアドレスを取得できます。

from flask import request
from flask import jsonify

@app.route("/get_ip", methods=["GET"])
def get_ip():
    return jsonify({'ip': request.remote_addr}), 200

#ipwhoisのインストール

pip3 install ipwhois

私の環境ではdnspython-1.16.0とipwhois-1.1.0がインストールされました。
#アクセス元IPアドレスのwhois情報を取得

from flask import request
from flask import jsonify
from ipwhois import IPWhois

@app.route("/get_whois", methods=["GET"])
def get_whois():
    obj = IPWhois(request.remote_addr)
    whoisInfo = obj.lookup_whois()
    return jsonify({'whois': whoisInfo}), 200

#参考文献
クイックスタート — requests-docs-ja 1.0.4 documentation
ipwhois · PyPI
・[Python Flaskを使って訪問者のIPアドレスを取得する]
(https://codeday.me/jp/qa/20181202/26342.html)

7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?