检测浏览器或脚本的简单方法

时间:2022-12-21 21:16:52

Complexities aside, what is the simplest quirty-and-dirty way to detect in a request whether that request was issues by a CLI program, such as curl, or whether it was by a browser? Here is what I'm trying to figure out:

除了复杂性之外,在请求中检测该请求是否是CLI程序发出的最简单的quirty-dirty方法是什么,例如curl,或者是否是浏览器?这是我想弄清楚的:

def view(request):
    if request.is_from_browser:
        return HTML_TEMPLATE
    else:
        return JSON

2 个解决方案

#1


0  

Request.is_ajax() checks if the HTTP_X_REQUESTED_WITH header equals XMLHttpRequest. This is becoming an "industry standard" among web frameworks/libraries to separate Ajax calls from normal requests. But it depends on cooperation from the client side to actually set the header. There's no 100 % foolproof way of detecting browser, client, Ajax etc without this cooperation.

Request.is_ajax()检查HTTP_X_REQUESTED_WITH标头是否等于XMLHttpRequest。这正在成为Web框架/库中的“行业标准”,以将Ajax调用与正常请求分开。但这取决于客户端的合作实际设置标头。没有这种合作,没有100%万无一失的方法来检测浏览器,客户端,Ajax等。

Btw, why do you need to know what's calling?

顺便问一下,为什么你需要知道在叫什么?

#2


0  

Somthing in the HTTP request headers, I'd first try using the Accept header. with the accept header the client can specify what sort of content it wants.this puts the responsibily on the client.

在HTTP请求标头中,我首先尝试使用Accept标头。使用accept标头,客户端可以指定它想要的内容类型。这将责任放在客户端上。

#1


0  

Request.is_ajax() checks if the HTTP_X_REQUESTED_WITH header equals XMLHttpRequest. This is becoming an "industry standard" among web frameworks/libraries to separate Ajax calls from normal requests. But it depends on cooperation from the client side to actually set the header. There's no 100 % foolproof way of detecting browser, client, Ajax etc without this cooperation.

Request.is_ajax()检查HTTP_X_REQUESTED_WITH标头是否等于XMLHttpRequest。这正在成为Web框架/库中的“行业标准”,以将Ajax调用与正常请求分开。但这取决于客户端的合作实际设置标头。没有这种合作,没有100%万无一失的方法来检测浏览器,客户端,Ajax等。

Btw, why do you need to know what's calling?

顺便问一下,为什么你需要知道在叫什么?

#2


0  

Somthing in the HTTP request headers, I'd first try using the Accept header. with the accept header the client can specify what sort of content it wants.this puts the responsibily on the client.

在HTTP请求标头中,我首先尝试使用Accept标头。使用accept标头,客户端可以指定它想要的内容类型。这将责任放在客户端上。