Httplib is a client implementation of the HTTP protocol in Python that can be used to interact with the HTTP server.
As in Example 1:
Import Httplib
url = "Www.126.com"
conn = Httplib. Httpconnection (URL)
Try
Conn.request (' Get ', '/')
result = Conn.getresponse () #获取http响应
Print Result.read () #返回网页内容 (message body)
Print Result.status #返回响应的状态码
Print Result.reason #返回服务器请求的结果说明
Print Result.msg #返回响应头信息
Conn.close ()
Except
Print "sorry!"
Example 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
Python Httplib Learning