<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

	<title>Comments on: What am I missing re: python/urllib2?</title>
	<link>http://ask.metafilter.com/67876/What-am-I-missing-re-pythonurllib2/</link>
	<description>Comments on Ask MetaFilter post What am I missing re: python/urllib2?</description>
	<pubDate>Thu, 26 Jul 2007 13:29:25 -0800</pubDate>
	<lastBuildDate>Thu, 26 Jul 2007 13:29:25 -0800</lastBuildDate>
	<language>en-us</language>
	<docs>http://blogs.law.harvard.edu/tech/rss</docs>
	<ttl>60</ttl>

	<item>
		<title>Question: What am I missing re: python/urllib2?</title>
		<link>http://ask.metafilter.com/67876/What-am-I-missing-re-pythonurllib2</link>	
		<description>Why can&apos;t I get python&apos;s urllib2 library to fetch a page using HTTP basic authentication? &lt;br /&gt;&lt;br /&gt; I&apos;m playing around with python, and trying to use the urllib2 library to fetch a page that&apos;s password protected with a simple .htaccess file.  &lt;br&gt;
&lt;br&gt;
From what I can tell the opener object, when it encounters a 401, is supposed to check the authentication object for a URI match, and if it finds one, retry the URL with the specified username/password.  &lt;br&gt;
&lt;br&gt;
That doesn&apos;t seem to be happening and I&apos;m not comfortable enough with python to know the best way to go about debugging this, and I&apos;m not even sure I&apos;m approaching this right (am I supposed to handle the 401 errors myself?)&lt;br&gt;
&lt;br&gt;
Working code is appreciated, but I&apos;m clearly missing something obvious here, so an explanation of what&apos;s going on would be more appreciated. &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;strong&gt;My Code:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;&lt;br&gt;
#!/usr/local/bin/python&lt;br&gt;
import urllib2&lt;br&gt;
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()&lt;br&gt;
password_mgr.add_password(None, &quot;alanstorm.com/testbed&quot;, &apos;admin&apos;,&apos;the-password&apos;)&lt;br&gt;
handler = urllib2.HTTPBasicAuthHandler(password_mgr)&lt;br&gt;
opener = urllib2.build_opener(handler)&lt;br&gt;
f = opener.open(&quot;http://alanstorm.com/testbed/password-test/test.txt&quot;)&lt;br&gt;
print(f.read());&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
&lt;strong&gt;The page&lt;/strong&gt;&lt;br&gt;
http://alanstorm.com/testbed/password-test/test.txt&lt;br&gt;
&lt;br&gt;
Username: admin&lt;br&gt;
Password: the-password&lt;br&gt;
&lt;br&gt;
&lt;strong&gt;the .htaccess&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;&lt;br&gt;
AuthUserFile /path/to/htpass&lt;br&gt;
AuthName grrrr&lt;br&gt;
AuthType Basic&lt;br&gt;
require user admin&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
&lt;strong&gt;The Error Message&lt;/strong&gt;&lt;br&gt;
&lt;br&gt;
&lt;code&gt;&lt;br&gt;
Traceback (most recent call last):&lt;br&gt;
  File &quot;./my-script.py&quot;, line 34, in ?&lt;br&gt;
    f = opener.open(&quot;http://alanstorm.com/testbed/password-test/test.txt&quot;)&lt;br&gt;
  File &quot;/usr/local/lib/python2.4/urllib2.py&quot;, line 364, in open&lt;br&gt;
    response = meth(req, response)&lt;br&gt;
  File &quot;/usr/local/lib/python2.4/urllib2.py&quot;, line 471, in http_response&lt;br&gt;
    response = self.parent.error(&lt;br&gt;
  File &quot;/usr/local/lib/python2.4/urllib2.py&quot;, line 402, in error&lt;br&gt;
    return self._call_chain(*args)&lt;br&gt;
  File &quot;/usr/local/lib/python2.4/urllib2.py&quot;, line 337, in _call_chain&lt;br&gt;
    result = func(*args)&lt;br&gt;
  File &quot;/usr/local/lib/python2.4/urllib2.py&quot;, line 480, in http_error_default&lt;br&gt;
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)&lt;br&gt;
urllib2.HTTPError: HTTP Error 401: Authorization Required&lt;br&gt;
&lt;/code&gt;</description>
		<guid isPermaLink="false">post:ask.metafilter.com,2007:site.67876</guid>
		<pubDate>Thu, 26 Jul 2007 12:29:10 -0800</pubDate>
		<dc:creator>alan</dc:creator>
		
			<category>code</category>
		
			<category>python</category>
		
			<category>urllib2</category>
		
	</item> <item>
		<title>By: cmiller</title>
		<link>http://ask.metafilter.com/67876/What-am-I-missing-re-pythonurllib2#1016475</link>	
		<description>A URI includes the protocol.&lt;br&gt;
&lt;br&gt;
Change that to &lt;br&gt;
&lt;b&gt;password_mgr.add_password(None, &quot;http://alanstorm.com/testbed&quot;, &apos;admin&apos;,&apos;the-password&apos;)&lt;/b&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.67876-1016475</guid>
		<pubDate>Thu, 26 Jul 2007 13:29:25 -0800</pubDate>
		<dc:creator>cmiller</dc:creator>
	</item><item>
		<title>By: alan</title>
		<link>http://ask.metafilter.com/67876/What-am-I-missing-re-pythonurllib2#1016520</link>	
		<description>No dice there.  I tried it both with the http protocol and without. Same results.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.67876-1016520</guid>
		<pubDate>Thu, 26 Jul 2007 14:10:02 -0800</pubDate>
		<dc:creator>alan</dc:creator>
	</item><item>
		<title>By: cmiller</title>
		<link>http://ask.metafilter.com/67876/What-am-I-missing-re-pythonurllib2#1016538</link>	
		<description>Uh huh.&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;cmiller@zippy:~ $ python t.py &lt;br&gt;
i r in your secret dir&lt;br&gt;
&lt;br&gt;
peeking at your files&lt;br&gt;
&lt;br&gt;
cmiller@zippy:~ $ cat t.py &lt;br&gt;
#!/usr/local/bin/python&lt;br&gt;
import urllib2&lt;br&gt;
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()&lt;br&gt;
password_mgr.add_password(None, &quot;&lt;strong&gt;http://&lt;/strong&gt;alanstorm.com/testbed&quot;, &apos;admin&apos;,&apos;the-password&apos;)&lt;br&gt;
handler = urllib2.HTTPBasicAuthHandler(password_mgr)&lt;br&gt;
opener = urllib2.build_opener(handler)&lt;br&gt;
f = opener.open(&quot;http://alanstorm.com/testbed/password-test/test.txt&quot;)&lt;br&gt;
print(f.read());&lt;/pre&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.67876-1016538</guid>
		<pubDate>Thu, 26 Jul 2007 14:24:55 -0800</pubDate>
		<dc:creator>cmiller</dc:creator>
	</item><item>
		<title>By: alan</title>
		<link>http://ask.metafilter.com/67876/What-am-I-missing-re-pythonurllib2#1016639</link>	
		<description>Yeah, that&apos;s really weird.  &lt;br&gt;
&lt;br&gt;
I took your script, put in on my host&apos;s server, and still got the same error, which to me says hosed/old python install.&lt;br&gt;
&lt;br&gt;
Thanks for sanity check!</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.67876-1016639</guid>
		<pubDate>Thu, 26 Jul 2007 15:33:02 -0800</pubDate>
		<dc:creator>alan</dc:creator>
	</item><item>
		<title>By: evariste</title>
		<link>http://ask.metafilter.com/67876/What-am-I-missing-re-pythonurllib2#1017921</link>	
		<description>Yo. I r in your secret dir, peeking at your files. Here&apos;s a &lt;a href=&quot;http://discardedlies.com/media/axme/alan_urrllib2.py.txt&quot;&gt;working version&lt;/a&gt; of your script. See if that works. If it doesn&apos;t, show me the output.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.67876-1017921</guid>
		<pubDate>Fri, 27 Jul 2007 21:15:40 -0800</pubDate>
		<dc:creator>evariste</dc:creator>
	</item><item>
		<title>By: evariste</title>
		<link>http://ask.metafilter.com/67876/What-am-I-missing-re-pythonurllib2#1017929</link>	
		<description>Hmm. cmiller&apos;s script works perfectly for me, too. But we approached things somewhat differently; I added a urllib2.install_opener call. I&apos;d be curious to see if my script works on your host&apos;s server or not.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.67876-1017929</guid>
		<pubDate>Fri, 27 Jul 2007 21:42:20 -0800</pubDate>
		<dc:creator>evariste</dc:creator>
	</item>
	</channel>
</rss>
