簡體   English   中英

使用SUDS清空cookiejar

[英]Empty cookiejar using SUDS

我正在使用python 2.6.2在Linux Slackware 13.0上運行SUDS 0.4。 當我使用以下代碼調用SOAP方法時:

from suds.client import Client

client = Client(url='file:acctWeb.wsdl',
                location='http://10.242.69.4:8088/pfmaccess')

res = client.service.login(login='user',password='passwd')

我收到以下回應:

DEBUG:suds.transport.http:received:
CODE: 200
HEADERS: {'set-cookie': 'OSP_Ref=0000000573800052;Domain=10.242.69.4:8088;Path=/pfmaccess', 'content-length': '26541', 'content-type': 'text/xml; charset=utf-8', 'connection': 'close', 'server': 'Alcatel-Lucent OSP 2.4'}

>>> client.options.transport.cookiejar
<cookielib.CookieJar[]>

顯示沒有可用的cookie。 這可能是什么原因? 我無法使用SOAP API,因為我需要傳遞在響應Cookie中發送的憑據。

請幫我。

BR

jan

好的,我已經玩了一些。

首先,一個小的測試服務器( 由soaplib提供 ):

import soaplib
from soaplib.core.service import rpc, DefinitionBase, soap
from soaplib.core.model.primitive import String, Integer
from soaplib.core.server import wsgi
from soaplib.core.model.clazz import Array
import sys, pprint

class HelloWorldService(DefinitionBase):
    @soap(String,Integer,_returns=Array(String))
    def say_hello(self,name,times):
        results = []
        for i in range(0,times):
            results.append('Hello, %s'%name)
        return results

class WsgiApp(wsgi.Application):

    def on_wsgi_return(self, env, headers, return_str):
        headers['Set-Cookie'] = 'spam=eggs;domain=127.0.0.1;path=/'
        print >>sys.stderr, headers

if __name__=='__main__':
    try:
        from wsgiref.simple_server import make_server
        soap_application = soaplib.core.Application([HelloWorldService], 'tns')
        wsgi_application = WsgiApp(soap_application)
        server = make_server('localhost', 7789, wsgi_application)
        server.serve_forever()
    except ImportError:
        print "Error: example server code requires Python >= 2.5"

稍加修改即可設置Cookie標頭。

和suds-testclient:

from suds import client, transport

c = client.Client("http://127.0.0.1:7789/?wsdl")
print c.service.say_hello("spam", 1)
print c.options.transport.cookiejar

運行此生成:

(stringArray){
   string[] = 
      "Hello, spam",
 }
<cookielib.CookieJar[<Cookie spam=eggs for .127.0.0.1/>]>

因此它可以正常工作。 但是,如果您將請求網址更改為http://localhost:7789/?wsdl ,則會得到:

(stringArray){
   string[] = 
      "Hello, spam",
 }
<cookielib.CookieJar[]>

在客戶端打開cookielib一些日志記錄...

import logging
import cookielib
logging.basicConfig()
logging.getLogger('cookielib').setLevel(logging.DEBUG)
cookielib.debug = True

...並揭示出原因:

DEBUG:cookielib:add_cookie_header
DEBUG:cookielib:extract_cookies: Date: Thu, 17 May 2012 15:56:01 GMT
Server: WSGIServer/0.1 Python/2.7.3
Set-Cookie: spam=eggs;domain=127.0.0.1;path=/
Content-Length: 822
Content-Type: text/xml

DEBUG:cookielib: - checking cookie spam=eggs
DEBUG:cookielib:   effective request-host localhost.local (even with added initial
                   dot) does not end with .127.0.0.1
(stringArray){
   string[] = 
      "Hello, spam",
 }
<cookielib.CookieJar[]>

簡單的解釋是:cookie域與請求的服務器域不匹配,並且似乎cookielib在驗證域時不進行任何查找。

因此解決方案將是以下之一:

  • 確保客戶端和服務器使用相同的域(域名或IP)
    在示例中,我必須將它們都設置為localhost.local才能正常工作(可能取決於hosts文件...)
  • 從發送的cookie中刪除域,然后cookieib自動使用請求域
  • 實現使用DNS查找的cookiejar

哦,最后但並非最不重要的一點:它在OP問題中不起作用的原因:
該端口不屬於域,因此始終拒絕包含Domain=10.242.69.4:8088的cookie。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM