基於socket的web伺服器檢測

來源:互聯網
上載者:User

標籤:ret   pre   add   with   art   res   基於   split   byte   

 

# coding=utf-8import sysimport socketimport redef check_webserver(address, port, resource):    address = socket.gethostbyname(address)    if not resource.startswith(‘/‘):        resource = ‘/‘ + resource    request_string = ‘GET %s HTTP/1.0\r\n\r\n‘ % (resource)    print ‘HTTP request: |||%s||||‘ % request_string    s = socket.socket()    failed = False    print ‘Attempting to connect to %s on port %s‘ % (address, port)    try:        s.connect((address, port))        print ‘Connected to %s on port %s‘ % (address, port)        s.send(request_string)        resp = s.recv(100)        print ‘Received 100 bytes of HTTP response‘        print ‘|||%s|||‘ % resp    except socket.error as e:        print ‘Connection to %s on port %s failed: %s‘ % (address, port, e)        failed = True    finally:        print ‘Close the connection‘        s.close()    if failed:        return False    lines = resp.splitlines()    print ‘First line of HTTP response: %s‘ % lines[0]    try:        version, status, message = re.split(‘\s+‘, lines[0], 2)    except ValueError:        print ‘Failed to split status line‘        return False    if status in [‘200‘, ‘301‘]:        print ‘Success - status was %s‘ % status        return True    else:        print ‘Failure - Status was %s‘ % status        return Falseif __name__ == ‘__main__‘:    from argparse import ArgumentParser    parser = ArgumentParser(description=u"基於socket的web伺服器檢測")    parser.add_argument(        ‘-a‘,        ‘--address‘,        dest="address",        default="localhost",        help="address for the server")    parser.add_argument(        "-p",        "--port",        dest="port",        type=int,        default=80,        help="port for the server")    parser.add_argument(        "-r",        ‘--resource‘,        dest=‘resource‘,        default="index.html",        help="resouce to check")    args = parser.parse_args()    check = check_webserver(args.address, args.port, args.resource)    print ‘check_webserver returned: %s‘ % check    sys.exit(not check)

 

基於socket的web伺服器檢測

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.