简介
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基于PycURL实现POST的方法Python中的CURL PycURL使用例子python中pycurl库的用法实例简单谈谈Python的pycurl模块Python安装pycurl失败的解决方法解决python3 安装完Pycurl在import pycurl时报错的问题python通过get,post方式发送http请求和接收http响应的方法Python模仿POST提交HTTP数据及使用Cookie值的方法python通过post提交数据的方法利用python模拟实现POST请求提交图片的方法Python3模拟curl发送post请求操作示例