Python httplib学习
httplib是python中http协议的客户端实现,可以使用该模块与HTTP服务器进行交互。
如示例1:
import httplib
url = "www.126.com"
conn = httplib.HTTPConnection(url)
try:
conn.request('get','/')
result = conn.getresponse() #获取http响应
print result.read() #返回网页内容(消息体)
print result.status #返回响应的状态码
print result.reason #返回服务器请求的结果说明
print result.msg #返回响应头信息
conn.close()
except:
print "Sorry!"
示例2:
import httplib
conn = httplib.HTTPConnection('www.qq.cn',80,False)
conn.request('get','/',headers = {"Host":"www.qq.com",\
"User-Agent":"Mozilla/5.0 ((Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1)\
Gecko/20090624 Firefox/3.5","Accept":"text/plain"})
res = conn.getresponse()
print 'version:',res.version
print 'msg',res.msg
print 'status',res.status
conn.close