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中利用zfill方法自动给数字前面补0

python中利用zfill方法自动给数字前面补0

python中有一个zfill方法用来给字符串前面补0,非常有用 view sourceprint? n = "123" s = n.zfill(5) assert s...

深入理解Django的自定义过滤器

深入理解Django的自定义过滤器

前言 本文主要给大家介绍了关于Django自定义过滤器的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍: 过滤器与函数 django过滤器的本质是函数,但"函...

python中requests库session对象的妙用详解

在进行接口测试的时候,我们会调用多个接口发出多个请求,在这些请求中有时候需要保持一些共用的数据,例如cookies信息。 妙用1 requests库的session对象能够帮我们跨请...

用Python脚本来删除指定容量以上的文件的教程

文件多了乱放, 突然有一天发现硬盘空间不够了, 于是写了个python脚本搜索所有大于10MB的文件,看看这些大文件有没有重复的副本,如果有,全部列出,以便手工删除 使用方式 加一个指定...

Python使用Tkinter实现滚动抽奖器效果

Python使用Tkinter实现滚动抽奖器效果

年底,抽奖这个话题很多人都会讨论,都希望可以中奖。 接下来我就使用 Python 中的 Tkinter 模块来实现一个简单的滚动抽奖器。 一、Tkinter简介 Tkinter 是 Py...