Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python pycurl get final url redirect

Tags:

python

pycurl

i need visit website whit pycurl, follow redirects, and print final url, i write this python code :

c = pycurl.Curl()
c.setopt(c.URL, 'http://localhost/redirect.php')
c.setopt(c.HTTPPOST, values)
c.setopt(c.WRITEFUNCTION, buf_pagina.write)
c.setopt(c.HEADERFUNCTION, buf_header.write)
c.setopt(c.CONNECTTIMEOUT, 30)
c.setopt(c.AUTOREFERER,1)
c.setopt(c.FOLLOWLOCATION, 1)
c.setopt(c.COOKIEFILE, '')
c.setopt(c.TIMEOUT, 30)
c.setopt(c.USERAGENT, '')
c.perform()

i need print the final url, how can i make this ? thanks.

the solution is this : url_effective = c.getinfo(c.EFFECTIVE_URL)

like image 504
kingcope Avatar asked Jan 29 '14 23:01

kingcope


1 Answers

here's an adaptation of the PHP script I linked in the comments:

import pycurl
import sys
import StringIO

o = StringIO.StringIO()
h = StringIO.StringIO()

c = pycurl.Curl()
c.setopt(c.URL, 'http://stackoverflow.com/questions/21444891')
# c.setopt(c.HTTPPOST, values)
c.setopt(c.WRITEFUNCTION, o.write)
c.setopt(c.HEADERFUNCTION, h.write)
c.setopt(c.CONNECTTIMEOUT, 30)
c.setopt(c.AUTOREFERER,1)
c.setopt(c.FOLLOWLOCATION, 1)
c.setopt(c.COOKIEFILE, '')
c.setopt(c.TIMEOUT, 30)
c.setopt(c.USERAGENT, '')
c.perform()

h.seek(0)

location = ""

for l in h:
    if "Location" in l:
        location = l.split(": ")[-1]

print location

though, as this example shows, you may not always have the full URI, only the path part of the URI (but if that's the case, that's easy to add the fqdn back)

like image 154
zmo Avatar answered Oct 11 '22 00:10

zmo