A secure HTTP request library with URL filtering
Project description
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に正規表現パターンを記述できます
使い方
- プロジェクトディレクトリに
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+
- 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: リクエスト先のURLmethod: 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へのリクエスト試行時には警告が表示されます
- リクエストが失敗した場合も警告が表示されます
- 正規表現が無効な場合は、正確な文字列マッチとして扱われます
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file unai-1.0.0.tar.gz.
File metadata
- Download URL: unai-1.0.0.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a58c3543a2bc42e44d0f4a051d2bc5eb221f981ed927a6980ec404647c5486d0
|
|
| MD5 |
42c41250813d7f75eff926a29bb78655
|
|
| BLAKE2b-256 |
37781e6cae9ff47c9491ca96f9d5f01b74684e0821dcbc6987af81a9be587791
|
File details
Details for the file unai-1.0.0-py3-none-any.whl.
File metadata
- Download URL: unai-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0dc374634312c686a842fb165c0085d1a82b2b17d2eadd5151567db4d79c7434
|
|
| MD5 |
8e0e9482394127995a3858c5e7019506
|
|
| BLAKE2b-256 |
6f9e206748f548f7eb79138860803416054b34cd845005bb20e986af3621cf8d
|