AttributeError:'URLError'对象没有属性'read'[关闭]

问题描述 投票:0回答:1

当我运行应用程序时,我遇到以下问题:

AttributeError:'URLError'对象没有属性'read'

# Create a content length
not_there_response_content_length = len(not_there_response.read())

if not_there_response.getcode():
    print '[-]    URLNotThere -> HTTP Code: %s, Response Length: %s' % (not_there_response.getcode(), not_there_response_content_length)
    response_code['not_there_code'], response_code['not_there_length'] = not_there_response.getcode(), not_there_response_content_length
else:
    print '[+]    URLNotThere -> HTTP Code: %s, Error Code: %s' % (not_there_response.code, not_there_response.reason)
    response_code['not_there_code'], response_code['not_there_reason'] = not_there_response.code

# Check if we didn't get a 404. This would indicate custom error messages or some redirection and will cause issues later.
if response_code['not_there_code'] != 404:
    print bcolors.RED + '[!]  FALSE POSITIVE ALERT: We may have a problem determining real responses since we did not get a 404 back.' + bcolors.ENDC

错误:

D:\Python\python.exe D:/tilde_enum-master/tilde_enum.py -u https://IIS-SERVER-w ./exts
[-]  Testing with dummy file request https://IIS-SERVER/UytUikqdgn.htm
Traceback (most recent call last):
  File "D:/tilde_enum-master/tilde_enum.py", line 673, in <module>
    if __name__ == "__main__": main()
  File "D:/tilde_enum-master/tilde_enum.py", line 487, in main
    response_code = initialCheckUrl(args.url)
  File "D:/tilde_enum-master/tilde_enum.py", line 107, in initialCheckUrl
    not_there_response_content_length = len(not_there_response.read())
AttributeError: 'URLError' object has no attribute 'read'

完整的Python脚本:https://github.com/WebBreacher/tilde_enum

感谢您的时间和帮助!

python python-requests subprocess
1个回答
0
投票

您的getWebServerResponse函数有一个用于返回URLError对象而不是请求对象的子句。当代码尝试调用不是URLError对象的方法的read()时代码中断。

getWebServerResponse从你的链接获得。

def getWebServerResponse(url):
    # This function takes in a URL and outputs the HTTP response code and content length (or error)
    try:
        req = urllib2.Request(url, headers=custom_headers)
        if args.cookies:
            req.add_header('Cookie', args.cookies)
            req.add_header('Connection', 'keep-alive')
        response = urllib2.urlopen(req)
        return response
    except urllib2.URLError as e:
        return e
    except ssl.CertificateError as e:
        sys.exit(bcolors.RED + '[!]  SSL Certificate Error: try running again with --no-check-certificate' + bcolors.ENDC)
    except Exception as e:
return 0

您可能希望在调用其上的方法之前检查not_there_response的类型。一种懒惰的方法可以如下:

if isinstance(not_there_response, urllib2.URLError):
    # Response-error handling here
elif isinstance(not_there_response, urllib2.Request):
    # Create a content length
    not_there_response_content_length = len(not_there_response.read())

    if not_there_response.getcode():
       ... # rest of your code
© www.soinside.com 2019 - 2024. All rights reserved.