Python常用代码段

2017-12-05

记录常用的Python代码段

提取字符串中IP+端口格式内容

#!/usr/bin/env python
import re

def check_proxy_host(incoming_string):
    return re.findall('(\d+\.\d+\.\d+\.\d+)+[^0-9]+(\d+)', incoming_string, re.S)

if __name__ == '__main__':
    a = ' 192.161.1.3:1211 ' \
        '123.1.1.1:4123 ' \
        '20.2.2.4  24'
    b = '124.1234.124'
    a1 = check_proxy_host(a)
    b1 = check_proxy_host(b)
    print(a1)
    print(b1)

输出:

> python CheckProxy.py
[('192.161.1.3', '1211'), ('123.1.1.1', '4123'), ('20.2.2.4', '24')]
[]