Tornado 异步请求使用 Digest 等复杂鉴权

方案简介

使用 tornado.curl_httpclient.CurlAsyncHTTPClient 模块代替 tornado.httpclient.AsyncHTTPClient 模块,其余与普通请求相同。

准备条件

  • 使用 CurlAsyncHTTPClient 模块需已安装 pycurl 模块,使用:

    pip install pycurl
    

    进行安装,出现以下信息时,安装完成:

    Collecting pycurl
      Using cached http://mirrors.aliyun.com/pypi/packages/09/ca/0b6da1d0f391acb8991ac6fdf8823ed9cf4c19680d4f378ab1727f90bd5c/pycurl-7.45.1.tar.gz (233 kB)
      Preparing metadata (setup.py) ... done
    Using legacy 'setup.py install' for pycurl, since package 'wheel' is not installed.
    Installing collected packages: pycurl
      Running setup.py install for pycurl ... done
    Successfully installed pycurl-7.45.1
    
  • 安装 pycurl 模块时,若出现以下错误且系统为 Linux:

    Preparing metadata (setup.py) ... error
      error: subprocess-exited-with-error
      
      × python setup.py egg_info did not run successfully.
      │ exit code: 1
      ╰─> [23 lines of output]
          Traceback (most recent call last):
            File "/tmp/pip-install-zcybffzb/pycurl_58616b955fb047baae5bab72545c74a1/setup.py", line 235, in configure_unix
              p = subprocess.Popen((self.curl_config(), '--version'),
            File "/usr/local/Python38/lib/python3.8/subprocess.py", line 854, in __init__
              self._execute_child(args, executable, preexec_fn, close_fds,
            File "/usr/local/Python38/lib/python3.8/subprocess.py", line 1702, in _execute_child
              raise child_exception_type(errno_num, err_msg, err_filename)
          FileNotFoundError: [Errno 2] No such file or directory: 'curl-config'
          
          During handling of the above exception, another exception occurred:
          
          Traceback (most recent call last):
            File "<string>", line 2, in <module>
            File "<pip-setuptools-caller>", line 34, in <module>
            File "/tmp/pip-install-zcybffzb/pycurl_58616b955fb047baae5bab72545c74a1/setup.py", line 1017, in <module>
              ext = get_extension(sys.argv, split_extension_source=split_extension_source)
            File "/tmp/pip-install-zcybffzb/pycurl_58616b955fb047baae5bab72545c74a1/setup.py", line 673, in get_extension
              ext_config = ExtensionConfiguration(argv)
            File "/tmp/pip-install-zcybffzb/pycurl_58616b955fb047baae5bab72545c74a1/setup.py", line 99, in __init__
              self.configure()
            File "/tmp/pip-install-zcybffzb/pycurl_58616b955fb047baae5bab72545c74a1/setup.py", line 240, in configure_unix
              raise ConfigurationError(msg)
          __main__.ConfigurationError: Could not run curl-config: [Errno 2] No such file or directory: 'curl-config'
          [end of output]
      
      note: This error originates from a subprocess, and is likely not a problem with pip.
    

    则代表系统特定库缺失,需重新安装

    • 系统使用 apt 软件管理(Ubuntu 等)
      apt-get install libcurl4-openssl-dev
      
    • 系统使用 yum 软件管理(RedHat、Centos 等)
      yum install libcurl-devel
      

代码示例

  • 鉴权信息主要放在 HTTPRequest 对象中,但如果使用 AsyncHTTPClient 发送请求, 则会报 ('unsupported auth_mode %s', 'digest'),因此需要用 CurlAsyncHTTPClient 模块作为替代将请求发送出去。
from tornado.httpclient import AsyncHTTPClient, HTTPRequest
from tornado.curl_httpclient import CurlAsyncHTTPClient

req_url = ''
username = ''
password = ''
headers = {'Content-type': 'application/json'}

# 非鉴权,默认为 Get
resp = await AsyncHTTPClient().fetch(HTTPRequest(
	url=req_url,
	headers=headers
))

# 鉴权
# auth_username – HTTP 身份验证的用户名
# auth_password – HTTP 身份验证的密码
# auth_mode – 认证模式;默认为“Basic”。允许的值是实现定义的;
#  			– curl_httpclient 支持 “Basic” 和 “Digest”; 
# 			– simple_httpclient 只支持 “Basic”
resp = await CurlAsyncHTTPClient().fetch(HTTPRequest(
    url=req_url, 
    method='POST',
    auth_mode='digest' # 使用 digest 鉴权模式,
    auth_username=username # 鉴权账户名,
    auth_password=password # 鉴权密码、token 等,
    headers=headers
))

resp_content = json.loads(resp.body.decode('utf8'))
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱吃芒果的芬里尔狼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值