How to work with cookies with httplib in python
<p>I am sending soap request using httplib but when I send it I am getting the following issue:</p>

<pre><code>INFO:root:Response:&lt;!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"&gt;
&lt;html&gt;&lt;head&gt;
&lt;title&gt;401 Authorization Required&lt;/title&gt;
&lt;/head&gt;&lt;body&gt;
&lt;h1&gt;Authorization Required&lt;/h1&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;/body&gt;&lt;/html&gt;
</code></pre>

<p>I am setting already headers below:</p>

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

<p>Instead of python if I send by SoapUI then it seems when request is sent by SoapUI from server some cookie is being sent and then soapUI resends it with cookie. </p>

<p>Instead of httplib, I used the <code>urllib2</code> module. I need not set a cookie, I changed the code to use Digest type authentication, instead of Basic authentication:</p>

<pre><code>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()
</code></pre>

python python-2.7