如果主机在5秒内没有响应,如何关闭连接

2024-04-24 16:07:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在试着写一个程序,它将检查哪些代理是活动的。 当我的脚本尝试连接到非活动代理时,它甚至需要大约30秒。当我检查数千个代理的列表时,脚本的工作时间会增加几个小时。你知道吗

如果响应时间超过5秒,是否可能中断此功能?你知道吗

def get(url, proxy):
proxies = {
    'http': 'http://'+proxy,
    'https': 'https://'+proxy
}
s = requests.Session()
s.headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
s.proxies = proxies
r = s.get(url)
return [r.status_code, r.reason, r.text]

with open('proxy.txt') as ips:
for ip in ips:
    ip = ip.split('\n', 1)[0]
    try:
        get(url, ip)
        with open('working.txt', 'a') as the_file:
            the_file.write(ip+'\n')
    except:
        print("error")

谢谢你。你知道吗


Tags: httpsiptxt脚本httpurl代理get
2条回答

处理凌乱的HTML是凌乱的。使用HTML cleaner实用程序,例如HtmlCleaner来完成这项工作

public static void main(String[] args) {
    String str = "This is the!@* text to be in words.";
    str = str.replaceAll("[^a-zA-Z0-9 ]+", "");
    System.out.println(str);
    int len = str.split("\\s+").length;
    System.out.println(len);
}

我假设特殊字符不是字母和数字

输出:

This is the text to be in words
8

相关问题 更多 >