【问题标题】:python http status codepython http状态码
【发布时间】:2013-04-04 18:49:26
【问题描述】:

我正在用 python 编写自己的目录破坏器,并在安全可靠的环境中针对我的 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”的页面,但是当我手动浏览这些页面时,我发现它们已被永久移动或访问受限。出了点问题,但我不知道应该在哪里准确修复代码,感谢任何帮助..

【问题讨论】:

    标签: python http-status-codes httpconnection


    【解决方案1】:

    建议您使用http://docs.python-requests.org/en/latest/# 表示http。

    【讨论】:

      【解决方案2】:

      我没有发现您的代码有任何问题,只是它几乎无法阅读。我已经把它改写成这个工作的sn-p:

      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.
      

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

      【讨论】:

      • 非常感谢,我会使其更具可读性,然后我会尝试通过查看您的实现来解决问题。
      • 我正在尝试使用您刚刚向我展示的示例,当我向 Google 发出请求时,我总是收到:400 bad request while using your code 我得到了您在这篇文章中编写的状态代码..不知道出了什么问题..也许我在某个地方遗漏了一些东西..如果你想看看代码的核心部分在这里:link
      • 我拿了你的脚本并开始使用它,我改变了检索目录的方式,我使用了:“for directory in fileinput.input('utils/Directories_Common.wordlist') :" 因为我已经列出了该文件中的所有目录..通过此修改,我总是收到 400 错误请求...
      • 除非我确切地知道 'utils/Directories_Common.wordlist' 文件中的内容以及您得到的输出,否则我无法提供进一步的建议。您确定您的网址返回不同的代码吗?尝试使用以下方法对其进行测试:stackoverflow.com/a/6136861/325365
      • 该文件包含所有要查找的目录的列表,每行一个条目..
      猜你喜欢
      • 1970-01-01
      • 2018-10-28
      • 2010-10-24
      • 2011-04-06
      • 2015-12-29
      • 2010-12-15
      • 2014-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多