WAKU-TAKE-A BLOG
メニューを開く
ブログ
GitHub
ツイッター
プログラム
プロフィール
Home
Pyhon
Bsic認証 Pyhon
Python3でBasic認証
Python3でBasic認証の設定されたサイトにアクセスします。
まずは、「python3 basic認証」でGoogle検索をしました。上から順番に読んでいき、とりあえず2パターンの方法を試すことができました。 ## ヘッダーを作成するパターン ["[Python] 3系でBasic認証付きのHTTPリクエストを発行する(urllib.request利用)"](https://www.yoheim.net/blog.php?q=20181003) を参考にしました。 サンプルコードは以下。うまくいけば、「status = 200」と表示されます。 ```python import urllib.request as rq import base64 url = "http://leggiero.sakura.ne.jp/xxxxbasic_auth_testxxxx/secret/kaiin_page_top.htm" user = "kaiin" passwd = "naisho" user_pass = base64.b64encode("{0}:{1}".format(user, passwd).encode("utf-8")) headers = {"Authorization" : "Basic " + user_pass.decode("utf-8")} req = rq.Request(url=url, headers=headers) #req = rq.Request(url=url) # without password res = rq.urlopen(req) print("status = {0}".format(res.status)) ``` ## パスワードマネージャーを利用するパターン [urllib による HTTP リクエスト (3) Basic 認証](https://maku77.github.io/python/http-request-with-basic-auth.html) を参考にしました。 HTTPPasswordMgrWithDefaultRealm()を使います。add_password メソッドの realm 引数は、 Noneです。 サンプルコードは以下。 ``` python import urllib.request as rq def set_basic_auth(uri, user, passwd): pass_mgr = rq.HTTPPasswordMgrWithDefaultRealm() pass_mgr.add_password(realm=None, uri=uri, user=user, passwd=passwd) auth_handler = rq.HTTPBasicAuthHandler(pass_mgr) opener = rq.build_opener(auth_handler) rq.install_opener(opener) uri = "http://leggiero.sakura.ne.jp/xxxxbasic_auth_testxxxx/secret/kaiin_page_top.htm" user = "kaiin" passwd = "naisho" set_basic_auth(uri, user, passwd) res = rq.urlopen(uri) print("status = {0}".format(res.status)) ``` ## ちなみに 認証を確認するためのサイトとして、[BASIC認証テスト](http://leggiero.sakura.ne.jp/xxxxbasic_auth_testxxxx/) を利用させていただきました。 あと、 ["urllib パッケージを使ってインターネット上のリソースを取得するには"](https://docs.python.org/ja/3/howto/urllib2.html) も読むと理解が進みます。
この記事をシェアする
Twitter
Facebook
Google+
B!
はてブ
Pocket
0 件のコメント :
コメントを投稿
Previous
Next
自己紹介
WAKU-TAKE-A
詳細プロフィールを表示
人気記事
サクラエディターでGrep置換
Visual Studioのソリューションファイルのダウングレード
Zenfone2 Laser ZE500KLをRoot化してみます
Markdown PDFのすすめ
Markdown TOCのすすめ
Python3でBasic認証
Python3でDigest認証
IronPythonのスクリプトをVisual Studio Codeで実行する方法(旧)
LivetのVisual Studio 2017用プロジェクトテンプレートなどについて
IronPythonからOpenCVを利用する
カテゴリー
blogger
( 5 )
Bsic認証
( 1 )
Digest認証
( 1 )
dll化
( 1 )
Git
( 3 )
GitBucket
( 3 )
Google Form
( 1 )
IronPython
( 14 )
Ldap
( 3 )
Livet
( 2 )
Markdown
( 4 )
Markdown PDF
( 1 )
Markdown TOC
( 1 )
MVVM
( 4 )
OpenCV
( 5 )
OpenCvSharp
( 4 )
OpenLdap
( 3 )
PDF
( 1 )
pip
( 1 )
Pyhon
( 3 )
R.NET
( 1 )
ROOT
( 1 )
R言語
( 1 )
TWRP
( 1 )
Vaster2
( 2 )
Visual Studio
( 1 )
Visual Studio Code
( 4 )
Windows
( 5 )
WPF
( 3 )
XAML
( 3 )
ZE500KL
( 1 )
Zenfone2 laser
( 1 )
ZXing
( 1 )
ZXing. Net
( 1 )
サクラエディタ
( 1 )
ショッピング
( 2 )
データバインディング
( 3 )
テンプレート
( 3 )
リモートデスクトップ接続
( 5 )
在宅勤務
( 5 )
注意点
( 1 )
統計解析R
( 1 )
特別定額給付金
( 1 )
ブログアーカイブ
10月 2023
( 2 )
5月 2020
( 3 )
4月 2020
( 4 )
7月 2019
( 1 )
5月 2019
( 5 )
2月 2019
( 1 )
12月 2018
( 2 )
11月 2018
( 4 )
10月 2018
( 3 )
8月 2018
( 5 )
7月 2018
( 3 )
6月 2018
( 7 )
2月 2018
( 2 )
10月 2016
( 1 )
0 件のコメント :
コメントを投稿