Python Network Programming 简明教程
Python - HTTP Authentication
身份验证是确定请求是否来自具有使用系统所需权限的有效用户的过程。在计算机网络的世界中,这是一个非常重要的要求,因为许多系统不断相互交互,并且需要适当的机制来确保仅在这些程序之间发生有效的交互。
Authentication is the process of determining if the request has come from a valid user who has the required privileges to use the system. In the world of computer networking this is a very vital requirement as many systems keep interacting with each other and proper mechanism needs to ensure that only valid interactions happen between these programs.
python 模块名称`@ {s2}`具有内置功能,可以调用由提供服务的网络应用程序提供的各种 API 及用户凭证。这些凭据必须嵌入到调用程序中。如果 API 成功验证凭据,则会进行有效的登录。
The python module names requests has in-built feature to call various APIs provided by the serving web apps along with the user credentials. These credentials have to be embedded in the calling program. If the APIs verify it successfully then a valid login happens.
Installing Requests
我们安装名为 requests 的必需 python 模块来运行身份验证程序。
We install the required python module named requests for running the authentication program.
pip install requests
Authenticating to Github
下面我们看到一个简单的身份验证机制,仅涉及用户名和密码。成功的响应表示有效的登录。
Below we see a simple authentication mechanism involving only the username and the password. A successful response indicates valid login.
import requests
r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
print r
当我们运行以上程序时,我们得到以下输出:
When we run the above program, we get the following output −