3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Proxy経由でWatson Assistantに接続する方法(Python)

Last updated at Posted at 2019-01-17

はじめに

PythonでProxy経由でWatson Assistantに接続する方法について記載します。

Watson AssistantはIBM Cloudのサービスですが、セキュリティが厳しい環境ではプログラムから直接外部のクラウドサービスへ接続することが許されない場合があります。
そこで、Proxyサーバーを経由してアクセスする方法について記載します。

前提

Python 3.6.4 (諸事情により古いですがご了承ください)

Watson AssistantのSDKを使った場合

まずは、SDKを使ってWatson Assistantへアクセスするためにこんな感じで書きます。

test.py
# 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"
)

versioniam_apikeyurlは個別の環境に合わせて変えてくださいね。

次にProxy情報(IPとポート番号)を設定して

test.py
######### 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に設定する

test.py
assistant.set_http_config(http_config)

後は、通常通り assistant.get_workspace() などでワークスペースの情報を取得したりすればOK。

SDKを使わない場合

SDKを使わず直接POSTする場合はこんな感じ。

test.py
# 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))

versionrequrl等は個別の環境に合わせて変えてください。

以上です。

参考資料

お断り

このサイトの掲載内容は私自身の見解であり、必ずしも所属会社の立場、戦略、意見を代表するものではありません。 記事は執筆時点の情報を元に書いているため、必ずしも最新情報であるとはかぎりません。 記事の内容の正確性には責任を負いません。自己責任で実行してください。

3
3
0

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
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?