我正在try 使用python进行HTTPS GET的基本身份验证.我对python非常陌生,指南似乎使用不同的库来做事情.(http.client、httplib和urllib).有人能告诉我是怎么做的吗?你如何告诉标准库使用它?
我正在try 使用python进行HTTPS GET的基本身份验证.我对python非常陌生,指南似乎使用不同的库来做事情.(http.client、httplib和urllib).有人能告诉我是怎么做的吗?你如何告诉标准库使用它?
在Python3中,以下内容将起作用.我使用的是标准库中较低级别的http.client.也可以查看rfc2617的第2节,了解基本授权的详细信息.此代码不会判断证书是否有效,但会设置https连接.请参阅http.client篇关于如何做到这一点的文档.
from http.client import HTTPSConnection
from base64 import b64encode
#This sets up the https connection
c = HTTPSConnection("www.google.com")
#we need to base 64 encode it
#and then decode it to acsii as python 3 stores it as a byte string
userAndPass = b64encode(b"username:password").decode("ascii")
headers = { 'Authorization' : 'Basic %s' % userAndPass }
#then connect
c.request('GET', '/', headers=headers)
#get the response back
res = c.getresponse()
# at this point you could check the status etc
# this gets the page text
data = res.read()