機械翻訳について

Python

urllib2などの手法を使用してWebサービスを呼び出し、HTTP要求やSudsクライアントを作成するには、Pythonを使用します。

urllib2ライブラリ

次のコードでは、urllib2ライブラリを使用してHTTP要求を作成します。
  1. サービスを呼び出すxmlペイロードを作成します。 このコード例は、ハードコードされた文字列を渡します。

  2. サービス・コールの資格証明のため、Base 64でエンコードされた文字列を作成します。

  3. オープナを作成して登録します。 呼出しがファイアウォールの内側のサーバーになっている場合は、プロキシを使用して処理します。

  4. サービスを呼び出す要求を作成します。

  5. 要求のコンテンツ・タイプがxmlになるように構成します。

  6. 認証情報を含む要求ヘッダーを構成します。

  7. 呼び出すSOAPActionを設定します。 サービス・コールはこの値がなくても行えますが、これが推奨される標準です。

  8. 要求にxmlペイロードを書き込み、要求を実行します。

  9. 応答を取得して処理します。 この例は、応答を単に出力します。

import urllib2, base64
username='username'
password='password'

# Construct xml payload to invoke the service. In the example, it is a hard coded string.
envelope = """<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
                             
                             xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                <soap:Body>
                 <findRule
                             xmlns="http://xmlns.oracle.com/apps/incentiveCompensation/cn/creditSetup/creditRule/creditRuleService/types/">
                    <findCriteria>
                        <fetchStart xmlns="http://xmlns.oracle.com/adf/svc/types/">0</fetchStart>
                        <fetchSize xmlns="http://xmlns.oracle.com/adf/svc/types/">-1</fetchSize>
                        <filter xmlns="http://xmlns.oracle.com/adf/svc/types/">
                            <group>
                               <upperCaseCompare>false</upperCaseCompare>
                               <item>
                                  <upperCaseCompare>false</upperCaseCompare>
                                  <attribute>RuleId</attribute>
                                  <operator>=</operator>
                                  <value>300000000851162</value>
                               </item>
                            </group>
                        </filter>
                        <excludeAttribute
                            xmlns="http://xmlns.oracle.com/adf/svc/types/">false</excludeAttribute>
                    </findCriteria>
                    <findControl>
                       <retrieveAllTranslations
                            xmlns="http://xmlns.oracle.com/adf/svc/types/">false</retrieveAllTranslations>
                    </findControl>
                 </findRule>
                </soap:Body>
            </soap:Envelope>"""
			
# Construct the base 64 encoded string used as credentials for the service call
creadentials = base64.encodestring('%s:%s' % (username, password))[:-1]
authorization = "Basic %s" % creadentials

# Create and register opener. Requires proxy when behind a firewall
opener = urllib2.build_opener(urllib2.HTTPHandler(), urllib2.HTTPSHandler(), urllib2.ProxyHandler({'https':'proxyhost:proxyport'}))
urllib2.install_opener(opener)

# Create request for the service call
request = urllib2.Request("https://host:port/icCnSetupCreditRulesPublicService/CreditRuleService")

# Configure the request content type to be xml
request.add_header("Content-Type", 'text/xml;charset=UTF-8')

# Configure the request authentication in the header with base64-encoded user name and password
request.add_header("Authorization", authorization)

# Set the SOAP action to be invoked; while the call works without this, the value is expected to be set based on standards
request.add_header("SOAPAction",
'http://xmlns.oracle.com/apps/incentiveCompensation/cn/creditSetup/creditRule/creditRuleService/findRule')

# Write the xml payload to the request
request.add_data(envelope)

# Execute the request
handle = urllib2.urlopen(request)

# Get the response and process it. This example just prints the response.
content = handle.read()
print content

Sudsクライアント

WSDLに基づくSudsを使用して、Webサービスを使用するためのSOAPエンベロープおよびクライアントを構築します。 sudsのインストールの詳細は、設定ツールを参照してください。

次に示すのは、sudsフレームワークを使用してサービスを呼び出サンプル・コードです。
  1. サービス・コールの資格証明のため、Base 64でエンコードされた文字列を作成します。

  2. コンテンツ・タイプ、SOAP処理、認証情報を含む要求ヘッダーを構成します。

  3. WSDLに基づいて新しいクライアントを作成します。

  4. パラメータとして使用するオブジェクトを移入します。

  5. サービスを呼び出します。

  6. 応答を処理します。 この例は、応答を単に出力します。

import base64
from suds.client import Client

username='username'
password='password'

# Construct the base64-encoded string used as the credentials for the service call
credentials = base64.encodestring('%s:%s' % (username, password))[:-1]
authorization = "Basic %s" % credentials

url = 'https://host:port/icCnSetupCreditRulesPublicService/CreditRuleService?wsdl'

# Configure the request content type, SOAP action, and authentication in header
authenticationHeader = {
   "SOAPAction" :"http://xmlns.oracle.com/apps/incentiveCompensation/cn/creditSetup/creditRule/creditRuleService/findRule",
   "Content-Type" : "text/xml;charset=UTF-8",
   "Authorization" : authorization }
   
# Create new client
client = Client(url=url, headers=authenticationHeader)

# Populate the objects to be used as parameter
findCriteria = client.factory.create('ns4:FindCriteria')
findControl = client.factory.create('ns4:FindControl')
viewCriteria = client.factory.create('ns4:ViewCriteria')
viewCriteriaRow = client.factory.create('ns4:ViewCriteriaRow')
viewCriteriaItem = client.factory.create('ns4:ViewCriteriaItem')

viewCriteriaItem.upperCaseCompare = "false";
viewCriteriaItem.attribute = "RuleId";
viewCriteriaItem.operator = "=";
viewCriteriaItem.value = "300000000851162"

viewCriteriaRow.upperCaseCompare = "false"
viewCriteriaRow.item.append([viewCriteriaItem])
viewCriteria.group.append([viewCriteriaRow])

findCriteria.fetchStart = "0"
findCriteria.fetchSize = "-1"
findCriteria.excludeAttribute = "false"
findCriteria.filter = viewCriteria

findControl.retrieveAllTranslations = "false"

# Call the service
rule = client.service.findRule(findCriteria, findControl)

# Process the response. This example just prints the response.
print rule
関連トピック
  • Javaクライアント
  • Oracle Database
  • .NET Framework
  • PHP
  • Perl
  • Ruby
  • SOAP Webサービスの呼出し