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实现批量修改图片格式和尺寸

本文实例为大家分享了python批量处理图片的具体代码,供大家参考,具体内容如下 公司的一个项目要求把所有4096x4096的图片全部转化成2048x2048的图片,这种批量转换图片大...

python数据结构之二叉树的建立实例

先建立二叉树节点,有一个data数据域,left,right 两个指针域复制代码 代码如下:# -*- coding: utf - 8 - *-   &nb...

Pytorch之finetune使用详解

finetune分为全局finetune和局部finetune。首先介绍一下局部finetune步骤: 1.固定参数 for name, child in model.named...

Python OpenCV处理图像之图像像素点操作

Python OpenCV处理图像之图像像素点操作

本文实例为大家分享了Python OpenCV图像像素点操作的具体代码,供大家参考,具体内容如下 0x01. 像素 有两种直接操作图片像素点的方法: 第一种办法就是将一张图片看成一个多维...

常见的python正则用法实例讲解

下面列出Python正则表达式的几种匹配用法: 此外,关于正则的一切http://deerchao.net/tutorials/regex/regex.htm  1.测试正则表...