Proxy経由でWatson Assistantに接続する方法(Python)
はじめに
PythonでProxy経由でWatson Assistantに接続する方法について記載します。
Watson AssistantはIBM Cloudのサービスですが、セキュリティが厳しい環境ではプログラムから直接外部のクラウドサービスへ接続することが許されない場合があります。
そこで、Proxyサーバーを経由してアクセスする方法について記載します。
前提
Python 3.6.4 (諸事情により古いですがご了承ください)
Watson AssistantのSDKを使った場合
まずは、SDKを使ってWatson Assistantへアクセスするためにこんな感じで書きます。
# Watson Developer Cloud SDK
from watson_developer_cloud import AssistantV1
from watson_developer_cloud import WatsonApiException
assistant = AssistantV1(
version = "2018-09-20",
iam_apikey = "*********",
url = "https://gateway.watsonplatform.net/assistant/api"
)
version
、iam_apikey
、url
は個別の環境に合わせて変えてくださいね。
次にProxy情報(IPとポート番号)を設定して
######### Proxy設定 #########
http_proxy="xxx.xxx.xxx.xx:yyyy"
https_proxy="xxx.xxx.xxx.xx:yyyy"
http_config={"proxies" :{
"http" : http_proxy,
"https" : https_proxy
}}
assisantに設定する
assistant.set_http_config(http_config)
後は、通常通り assistant.get_workspace()
などでワークスペースの情報を取得したりすればOK。
SDKを使わない場合
SDKを使わず直接POSTする場合はこんな感じ。
#Request設定
headers = {'Content-Type': 'application/json'}
params = (('version', "2018-09-20"))
requrl = "https://gateway.watsonplatform.net/assistant/api/v1/workspaces"
#Proxy設定
proxies = {
"http" : http_proxy,
"https" : https_proxy
}
#POST
requests.post(requrl, proxies=proxies, headers=headers, params=params, data=json_data, auth=(wa_usr, wa_pwd))
version
、requrl
等は個別の環境に合わせて変えてください。
以上です。
参考資料
- Watson Assistant API Reference
お断り
このサイトの掲載内容は私自身の見解であり、必ずしも所属会社の立場、戦略、意見を代表するものではありません。 記事は執筆時点の情報を元に書いているため、必ずしも最新情報であるとはかぎりません。 記事の内容の正確性には責任を負いません。自己責任で実行してください。
Author And Source
この問題について(Proxy経由でWatson Assistantに接続する方法(Python)), 我々は、より多くの情報をここで見つけました https://qiita.com/nax/items/2a3f327bb0fee297a8d3著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .