问题
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
回答1:
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"
来源:https://stackoverflow.com/questions/23764639/python-detect-is-a-url-needs-to-be-https-vs-http