2016-05-19 7 views
3

Webサービスの使用にSUDSを使用しています。私は怒鳴るように試してみました:SUDSを使用してWebサービスを使用している場合はSSLをバイパスします

client = Client(wsdl_url) 
list_of_methods = [method for method in client.wsdl.services[0].ports[0].methods] 
print(list_of_methods) 

私はこのエラーを得た:

urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)> 

私はlinkを見たが、それは、Python 2.7のためだけのソリューションです。 SUDSでSSLをバイパスするにはどうすればよいですか?または、何もpythonのソリューションはありません(たとえば、Windows OSに偽の証明書を追加する)?私はpython 3を使用しています(私はurllib2の代わりにurllibを使用しなければなりません)。

答えて

3

sudsクライアントは、要求を処理するためにsuds.transport.Transportというサブクラスを使用します。

使用されるデフォルトのトランスポートはsuds.transport.https.HttpAuthenticatedのインスタンスですが、transportキーワード引数を渡してクライアントをインスタンス化するときにこれをオーバーライドできます。

httpとhttpsのトランスポートは、urlopenerを作成してurllib.request(またはpython2の場合はurllib2)を使用して実装されています。このurlopenerの作成に使用されたハンドラのリストは、callingu2handlers()メソッドによって取得されます。これは例えば、デフォルトをサブクラス化し、特定のssl contextHTTPSHanderを使用するために、そのメソッドをオーバーライドすることで独自のトランスポートを作成することができますことを意味します

from suds.client import Client 
from suds.transport.https import HttpAuthenticated 
from urllib.request import HTTPSHandler 
import ssl 

class CustomTransport(HttpAuthenticated): 

    def u2handlers(self): 

     # use handlers from superclass 
     handlers = HttpAuthenticated.u2handlers(self) 

     # create custom ssl context, e.g.: 
     ctx = ssl.create_default_context(cafile="/path/to/ca-bundle.pem") 
     # configure context as needed... 
     ctx.check_hostname = False 

     # add a https handler using the custom context 
     handlers.append(HTTPSHandler(context=ctx)) 
     return handlers 

# instantiate client using this transport 
c = Client("https://example.org/service?wsdl", transport=CustomTransport()) 
+0

cafileの価値は何ですか?どうすれば設定できますか? –

+0

これは、コンテキストを作成する方法の例です。この場合、 'cafile'はサーバーのSSL証明書へのパスになります(pem-encoded)。もちろん、スキップ検証にリンクした答えのように 'ctx = ssl._create_unverified_context()'を使うこともできます。または、[証明書を保存する](http://superuser.com/questions/97201/how-to-save-a-remote-server-ssl-certificate-locally-as-a-file)をpemファイルとして保存することができます検証を完全に無効にする必要はありません。 – mata

+0

エラー:例外:(415、u "コンテンツタイプ 'text/xml; charset = utf-8'が期待した内容でないためメッセージを処理できません pe 'application/soap + xml; charset = utf- 8 "。)でも私はヘッダーが石鹸に設定されています –

1

あなたは輸送のための要求のライブラリを活用するhttps://pypi.python.org/pypi/suds_requestsを使用することができます。これにより、ssl検証を無効にすることができます。

それとも私の新しいSOAPライブラリを試し、それが箱から出して、それをサポートしています。

3

このコードは、私の仕事:

from suds.client import Client 
import ssl 

if hasattr(ssl, '_create_unverified_context'): 
    ssl._create_default_https_context = ssl._create_unverified_context 
cli = Client('https://your_lik_to?wsdl') 

print(cli) 
0

以下のコードを追加してsuds clienをインスタンス化することができますT:

import ssl 


try: 
    _create_unverified_https_context = ssl._create_unverified_context 
except AttributeError: 
    pass 
else: 
    ssl._create_default_https_context = _create_unverified_https_context 

詳細については、私自身のウェブサイトを参照してください:https://lucasmarques.me/bypass-ssl/

0

は、これは私がうまく動作するように思われる思い付いたものです:

class MyTransport(HttpAuthenticated): 

    def u2handlers(self): 
     """ 
     Get a collection of urllib handlers. 

     @return: A list of handlers to be installed in the opener. 
     @rtype: [Handler,...] 

     """ 
     handlers = [] 
     context = ssl._create_unverified_context() 
     handlers.append(urllib2.HTTPSHandler(context=context)) 
     return handlers 

乾杯!

関連する問題