
- Python - 網路程式設計
- Python - 網路入門
- Python - 網路環境
- Python - 網際網路協議
- Python - IP地址
- Python - DNS查詢
- Python - 路由
- Python - HTTP請求
- Python - HTTP響應
- Python - HTTP頭部
- Python - 自定義HTTP請求
- Python - 請求狀態碼
- Python - HTTP身份驗證
- Python - HTTP資料下載
- Python - 連線重用
- Python - 網路介面
- Python - 套接字程式設計
- Python - HTTP客戶端
- Python - HTTP伺服器
- Python - 構建URL
- Python - 網頁表單提交
- Python - 資料庫和SQL
- Python - Telnet
- Python - 郵件訊息
- Python - SMTP
- Python - POP3
- Python - IMAP
- Python - SSH
- Python - FTP
- Python - SFTP
- Python - Web伺服器
- Python - 上傳資料
- Python - 代理伺服器
- Python - 目錄列表
- Python - 遠端過程呼叫
- Python - RPC JSON伺服器
- Python - 谷歌地圖
- Python - RSS Feed
Python - 請求狀態碼
在接收和解釋請求訊息後,伺服器會返回一個HTTP響應訊息。響應訊息包含一個狀態碼。它是一個三位整數,狀態碼的第一位數字定義了響應的類別,後兩位數字沒有任何分類作用。第一位數字有5個值。
狀態碼
序號 | 程式碼和描述 |
---|---|
1 | 1xx: 資訊 表示請求已接收,正在處理中。 |
2 | 2xx: 成功 表示操作已成功接收、理解和接受。 |
3 | 3xx: 重定向 表示必須採取進一步操作才能完成請求。 |
4 | 4xx: 客戶端錯誤 表示請求包含不正確的語法或無法完成。 |
5 | 5xx: 伺服器錯誤 表示伺服器未能完成一個明顯有效的請求。 |
成功響應
在下面的示例中,我們從URL訪問檔案,響應成功。因此返回的狀態碼為200。
import urllib3 http = urllib3.PoolManager() resp = http.request('GET', 'https://tutorialspoint.tw/robots.txt') print resp.data # get the status of the response print resp.status
執行上述程式後,我們將得到以下輸出:
User-agent: * Disallow: /tmp Disallow: /logs Disallow: /rate/* Disallow: /cgi-bin/* Disallow: /videotutorials/video_course_view.php?* Disallow: /videotutorials/course_view.php?* Disallow: /videos/* Disallow: /*/*_question_bank/* Disallow: //*/*/*/*/src/* 200
不成功響應
在下面的示例中,我們訪問一個不存在的URL檔案。響應不成功。因此返回的狀態碼為403。
import urllib3 http = urllib3.PoolManager() resp = http.request('GET', 'https://tutorialspoint.tw/robot.txt') print resp.data # get the status of the response print resp.status
執行上述程式後,我們將得到以下輸出:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>403 Forbidden</title> </head><body> <h1>Forbidden</h1> <p>You don't have permission to access /robot.txt on this server.</p> </body></html> 403
廣告