這篇文章主要介紹了python pycurl驗證basic和digest認證的方法,現在分享給大家,也給大家做個參考。一起來看看吧
簡介
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_digestLoadModule 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()
# -*- 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驗證basic和digest認證的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!