LoginSignup
3

More than 5 years have passed since last update.

PythonistaでWhois Search

Posted at

はじめに

iOS上でPythonを実行できるとんでもないApp「Pythonista 3」を利用して,ブラウザで閲覧中のページのWhois情報を確認できるようにしてみました。

実行環境

  • iPad Air 2 (iOS 11.4.1)
  • Pythonista 3 version 3.2
  • Google Chrome version 69.0.3497.105

ライブラリ

whoisは標準ではインストールされていないと思うので,追加してください。
StaShが使えるのであれば,pip install whoisでOKです。
StaShが使えないなら,こんなくだらないことをしていないで使えるようにしましょう。

ソースコード

whois.py
import appex
import socket
from urllib.parse import urlparse
from whois import whois


def pad(value, length):
    return value + ' ' * (length-len(value)) + ': '


def print_data(title, value):
    if value is None:
        value = 'N/A'
    print(f'{title}{value}')


def main():
    url = appex.get_url()
    host = urlparse(url).netloc
    addr = socket.gethostbyname(host)
    print(f'{host} ({addr})')
    try:
        info = whois(host)
        max_len = max([len(k) for k in info])
        for k, v in info.items():
            if type(v) is list:
                padding = ' ' * (max(len(k), max_len)+2)
                print_data(pad(k, max_len), v[0])
                for i in range(1, len(v)):
                    print_data(padding, v[i])
            else:
                print_data(pad(k, max_len), v)
    except:
        print('Failed to get information')

if __name__ == "__main__":
    if appex.is_running_extension():
        main()

[共有]に追加

  1. [Settings] > [Share Extension Shortcuts]を開く
  2. +マークをタップする
  3. ファイルを選択し,必要に応じてタイトルやアイコンを編集する
  4. Done!

実際に使ってみる

ブラウザの[共有]メニューから[Run Pythonista Script]を選択し,登録したショートカットをタップします。

画像では途中で切れていますがこんな感じの情報を確認できます。
実際にこの機能が役に立つのかは不明ですが……

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
3