【问题标题】:500 + Website Check in Python for multiple status500 + 网站在 Python 中检查多个状态
【发布时间】:2016-07-08 20:56:40
【问题描述】:
  • 我知道网址检查有多个问题。我很新 python所以试图从多个帖子中理解并搜索 新图书馆也有帮助。我正在努力为以下点工作 内部网站和外部网站。 :

       Status Code
       Status Description
       Response Length
       Time Taken 
       Websites are like ,, www.xyz.com , www.abc.log , www.abc.com/xxx/login.html and more combinations. Below is the
    

    初始代码..

    import socket
    from urllib2 import urlopen, URLError, HTTPError
    
    import urllib
    socket.setdefaulttimeout( 23 )  # timeout in seconds
    #print "---------URL----------", " ---Status Code---"
    url='https://www.google.com'
    
        try :
          response = urlopen( url )
        except HTTPError, e:
            print 'The server couldn\'t fulfill the request. Reason:', str(e.code)
            #Want to get code for that but its not showing
    
        except URLError, e:
            print 'We failed to reach a server. Reason:', str(e.reasonse)
            #Want to get code for that but its not showing
    
    
        else :
    
            code=urllib.urlopen(url).getcode()
            **#here getcode is working
            print url,"-------->", code
            #print 'got response!'
    
  • 我想先检查网站是否存在。然后会去 其余检查如上所述。如何组织这个工作 500 多个网址的所有上述要点。我需要从txt文件导入吗 ?还有一点我已经看到,如果 www.xyx.com 正在工作并且 www.xyz.com/lmn.html 不存在,它仍然显示 200 。

【问题讨论】:

    标签: python python-2.7 python-3.x url http-headers


    【解决方案1】:

    我认为您可以使用此代码显示页面:

    import httplib
    from urlparse import urlparse
    
    def chkUrl(url):
        p = urlparse(url)
        conn = httplib.HTTPConnection(p.netloc)
        conn.request('HEAD', p.path)
        resp = conn.getresponse()
        return resp.status < 400
    
    if __name__ == '__main__':
        print chkUrl('http://www.stackoverflow.com') # True
        print chkUrl('http://stackoverflow.com/notarealpage.html') # False
    

    【讨论】:

    • 好的。但是如何将它与我的代码以及我提到的要点结合起来。 .您的代码很好检查,如果网站存在与否。但我真的在寻找更多点:)。如果网络关闭,我想获取代码和状态。更类似于以下所有内容:状态代码状态描述响应长度所用时间
    • 你想知道服务器正在服务吗,我认为你需要 cURL,如果你得到响应,那就是。这里有 pycurl 的 url:pycurl.io
    • 我想用它的状态码和描述来检查 URL 是否已经启动。它的响应长度和时间。
    • 我想检查和打印那些东西。
    • 查看stackoverflow.com/questions/15968031/python-http-status-code此链接可能会对您有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    • 2013-10-17
    相关资源
    最近更新 更多