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
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.
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.
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.
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!
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With