【问题标题】:Python request.get() response different to response in browser or when proxied over burp suitePython request.get() 响应不同于浏览器中的响应或通过 burp 套件代理时的响应
【发布时间】:2021-04-12 10:30:41
【问题描述】:

我正在尝试使用 python 发送 get 请求,如下所示:

import requests

url = "internal_url"  # I replaced all internal urls
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0", "Accept": "*/*", "Accept-Language": "en-GB,en;q=0.5", "Accept-Encoding": "gzip, deflate", "X-Requested-With": "XMLHttpRequest", "Connection": "close", "Referer": "internal url"}
r = requests.get(url , headers=header)
print(r.text)

作为响应,我期待 json 数据。但相反,我得到了这个:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function getCookie(c_name) { // Local function for getting a cookie value
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start!=-1) {
        c_start=c_start + c_name.length + 1;
        c_end=document.cookie.indexOf(";", c_start);

        if (c_end==-1)
            c_end = document.cookie.length;

        return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}
function setCookie(c_name, value, expiredays) { // Local function for setting a value of a cookie
    var exdate = new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString()) + ";path=/";
}
function getHostUri() {
    var loc = document.location;
    return loc.toString();
}
setCookie('STRING redacted', IP-ADDRESS redacted, 10);
try {
    location.reload(false);
} catch (err1) {
    try {
        location.reload();
    } catch (err2) {
        location.href = getHostUri();
    }
}
</script>
</head>
<body>
<noscript>This site requires JavaScript and Cookies to be enabled. Please change your browser settings or upgrade your browser.</noscript>
</body>
</html>

当我将请求更改为使用 burp 套件代理以便我可以看到该请求时,它突然起作用并且我得到了正确的响应:

proxies = {"http": "127.0.0.1:8080", "https": "http://127.0.0.1:8080"}
r = requests.get(url, headers=headers, verify=False, proxies=proxies)

当我访问链接本身时,我的浏览器会以文本形式显示正确的结果。不需要 Burp 套件代理。

我认为这可能与公司代理有关。
但即使我尝试使用提供的公司代理运行请求,它仍然无法正常工作。
有什么我遗漏的吗?

编辑: 经过更多搜索后,当我在 python 中不使用任何代理时,我似乎被重定向了。当我通过 burp 套件代理时,不会发生这种情况。

【问题讨论】:

    标签: python proxy python-requests


    【解决方案1】:

    经过几天和一些外界的帮助,我终于找到了解决方案。在这里发布以备将来使用。

    我的问题是我使用的是partially qualified domain name 而不是fully qualified domain name

    例如:myhost 而不是 myhost.example.com

    Burp 套件或浏览器正在为我处理翻译,但在 python 中我必须自己做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-28
      • 2020-08-25
      • 2011-09-07
      • 1970-01-01
      • 1970-01-01
      • 2016-01-12
      • 1970-01-01
      • 2017-06-25
      相关资源
      最近更新 更多