How to get the absolute url in python
<p>I am currently working on a http server in python. I have subclassed BaseHttpRequestHandler to handler a get/post request. As per documentation, BaseHttpRequestHandler  has an instance variable path, but how do i get the full request url</p>

<p>Example <a href="http://www.cnn.com/index.html" rel="nofollow">http://www.cnn.com/index.html</a></p>

<pre><code>Class handler(BaseHTTPRequestHandler):
    def do_GET(self):
        # This gives me /index.html
        print self.path 
</code></pre>

<p>But is there a way I can get full url?</p>

<p>You can get the server name (and port, if the server is on a specific port) via <code>self.server.server_name</code> and <code>self.server.server_port</code>. Then just concatenate them - assuming you have a port, and that the server name doesn't include a trailing '/' (can't check at the moment, myself):</p>

<pre><code>full_url = ''.join([name, ':', port, '/', path])
</code></pre>

<p>If you don't have a port specified:</p>

<pre><code>full_url = '/'.join([name, path])
</code></pre>

<p>In both cases, <code>path</code> is self.path, and <code>name</code> and <code>port</code> are as explained at the top.</p>

python python-2.7 http http-headers