协议判断:HTTP?HTTPS?
Posted KeepPassion
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了协议判断:HTTP?HTTPS?相关的知识,希望对你有一定的参考价值。
#coding=utf-8
# 脚本功能:判断http协议是http还是https
import requests
def get_status(url):
try:
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/75.0.3770.80 Safari/537.36'}
r = requests.head(url,headers = headers, timeout=5)
# r = requests.get(url,headers = headers, timeout=5,allow_redirects=False) 控制requests请求不自动跳转
code = r.status_code
except :
code = 0
return code
def main():
fr = open('test.txt',"r")
fw = open ('test1.txt',"w")
domains = fr.readlines()
# print(type(domains))
for domain in domains: # 依次读取每行
domain = domain.strip() # 去掉每行头尾空白
print ("读取的数据为: %s" % (domain))
code=get_status(domain)
print (code)
if not code: # code 为0、False、None、都可以这样写
fw.write("请求失败\n")
elif code in [301,302]:
fw.write(domain + " https\n")
else:
fw.write(domain + " http\n")
fr.close()
fw.close()
if __name__=="__main__":
main()
运行结果:
日期:2020.1.10
以上是关于协议判断:HTTP?HTTPS?的主要内容,如果未能解决你的问题,请参考以下文章
Flutter 报错 DioError [DioErrorType.DEFAULT]: Bad state: Insecure HTTP is not allowed by platform(代码片段