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

yipeiwu_com5年前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中遇到的小问题及解决方法汇总

Python中遇到的小问题及解决方法汇总

本文会把学习过程中遇到的一些小问题和解决办法放在这里,以便于大家能够更好地学习python。 一、Python的异常处理 因为想到自己不断尝试写小程序的话会用到抛出异常信息来判断哪里出现...

Python生成随机MAC地址

利用python代码生成一个随机的MAC地址,使用python网络编程时或可用上,如果使用scapy模块则可直接利用RandMAC()函数来生成MAC。 python 复制代码 代码如下...

用Python的Django框架编写从Google Adsense中获得报表的应用

 我完成了更新我们在 Neutron的实时收入统计。在我花了一周的时间完成并且更新了我们的PHP脚本之后,我最终认决定开始使用Python进行抓取,这是值得我去花费我的时间和精...

python3的输入方式及多组输入方法

python3的输入方式 1. 读取键盘输入 内置函数 input()接收键盘标准输入 str = input("请输入") print(str) 默认返回的是字符串类型,通过强...

Python ORM框架SQLAlchemy学习笔记之数据查询实例

前期我们做了充足的准备工作,现在该是关键内容之一查询了,当然前面的文章中或多或少的穿插了些有关查询的东西,比如一个查询(Query)对象就是通过Session会话的query()方法获取...