Python HTTP状态码

2024-05-15 01:38:46 发布

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

我正在用python编写自己的目录buster,并在一个安全的环境中对我的web服务器进行测试。这个脚本基本上试图从给定的网站检索公共目录,通过查看响应的HTTP状态代码,它能够确定页面是否可访问。
首先,脚本读取包含要查找的所有感兴趣目录的文件,然后按以下方式发出请求:

for dir in fileinput.input('utils/Directories_Common.wordlist'):

    try:
        conn = httplib.HTTPConnection(url)
        conn.request("GET", "/"+str(dir))
        toturl = 'http://'+url+'/'+str(dir)[:-1]
        print '    Trying to get: '+toturl
        r1 = conn.getresponse()
        response = r1.read()
        print '   ',r1.status, r1.reason
        conn.close()

然后,解析响应,如果返回等于“200”的状态代码,则可以访问该页。我已经通过以下方式实现了所有这些:

if(r1.status == 200):
    print '\n[!] Got it! The subdirectory '+str(dir)+' could be interesting..\n\n\n'

在我看来,除了脚本标记为实际不可访问的页面之外,其他一切似乎都很好。实际上,算法只收集返回“200 OK”的页面,但当我手动浏览这些页面时,发现它们已被永久移动或访问受限。出了点问题,但我找不到我应该在哪里确切地修复代码,任何帮助都是感激的。。


Tags: 代码目录脚本url状态statusdir方式
2条回答

我没有发现你的代码有任何问题,只是它几乎不可读。我已经把它改写成这个工作片段:

import httplib

host = 'www.google.com'
directories = ['aosicdjqwe0cd9qwe0d9q2we', 'reader', 'news']

for directory in directories:
    conn = httplib.HTTPConnection(host)
    conn.request('HEAD', '/' + directory)

    url = 'http://{0}/{1}'.format(host, directory)
    print '    Trying: {0}'.format(url)

    response = conn.getresponse()
    print '    Got: ', response.status, response.reason

    conn.close()

    if response.status == 200:
        print ("[!] The subdirectory '{0}' "
               "could be interesting.").format(directory)

输出:

$ python snippet.py
    Trying: http://www.google.com/aosicdjqwe0cd9qwe0d9q2we
    Got:  404 Not Found
    Trying: http://www.google.com/reader
    Got:  302 Moved Temporarily
    Trying: http://www.google.com/news
    Got:  200 OK
[!] The subdirectory 'news' could be interesting.

另外,我确实使用了HEADHTTP请求而不是GET,因为如果您不需要内容并且只对状态代码感兴趣,那么它会更有效。

我建议您使用http://docs.python-requests.org/en/latest/#进行http。

相关问题 更多 >

    热门问题