做一个接口查询时,碰到要在header中填写认证身份信息的,要怎么写代码呢?

做一个接口查询时,碰到要在header中填写认证身份信息的,要怎么写代码呢?在网络中找了好多资料,都找不到这个在header中添加认证信息的办法,都没有办法解决,用soapui获取的接口文档如下,请论坛里会的坛友指点一下,或者发一个有这个认证的例子连接给,谢谢了!

下面是我自己写的post请求,用的是requests库

讨论数量: 2
Jason990420

Maybe following code can help

from urllib import request, error
from urllib.parse import urlsplit
from base64 import b64encode, b64decode
import zlib
import brotli
from http import client


def read_URL(url, data=None, headers=None, encoding='utf-8', errors='ignore',
             user=None, password=None, byte=False):
    """
    Read text from URL
    Compress method for gzip, deflate, br dealed internally.
    :Parameter
      url     : string or a Request object.
      data    : an object specifying additional data to be sent, or None.
      headers : dictionary, header of Http request entity.
      encoding: name of encoding to convert bytes into string.
      errors  : error process, 'strict', 'ignore', 'replace', ...
      user    : string, user name.
      password: string, password.
      byte    : flag for not decoding by encoding
    :Return
      (None, None) if failed, else response, string of html content
    """
    if not headers:
        url_base = urlsplit(url).netloc
        headers = {
            'Accept': ('text/html,application/xhtml+xml,application/xml;q=0.9,'
                       'application/json,'
                       'image/webp,image/apng,*/*;q=0.8,application/signed-exc'
                       'hange;v=b3;q=0.9'),
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh-TW;q=0.9,zh;q=0.8,en;q=0.7',
            'Connection': 'keep-alive',
            'Host': url_base,
            'Sec-Fetch-Dest': 'document',
            'Sec-Fetch-Mode': 'navigate',
            'Sec-Fetch-Site': 'none',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': ('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWeb'
                           'Kit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.14'
                           '9 Safari/537.36')}
    if not (None in [user, password]):
        auth = str.encode("%s:%s" % (user, password))
        user_and_pass = b64encode(auth).decode("ascii")
        headers['Authorization'] = 'Basic %s' % user_and_pass
    try:
        req = request.Request(url, data=data, headers=headers)
    except:
        signal('ValueError')
        return None, None
    try:
        response = request.urlopen(req)
    except error.HTTPError as e:
        signal('HTTPError')
        return (None, None)
    except error.URLError as e:
        signal('URLError')
        return (None, None)
    if not str(response.status).startswith('2'):
        signal(client.responses[response.status])
        return (None, None)

    data = response.read()

    accept_encoding = response.headers['Content-Encoding']
    if accept_encoding == 'gzip':
        data = zlib.decompress(data, zlib.MAX_WBITS|16)
    elif accept_encoding == 'deflate':
        data = zlib.decompress(data, -zlib.MAX_WBITS)
    elif accept_encoding == 'br':
        data = brotli.decompress(data)
    html = data if byte else data.decode(encoding=encoding, errors=errors)

    return (response, html)
2周前 评论

感谢二楼的回复,测试了,没有返回值

2周前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!