国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Python erh?lt den Statuscode der HTTP-Anfrage (200, 404 usw.)
歐陽(yáng)克
歐陽(yáng)克 2017-06-28 09:25:31
0
2
1241

Python ruft den Statuscode der HTTP-Anfrage (200, 404 usw.) ab, ohne auf den gesamten Quellcode der Seite zuzugreifen, was eine Verschwendung von Ressourcen darstellt:

輸入:segmentfault.com 輸出:200
輸入:segmentfault.com/nonexistant 輸出:404
歐陽(yáng)克
歐陽(yáng)克

溫故而知新,可以為師矣。 博客:www.ouyangke.com

Antworte allen(2)
ringa_lee

參考文章:Python實(shí)用腳本清單

http不只有get方法(請(qǐng)求頭部+正文),還有head方法,只請(qǐng)求頭部

import httplib

def get_status_code(host, path="/"):
    """ This function retreives the status code of a website by requesting
        HEAD data from the host. This means that it only requests the headers.
        If the host cannot be reached or something else goes wrong, it returns
        None instead.
    """
    try:
        conn = httplib.HTTPConnection(host)
        conn.request("HEAD", path)
        return conn.getresponse().status
    except StandardError:
        return None
        
print get_status_code("segmentfault.com") # prints 200
print get_status_code("segmentfault.com", "/nonexistant") # prints 404
劉奇

你用get請(qǐng)求就會(huì)請(qǐng)求整個(gè)頭部+正文, 可以試下head方法, 直接訪問(wèn)頭部!

import requests
html = requests.head('http://segmentfault.com')    # 用head方法去請(qǐng)求資源頭部
print html.status_code  # 狀態(tài)碼

html = requests.head('/nonexistant')   # 用head方法去請(qǐng)求資源頭部
print html.status_code   # 狀態(tài)碼

# 輸出:
200
404
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage