Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 960
code base 5000 Avatar asked May 20 '14 15:05

code base 5000


People also ask

What is the HTTP version of a Python website?

For example, the path of this page is /python-https. The version is one of several HTTP versions, like 1.0, 1.1, or 2.0. The most common is probably 1.1. The headers help describe additional information for the server. The body provides the server with information from the client.

Can you use Python for HTTPS?

You’ve made a fully-functioning Python HTTPS server and queried it successfully. You and the Secret Squirrels now have messages that you can trade back and forth happily and securely! In this tutorial, you’ve learned some of the core underpinnings of secure communications on the Internet today.

What is the path and version of the HTTP request?

The path indicates to the server what web page you would like to request. For example, the path of this page is /python-https. The version is one of several HTTP versions, like 1.0, 1.1, or 2.0. The most common is probably 1.1.

How do I start a Python HTTPS server?

Using your original server.py file, run the following command to start your brand new Python HTTPS application: Congratulations! You now have a Python HTTPS-enabled server running with your very own private-public key pair, which was signed by your very own Certificate Authority!


1 Answers

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"
like image 132
m3nda Avatar answered Sep 28 '22 00:09

m3nda