• web服务器检测


    # coding=utf-8
    import sys
    import socket
    import re
    
    
    def check_webserver(address, port, resource):
        address = socket.gethostbyname(address)
    
        if not resource.startswith('/'):
            resource = '/' + resource
    
        request_string = 'GET %s HTTP/1.0
    
    ' % (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 False
    
    
    if __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)

     使用httplib

    def check_webserver_by_httplib(address, port, resource):
        address = socket.gethostbyname(address)
        failed = False
        if not resource.startswith('/'):
            resource = '/' + resource
        try:
            conn = httplib.HTTPConnection(address, port)
            print 'HTTP connection created successfully'
            # make request
            conn.request('GET', resource)
            print 'request for %s successful' % (resource)
            # get response
            resp = conn.getresponse()
            print 'response status: %s' % resp.status
        except socket.error as e:
            print 'HTTP connection failed: %s' % e
            return False
        finally:
            conn.close()
            print 'HTTP connection closed successfully'
    
        if resp.status in [200, 301]:
            return True
        else:
            return False
  • 相关阅读:
    python 安装 reportlab 报错 “ImportError: No module named reportlab.lib”
    人声和乐器的频谱范围
    C语言中指针中的值赋值给数组
    快速熟悉Matlab
    Ubuntu 16.04 下octave的使用入门
    IP达人启示录
    python常用的十进制、16进制之间的转换
    GNU 下命令objcopy 用法
    python zeros用法实例
    Debian/Ubuntu清理硬盘空间的8个技巧
  • 原文地址:https://www.cnblogs.com/hupeng1234/p/6715507.html
Copyright © 2020-2023  润新知