How to un-shorten (resolve) a url using python, when final url is https?

kyrenia

I am looking to unshorten (resolve) a url in python, when the final urls are https. I have seen the question: How can I un-shorten a URL using python? (as well as similar others), however as noted in the comment to the accepted answer, this solution only works when the urls is not redirected to https.

For reference, the code in that question (which works fine when redirecting to http urls) is:

# This is for Py2k.  For Py3k, use http.client and urllib.parse instead, and
# use // instead of / for the division
import httplib
import urlparse

def unshorten_url(url):
    parsed = urlparse.urlparse(url)
    h = httplib.HTTPConnection(parsed.netloc)
    resource = parsed.path
    if parsed.query != "":
        resource += "?" + parsed.query
    h.request('HEAD', resource )
    response = h.getresponse()
    if response.status/100 == 3 and response.getheader('Location'):
        return unshorten_url(response.getheader('Location')) # changed to     process chains of short urls
    else:
        return url

(note - for obvious bandwidth reasons, I am looking to achieve via only asking for the file header's [i.e. like the http-only version above] and not by asking for the content of the whole pages)

AChampion

You can get the scheme from the url and then use HTTPSConnection if the parsed.scheme is https.
You can also use the requests library to do this very simply.

>>> import requests
>>> r = requests.head('http://bit.ly/IFHzvO', allow_redirects=True)
>>> print(r.url)
https://www.google.com

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Shorten URL using htaccess

From Dev

Get full url from shorten url using python

From Dev

Shorten URL using htaccess file

From Dev

Using htaccess to shorten URL with variables

From Dev

resolve this cover url to final destination url in php

From Dev

how to rewrite or shorten the Sitecore URL

From Dev

How to shorten the URL for a Restful service

From Dev

php how to shorten url - glob function

From Dev

php how to shorten url - glob function

From Dev

How to encode and shorten hash for url safety?

From Dev

Avoiding getting final '&' in Python regex when extracting url param

From Dev

Shorten url with GET parameters

From Dev

Shorten URL with mod rewrite

From Dev

How to download file using python when url doesn't change

From Dev

How to shorten a link using bitly API that contains a url in GET parameters [PHP]

From Dev

Not able to load https url when using selenium webdriver + phantom

From Dev

how to resolve optional url path using ng-resource

From Dev

Creating a shorten URL for products using Amazon advertising API

From Dev

Java, shorten URL using goo.gl API?

From Dev

Apache rewrite : how avoid to display url parameter when the url final slash is omitted?

From Dev

<403> Forbidden on the https:// URL, and <302> Moved Temporarily when using the http:// URL

From Dev

When using URL.openConnection(), what is the best way to handle URL variations like "www" and "https"?

From Dev

How to get final_url using the URL Fetch PHP API on Google App Engine?

From Dev

How to get final_url using the URL Fetch PHP API on Google App Engine?

From Dev

How to validate Vine URL and allow HTTP or HTTPS using PHP

From Dev

How to get response code from a Https Url using java

From Dev

How to parse https url that redirects to another url

From Dev

How can I shorten URL extentions to make them more friendly

From Dev

How to shorten a long url containing meta data, to work within a javascript

Related Related

  1. 1

    Shorten URL using htaccess

  2. 2

    Get full url from shorten url using python

  3. 3

    Shorten URL using htaccess file

  4. 4

    Using htaccess to shorten URL with variables

  5. 5

    resolve this cover url to final destination url in php

  6. 6

    how to rewrite or shorten the Sitecore URL

  7. 7

    How to shorten the URL for a Restful service

  8. 8

    php how to shorten url - glob function

  9. 9

    php how to shorten url - glob function

  10. 10

    How to encode and shorten hash for url safety?

  11. 11

    Avoiding getting final '&' in Python regex when extracting url param

  12. 12

    Shorten url with GET parameters

  13. 13

    Shorten URL with mod rewrite

  14. 14

    How to download file using python when url doesn't change

  15. 15

    How to shorten a link using bitly API that contains a url in GET parameters [PHP]

  16. 16

    Not able to load https url when using selenium webdriver + phantom

  17. 17

    how to resolve optional url path using ng-resource

  18. 18

    Creating a shorten URL for products using Amazon advertising API

  19. 19

    Java, shorten URL using goo.gl API?

  20. 20

    Apache rewrite : how avoid to display url parameter when the url final slash is omitted?

  21. 21

    <403> Forbidden on the https:// URL, and <302> Moved Temporarily when using the http:// URL

  22. 22

    When using URL.openConnection(), what is the best way to handle URL variations like "www" and "https"?

  23. 23

    How to get final_url using the URL Fetch PHP API on Google App Engine?

  24. 24

    How to get final_url using the URL Fetch PHP API on Google App Engine?

  25. 25

    How to validate Vine URL and allow HTTP or HTTPS using PHP

  26. 26

    How to get response code from a Https Url using java

  27. 27

    How to parse https url that redirects to another url

  28. 28

    How can I shorten URL extentions to make them more friendly

  29. 29

    How to shorten a long url containing meta data, to work within a javascript

HotTag

Archive