bigdata/chapter4/check_proxy.py

31 lines
892 B
Python

import requests
import time
import re
def check_alive_proxy(ip, port):
begin_time = int(time.time())
proxies = {
"http": "http://%s:%s" % (ip, port),
"https": "https://%s:%s" % (ip, port),
}
response = requests.get(
'http://2019.ip138.com/ic.asp', proxies=proxies, timeout=3)
check_time = int(time.time()) - begin_time
response.encoding = 'gb2312'
m = re.search(r'.+\[((\d+\.){3}\d+)\].+', response.text)
if m:
if m.group(1) == ip:
return check_time
raise RuntimeError("连接出错")
if __name__ == "__main__":
with open('results.csv') as f:
for line in f:
try:
check_time = check_alive_proxy(line.strip(), 9999)
if check_time<5:
print("%s:%d %d", line, 9999, check_time)
except Exception as e:
print(e)