igd.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:igd-exporter 作者: yrro 项目源码 文件源码
def probe_metric(service_url, metric):
    '''
    Query the service at the given URL for the given metric value.

    Assumptions are made about the name of the method and output parameters
    which are only valid for the WanCommonInterfaceConfig service.
    '''
    envelope = E(QName(ns['s'], 'Envelope'), {QName(ns['s'], 'encodingStyle'): 'http://schemas.xmlsoap.org/soap/encoding/'})
    body = sE(envelope, QName(ns['s'], 'Body'))
    method = sE(body, QName(ns['i'], 'Get{}'.format(metric)))
    request_tree = ET(envelope)
    with io.BytesIO() as out:
        out.write(b'<?xml version="1.0"?>')
        request_tree.write(out, encoding='utf-8')
        out.write(b'\r\n') # or else my Belkin F5D8236-4 never responds...
        req = urllib.request.Request(service_url, out.getvalue())

    req.add_header('Content-Type', 'text/xml')
    req.add_header('SOAPAction', '"{}#{}"'.format(ns['i'], 'Get{}'.format(metric)))

    with urllib.request.urlopen(req) as result:
        result_tree = ElementTree.parse(result)
        return int(result_tree.findtext('.//New{}'.format(metric), namespaces=ns))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号