Python request.get() 响应与浏览器中的响应或通过 burp 套件代理时的响应不同
Python request.get() response different to response in browser or when proxied over burp suite
我正在尝试使用 python 发送获取请求,如下所示:
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)
当我访问 link 本身时,我的浏览器将正确的结果显示为文本。不需要 Burp 套件代理。
我觉得可能跟公司代理有关
但是即使我尝试 运行 提供公司代理的请求仍然无法正常工作。
有什么我想念的吗?
编辑:
经过更多搜索后,当我在 python 中不使用任何代理时,我似乎被重定向了。当我检查 burp 套件代理时,不会发生这种情况。
经过几天的努力和一些外部帮助,我终于找到了解决方案。为将来张贴在这里。
我的问题是我使用的是 partially qualified domain name
而不是 fully qualified domain name
例如:myhost
而不是 myhost.example.com
Burp 套件或浏览器正在为我处理翻译,但在 python 中我必须自己完成。
我正在尝试使用 python 发送获取请求,如下所示:
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)
当我访问 link 本身时,我的浏览器将正确的结果显示为文本。不需要 Burp 套件代理。
我觉得可能跟公司代理有关
但是即使我尝试 运行 提供公司代理的请求仍然无法正常工作。
有什么我想念的吗?
编辑: 经过更多搜索后,当我在 python 中不使用任何代理时,我似乎被重定向了。当我检查 burp 套件代理时,不会发生这种情况。
经过几天的努力和一些外部帮助,我终于找到了解决方案。为将来张贴在这里。
我的问题是我使用的是 partially qualified domain name
而不是 fully qualified domain name
例如:myhost
而不是 myhost.example.com
Burp 套件或浏览器正在为我处理翻译,但在 python 中我必须自己完成。