2014-07-07 40 views

回答

1

好吧,假設該網站是在頭標籤提供的服務器,你可以使用Python的request框架來獲取服務器頭:

requests.get("website").headers["server"] 

如果你不知道,如果服務器將返回服務器,請確保在訪問字典之前檢查它。網站也可能動態地更改標題值(尤其是服務器標題)。許多服務器也考慮爲服務器提供一個安全漏洞。所以,不要指望每個網站都給你它的服務器類型。服務器可能會突然隱藏它。一個更好的例子,打印服務器爲多個網站是:

import requests 

def getServer(uri, default = "-- Server not given --"): 
    request = requests.get(uri) 
    if "server" in request.headers: 
     return request.headers["server"] 
    else: 
     return default 

if __name__ == "__main__": 
    uris = [ 
     "http://superuser.com", 
     "http://google.com", 
     "http://docs.python-requests.org/en/latest/", 
     "http://yahoo.com" 
    ] 

    for theURI in uris: 
     print "Website: %s uses %s" % (theURI, getServer(theURI)) 

在我的機器,截至7月7日,2014年腳本返回:

Website: http://superuser.com uses -- Server not given -- 
Website: http://google.com uses gws 
Website: http://docs.python-requests.org/en/latest/ uses nginx/1.4.6 (Ubuntu) 
Website: http://yahoo.com uses ATS