python pycurl验证basic和digest认证的方法

yipeiwu_com6年前Python基础

简介

pycurl类似于Python的urllib,但是pycurl是对libcurl的封装,速度更快。

本文使用的是pycurl 7.43.0.1版本。

Apache下配置Basic认证

生成basic密码文件

htpasswd -bc passwd.basic test 123456

开启mod_auth_basic

LoadModule auth_basic_module modules/mod_auth_basic.so

配置到具体目录

<Directory "D:/test/basic">
  AuthName "Basic Auth Dir"
  AuthType Basic
  AuthUserFile conf/passwd.basic
  require valid-user
</Directory>

重启Apache

Apache下配置Digest认证

生成Digest密码文件

htdigest -c passwd.digest "Digest Encrypt" test

开启mod_auth_digest

LoadModule auth_digest_module modules/mod_auth_digest.so

配置到具体目录

<Directory "D:/test/digest">
  AuthType Digest
  AuthName "Digest Encrypt" # 要与密码的域一致
  AuthDigestProvider file
  AuthUserFile conf/passwd.digest
  require valid-user
</Directory>

重启Apache

验证Basic认证

# -*- coding: utf-8 -*-
import pycurl
try:
  from io import BytesIO
except ImportError:
  from StringIO import StringIO as BytesIO
buffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.URL, 'http://test/basic/')
c.setopt(c.WRITEDATA, buffer)
c.setopt(c.HTTPAUTH, c.HTTPAUTH_BASIC)
c.setopt(c.USERNAME, 'test')
c.setopt(c.PASSWORD, '123456')
c.perform()
print('Status: %d' % c.getinfo(c.RESPONSE_CODE))
print(buffer.getvalue())
c.close()

验证Digest认证

# -*- coding: utf-8 -*-
import pycurl
try:
  from io import BytesIO
except ImportError:
  from StringIO import StringIO as BytesIO
buffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.URL, 'http://test/digest/')
c.setopt(c.WRITEDATA, buffer)
c.setopt(c.HTTPAUTH, c.HTTPAUTH_DIGEST)
c.setopt(c.USERNAME, 'test')
c.setopt(c.PASSWORD, '123456')
c.perform()
print('Status: %d' % c.getinfo(c.RESPONSE_CODE))
print(buffer.getvalue())
c.close()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

从局部变量和全局变量开始全面解析Python中变量的作用域

理解全局变量和局部变量 1.定义的函数内部的变量名如果是第一次出现, 且在=符号前,那么就可以认为是被定义为局部变量。在这种情况下,不论全局变量中是否用到该变量名,函数中使用的都是局部变...

python读取注册表中值的方法

在Python的标准库中,_winreg.pyd可以操作Windows的注册表,另外第三方的win32库封装了大量的Windows API,使用起来也很方便。不过这里介绍的是使用_win...

python根据出生年份简单计算生肖的方法

本文实例讲述了python根据出生年份简单计算生肖的方法。分享给大家供大家参考。具体分析如下: 这里使用python根据出生年份计算生肖,看了代码会发现原来这么简单 #计算生肖 de...

python定义类self用法实例解析

这篇文章主要介绍了python定义类self用法实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 在定义类的过程中,无论是显式的...

Python 中的 global 标识对变量作用域的影响

global 标识用于在函数内部,修改全局变量的值。 我们可以通过以下规则,来判定一个变量到底是在全局作用域还是局部作用域: 变量定义在全局作用域,那就是全局变量。 变量在函数...