繁体   English   中英

如何在 python 中使用 httplib 处理 cookie

[英]How to work with cookies with httplib in python

我正在使用 httplib 发送肥皂请求,但是当我发送它时,我遇到了以下问题:

INFO:root:Response:<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Authorization Required</title>
</head><body>
<h1>Authorization Required</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
</body></html>

我已经在下面设置标题:

auth = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
headers = {"Content-type": "text/xml;charset=utf-8","SOAPAction":"\"\"","Authorization": "Basic %s" % auth}

如果我通过 SoapUI 发送而不是 python,那么当 SoapUI 从服务器发送请求时,似乎会发送一些 cookie,然后 soapUI 用 cookie 重新发送它。

我使用了urllib2模块而不是 httplib。 我不需要设置 cookie,我将代码更改为使用 Digest 类型身份验证,而不是基本身份验证:

password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None,uri=uri,user=username,passwd=password)
authHandler=urllib2.HTTPDigestAuthHandler(password_mgr)
opener = urllib2.build_opener(authHandler)
response=opener.open(webServiceUrl2,data)
res=response.read()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM