Python, Detect is a URL needs to be HTTPS vs HTTP
Asked Answered
E

1

9

Using the python standard library, is there a way to determine if a given web address should use HTTP or HTTPS? If you hit a site using HTTP://.com is there a standard error code that says hey dummy it should be 'HTTPS' not http?

Thank you

Ebonize answered 20/5, 2014 at 15:53 Comment(0)
A
6

Did u make any sort of testing?

The short, prematural answer of your questions is: Does not exist should use... it's your preference, or a server decision at all, because of redirects.

Some servers does allow only https, and when you call http does return 302 code.

So, if you goal is to load https from a given url, just try it with a fallback to normal http.

I've recommend you to send only HEAD requests, so you can recognize very fast if the https connection is being listening or not. I do not recommend you to check for port 443 (ssl) because sometimes people do not follow that rule and https protocol will ensure that you is under https and not under a fake 443 port.

A bit of code:

#!/usr/bin/env python
#! -*- coding: utf-8 -*-

from urlparse import urlparse
import httplib, sys

def check_url(url):
  url = urlparse(url)
  conn = httplib.HTTPConnection(url.netloc)   
  conn.request("HEAD", url.path)
  if conn.getresponse():
    return True
  else:
    return False

if __name__ == "__main__":
  url = "http://httpbin.org"
  url_https = "https://" + url.split("//")[1]
  if check_url(url_https):
    print "Nice, you can load it with https"
  else:
    if check_url(url):
      print "https didn't load, but you can use http"
  if check_url(url):
    print "Nice, it does load with http too"
Anthropophagi answered 22/10, 2015 at 23:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.