zoukankan      html  css  js  c++  java
  • Python IP地址和端口的分割方法

    需求:要把输入的URL进行处理,并且取出IP地址和端口
    注意:此代码来源Tornado源码
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import re
    import urllib.parse
    from typing import Tuple, Optional
    
    # . 一次或多次,()结果整体为一个分组
    _netloc_re = re.compile(r"^(.+):(d+)$")
    
    
    def split_host_and_port(input_url: str) -> Tuple[str, Optional[int]]:
        """
            取出IP地址和端口 返回:`(host, port)` 元组
        """
        parsed_ret = urllib.parse.urlsplit(input_url)
        if parsed_ret.scheme not in ("http", "https"):
            raise ValueError("Unsupported url scheme: %s" % input_url)
        netloc = parsed_ret.netloc
        match = _netloc_re.match(netloc)
        if match:
            host = match.group(1)
            port = int(match.group(2))  # type: Optional[int]
        else:
            host = netloc
            if parsed_ret.scheme == 'http':
                port = 80
            elif parsed_ret.scheme == 'https':
                port = 443
            else:
                port = None
        return (host, port)
    
    if __name__ == '__main__':
        ret = split_host_and_port('https://www.cnblogs.com/liown/p/9927879.html')
        print(ret)  # ('www.cnblogs.com', 443)
  • 相关阅读:
    Linux-CentOS6.9启动流程排错
    jenkins+maven+svn 自动化部署
    Linux下Mysql5.6 二进制安装
    es的api
    es的QueryBuilder学习使用
    es的QueryBuilders使用
    安装vue的开发环境
    自定义组件
    mounted钩子函数,页面初始化完成此函数加载
    双亲委派机制
  • 原文地址:https://www.cnblogs.com/ygbh/p/14029979.html
Copyright © 2011-2022 走看看