<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>Thoughts from Mads Sülau Jørgensen &#187; HTTP</title> <atom:link href="http://madssj.com/blog/tag/http/feed/" rel="self" type="application/rss+xml" /><link>http://madssj.com/blog</link> <description>Various articles about programming and systems administration.</description> <lastBuildDate>Mon, 27 Jun 2011 08:54:43 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>Python http_head method</title><link>http://madssj.com/blog/2009/05/15/python-http_head-method/</link> <comments>http://madssj.com/blog/2009/05/15/python-http_head-method/#comments</comments> <pubDate>Fri, 15 May 2009 10:30:28 +0000</pubDate> <dc:creator>Mads Sülau Jørgensen</dc:creator> <category><![CDATA[Uncategorized]]></category> <category><![CDATA[HEAD]]></category> <category><![CDATA[HTTP]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[Work]]></category> <guid
isPermaLink="false">http://swag.dk/blog/2009/05/15/python-http_head-method/</guid> <description><![CDATA[Seeing as there is no really easy way to do a HTTP HEAD request from python, I wrote up the following small method: In advance I&#8217;d like to apologize for the method that assemblies the request path. Update: Added handling &#8230; <a
href="http://madssj.com/blog/2009/05/15/python-http_head-method/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>Seeing as there is no really easy way to do a HTTP <code>HEAD</code> request from python, I wrote up the following small method:</p><p>In advance I&#8217;d like to apologize for the method that assemblies the request path.</p><p><strong>Update:</strong> <em>Added handling of redirects.</em></p><div
class="wp_syntax"><div
class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> http_head<span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">httplib</span>
    <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urlparse</span>
&nbsp;
    redirects = <span style="color: #ff4500;">0</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">while</span> redirects <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">10</span>:
        scheme, netloc, path, query, fragment = <span style="color: #dc143c;">urlparse</span>.<span style="color: black;">urlsplit</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> scheme == <span style="color: #483d8b;">'https'</span>:
            conn = <span style="color: #dc143c;">httplib</span>.<span style="color: black;">HTTPSConnection</span><span style="color: black;">&#40;</span>netloc<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            conn = <span style="color: #dc143c;">httplib</span>.<span style="color: black;">HTTPConnection</span><span style="color: black;">&#40;</span>netloc<span style="color: black;">&#41;</span>
&nbsp;
        conn.<span style="color: black;">request</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;HEAD&quot;</span>, <span style="color: #483d8b;">&quot;%s%s%s%s%s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>path, query <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #483d8b;">&quot;?&quot;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #483d8b;">&quot;&quot;</span>, query,
                                             fragment <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #483d8b;">&quot;#&quot;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #483d8b;">&quot;&quot;</span>, fragment<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
        res = conn.<span style="color: black;">getresponse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> res.<span style="color: black;">status</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">301</span>, <span style="color: #ff4500;">302</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> res.<span style="color: black;">getheader</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'location'</span><span style="color: black;">&#41;</span>:
            url = res.<span style="color: black;">getheader</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'location'</span><span style="color: black;">&#41;</span>
            redirects += <span style="color: #ff4500;">1</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #ff7700;font-weight:bold;">break</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> res.<span style="color: black;">status</span>, res.<span style="color: black;">reason</span></pre></div></div> ]]></content:encoded> <wfw:commentRss>http://madssj.com/blog/2009/05/15/python-http_head-method/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
