我正在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()  

Python-3.x相关问答推荐

如何在选中项目时设置QTreeView中整行的 colored颜色 ?

如何使用regex将电话号码和姓名从文本字符串中分离出来

动态范围内来自另外两列的列求和

如何使用python将pdf文件的页面合并为单个垂直组合页面

使用gekko python的混合整数非线性规划

如何使用`re.findall`从字符串中提取数据

非拉丁字符的Python正则表达式不起作用

Python - 使用 OpenCV 将字节图像转换为 NumPy 数组

Python 3:函数参数中的省略号?

ValueError:预期的 2D 数组,得到 1D 数组:

如何通过命令行将数组传递给python

ImportError:无法导入名称cross_validate

如何制作函数Collection

Python 的 unittest 和 unittest2 模块有什么区别?

谁能给我一个 Python 3 中标准输入和标准输出的快速教程?

用于 unicode 大写单词的 Python 正则表达式

使用 asyncio 的多个循环

如何阻止散景在 Jupyter Notebook 中打开新标签?

__iter__ 和 __getitem__ 有什么区别?

如何更改 tkinter 文本小部件中某些单词的 colored颜色 ?