2017-04-24 10 views
0

正常に実行されている次のコードを記述しました。しかし、 soap body内で直接ハードコーディングする代わりに、usernameをパラメータとして渡したいと思います。リクエストモジュールを使用してsoap body内の値をパラメータとして渡す方法

import requests 

url = "url?WSDL" 

headers = {"Content-Type": "text/xml;charset=UTF-8"} 

body = """<?xml version="1.0" encoding="UTF-8"?> 
      <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"> 
    <soapenv:Header> 
     <tem:Authentication> 
     <!--Optional:--> 
     <tem:UserName>username</tem:UserName> 
     <!--Optional:--> 
     <tem:Password>password</tem:Password> 
     </tem:Authentication> 
    </soapenv:Header> 
    <soapenv:Body> 
     <tem:func1> 
     <!--Optional:--> 
     <tem:incident_number>INC000005215731</tem:incident_number> 
     <!--Optional:--> 
     <tem:wii> 
      <tem:Submitted>false</tem:Submitted> 
      <tem:Work_Info_Type>General_Information</tem:Work_Info_Type> 
      <!--Optional:--> 
      <tem:Summary>test</tem:Summary> 
     </tem:wii> 
     </tem:func1> 
    </soapenv:Body> 
</soapenv:Envelope>""" 

response = requests.post(url, data=body, headers=headers) 
print response.content 

はまた、どのように私はsudsを使用して上記のコードを変換することができますか?あなたはまた、ジャンゴ/ Jinja2のようなテンプレートエンジンを使用してコンテキスト変数を使用することができ

from suds.client import Client 
import logging 

client = Client("url?WSDL") 
user = client.factory.create('Authentication') 
user['UserName'] = 'username' 
user['Password'] = 'password' 
client.set_options(soapheaders=user) 

答えて

0
body = """<?xml version="1.0" encoding="UTF-8"?> 
      <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"> 
    <soapenv:Header> 
     <tem:Authentication> 
     <!--Optional:--> 
     <tem:UserName>{username}</tem:UserName> 
     <!--Optional:--> 
     <tem:Password>{password}</tem:Password> 
     </tem:Authentication> 
    </soapenv:Header>""" 
response = requests.post(url, data=body.format(username="bob", password="abc123"), headers=headers) 

はこれまでのところ、私は、次のコードを書かれています。

ここで適切な解決策は、泡の代わりに要求

https://bitbucket.org/jurko/suds

+0

に切り替えることで、私はすでに、 'suds'を試してみましたが、私はデバッグの問題を持っていたし、それは私が戻ってrequests''へ移動し、なぜ私が追加したあります元の質問で 'suds'を使っているコードの一部です。あなたはさらに移動する方法を提案できますか? – me24hour

+0

あなたはどのような問題を抱えていますか?あなたの泡の問題の詳細を参考にしてください。パブリックなWSDLですか?あなたの設定は、私がsoapheadersが私のためのリストだと思う以外は何をしているかと似ているようです – Plahcinski

関連する問題