尝试获取网址状态时获取异常错误10054

时间:2017-11-01 12:58:06

标签: python urllib urlopen

我正在编写一个检查多个URL状态的脚本 但每次该网站的某个网站出现故障时我都会收到以下错误:

  

错误[错误号10054]现有连接被强行关闭   远程主机

脚本如下:

from urllib2 import Request, urlopen, HTTPError, URLError
import time
import tkMessageBox
import Tkinter as tk

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'
headers = {'User-Agent': user_agent}
sites = ["http://www.google.co.il",] #I got my internal site so i wrote here google's site

root = tk.Tk()
root.withdraw()

while 1 == 1:
    for link in sites:
        ta = 0
        req = Request(link, headers=headers)
        try:
            page_open = urlopen(req)
        except HTTPError, e:
            print link, "First time error"
            time.sleep(20)
            try:
                page_open = urlopen(req)
            except HTTPError, e:
                print link, e.code
                print tkMessageBox.showerror(link, link + "  Server is unreachable!!")
                time.sleep(1)
        except URLError, e:
            print link, "First time error"
            time.sleep(20)
            try:
                page_open = urlopen(req)
            except HTTPError, e:
                print link, e.reason
                print tkMessageBox.showerror(link, link + "  Server is unreachable!!")
                log = open("log.txt", "a")
                log.write(time.ctime() + " " + link + " Site is UNREACHABLE!\n")
                log.close()
                time.sleep(1)
        else:
            print link
            time.sleep(1)

我试图在论坛中找到解决方案,但没有成功。

0 个答案:

没有答案