使用 SUDS 清空 cookiejar

发布于 2024-12-27 18:29:48 字数 838 浏览 7 评论 0原文

我正在 Linux Slackware 13.0 上使用 python 2.6.2 运行 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

I'm running SUDS 0.4 on Linux Slackware 13.0 with python 2.6.2. When I call SOAP method using this code:

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')

I receive following response:

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'}

but

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

shows that there are no cookies available. What could be a reason for that? I'm not able to use SOAP API because I need to pass credentials sent in response cookie.

Please help me on this.

BR

rjan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

菊凝晚露 2025-01-03 18:29:48

好吧,我已经玩了一下。

首先,一个小测试服务器(由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 将始终被拒绝。

Ok, i've played around a bit with it.

first, a little test server (courtesy of 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"

with some little modification to set a cookie header.

and a 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

running this produces:

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

so it seams to work. but if you change the request url to http://localhost:7789/?wsdl you'll get:

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

turning on some logging for cookielib at the client...

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

... and it reveals reveals why:

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[]>

the simple explanation is: the cookie domain does not match the request's server domain, and as it seems cookielib does not do any lookup when verifying the domain.

so the solution would be one of:

  • make sure client and server use the same domain (either domainname or IP)
    in the example i have to set both to localhost.local to make it work (may depend on the hosts file...)
  • remove the domain from the sent cookie, then cookieib uses the request domain automatically
  • implement a cookiejar that uses DNS lookups

Oh, and last but not least: the reason why it didn't work in the OPs question:
the port is not part of the domain, therfore a cookie with Domain=10.242.69.4:8088 will always be rejected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文