Skip to main content

Unai Library

Unaiは、特定のURLのみへのHTTPリクエストを制限するPythonライブラリです。

インストール

pip install unai

機能

  • import unaiを実行すると、呼び出し元のPythonファイルと同じディレクトリにあるrequest_urls.txtを読み込みます
  • unai.request()メソッドは、引数で受け取ったURLがrequest_urls.txtに含まれる場合のみリクエストを送信します
  • GET、POST、PUT、DELETEなどのHTTPメソッドをサポートしています
  • 正規表現をサポート - request_urls.txtに正規表現パターンを記述できます

使い方

  1. プロジェクトディレクトリにrequest_urls.txtを作成し、許可するURLまたは正規表現パターンを1行に1つずつ記述します:
# 正確なURLマッチ
https://api.example.com/data

# 正規表現パターン(ワイルドカード)
https://httpbin\.org/.*
https://api\.github\.com/users/.*

# 複雑な正規表現
https://.*\.wikipedia\.org/api/.*
https://jsonplaceholder\.typicode\.com/posts/\d+
  1. Pythonコードでライブラリをインポートして使用します:
import unai

# GETリクエスト
response = unai.get("https://httpbin.org/get")
if response:
    print(response.json())

# POSTリクエスト
response = unai.post("https://httpbin.org/post", json={"key": "value"})
if response:
    print(response.json())

# 一般的なリクエスト
response = unai.request("https://httpbin.org/put", method="PUT", json={"data": "test"})
if response:
    print(response.json())

# request_urls.txtにないURLや一致しないパターンはブロックされます
response = unai.get("https://unauthorized-site.com")
# responseはNoneになります

正規表現のサポート

request_urls.txtには以下の2種類のパターンを記述できます:

1. 正確なURLマッチ

https://api.example.com/endpoint

2. 正規表現パターン

# httpbin.orgのすべてのパスを許可
https://httpbin\.org/.*

# GitHub APIのユーザーエンドポイントを許可
https://api\.github\.com/users/.*

# 数字のIDを持つ投稿を許可
https://jsonplaceholder\.typicode\.com/posts/\d+

# すべてのWikipedia APIエンドポイントを許可
https://.*\.wikipedia\.org/api/.*

注意: 正規表現ではドット(.)などの特殊文字はエスケープする必要があります(例:httpbin\.org

API

unai.request(url, method='GET', **kwargs)

指定されたURLにHTTPリクエストを送信します。URLがrequest_urls.txtのパターンに一致する場合のみ実行されます。

  • url: リクエスト先のURL
  • method: HTTPメソッド(GET、POST、PUT、DELETEなど)
  • **kwargs: requests.requestに渡される追加の引数

unai.get(url, **kwargs)

GETリクエストを送信します。

unai.post(url, **kwargs)

POSTリクエストを送信します。

unai.put(url, **kwargs)

PUTリクエストを送信します。

unai.delete(url, **kwargs)

DELETEリクエストを送信します。

セキュリティ

このライブラリは、意図しないURLへのリクエストを防ぐために設計されています。request_urls.txtに明示的に記載されたURLや正規表現パターンのみにアクセスを制限することで、セキュリティを強化します。

注意事項

  • request_urls.txtが見つからない場合、警告が表示され、すべてのリクエストがブロックされます
  • 許可されていないURLへのリクエスト試行時には警告が表示されます
  • リクエストが失敗した場合も警告が表示されます
  • 正規表現が無効な場合は、正確な文字列マッチとして扱われます

Release files for unai 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for unai 1.0.0
File Size Uploaded
unai-1.0.0.tar.gz 5.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for unai 1.0.0
File Interpreter ABI Platform
unai-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.5 kB

Release files / unai-1.0.0.tar.gz

Download URL unai-1.0.0.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a58c3543a2bc42e44d0f4a051d2bc5eb221f981ed927a6980ec404647c5486d0
BLAKE2b-256 checksum
How to use checksums
37781e6cae9ff47c9491ca96f9d5f01b74684e0821dcbc6987af81a9be587791
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.6

Release files / unai-1.0.0-py3-none-any.whl

Download URL unai-1.0.0-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0dc374634312c686a842fb165c0085d1a82b2b17d2eadd5151567db4d79c7434
BLAKE2b-256 checksum
How to use checksums
6f9e206748f548f7eb79138860803416054b34cd845005bb20e986af3621cf8d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.6

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page