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 "", line 2, in <module>
            File "", 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'))

你可能感兴趣的:(Python,Tornado,tornado,linux,python)