Python httplib模块使用实例


Posted in Python onApril 11, 2015

httplib模块是一个底层基础模块,实现的功能比较少,正常情况下比较少用到.推荐用urllib, urllib2, httplib2.

HTTPConnection 对象

class httplib.HTTPConnection(host[, port[, strict[, timeout[, source_address]]]])

创建HTTPConnection对象

HTTPConnection.request(method, url[, body[, headers]])

发送请求

HTTPConnection.getresponse()

获得响应

HTTPResponse对象

HTTPResponse.read([amt])
Reads and returns the response body, or up to the next amt bytes.

HTTPResponse.getheader(name[, default])

获得指定头信息

HTTPResponse.getheaders()

获得(header, value)元组的列表

HTTPResponse.fileno()

获得底层socket文件描述符

HTTPResponse.msg

获得头内容

HTTPResponse.version

获得头http版本

HTTPResponse.status

获得返回状态码

HTTPResponse.reason

获得返回说明

实例

#!/usr/bin/python

import httplib
conn = httplib.HTTPConnection("3water.com")

conn.request("GET", "/")

r1 = conn.getresponse()
print r1.status, r1.reason

print '-' * 40
headers = r1.getheaders()

for h in headers:

    print h

print '-' * 40
print r1.msg

输出:

200 OK

----------------------------------------

('content-length', '106883')

('accept-ranges', 'bytes')

('vary', 'Accept-Encoding, Accept-Encoding')

('keep-alive', 'timeout=20')

('server', 'ngx_openresty')

('last-modified', 'Fri, 10 Apr 2015 09:30:10 GMT')

('connection', 'keep-alive')

('etag', '"55279822-1a183"')

('date', 'Fri, 10 Apr 2015 09:48:15 GMT')

('content-type', 'text/html; charset=utf-8')

----------------------------------------

Server: ngx_openresty

Date: Fri, 10 Apr 2015 09:48:15 GMT

Content-Type: text/html; charset=utf-8

Content-Length: 106883

Connection: keep-alive

Keep-Alive: timeout=20

Vary: Accept-Encoding

Last-Modified: Fri, 10 Apr 2015 09:30:10 GMT

Vary: Accept-Encoding

ETag: "55279822-1a183"

Accept-Ranges: bytes
Python 相关文章推荐
Python Web框架Flask下网站开发入门实例
Feb 08 Python
Python调用C++程序的方法详解
Jan 24 Python
python机器学习之神经网络(二)
Dec 20 Python
浅析python协程相关概念
Jan 20 Python
详解Python中的分组函数groupby和itertools)
Jul 11 Python
python绘制简单彩虹图
Nov 19 Python
Python面向对象基础入门之编码细节与注意事项
Dec 11 Python
通过python爬虫赚钱的方法
Jan 29 Python
NumPy排序的实现
Jan 21 Python
解决keras使用cov1D函数的输入问题
Jun 29 Python
python基础之匿名函数详解
Apr 21 Python
Python中time标准库的使用教程
Apr 13 Python
初步探究Python程序的执行原理
Apr 11 #Python
Python与shell的3种交互方式介绍
Apr 11 #Python
Python函数参数类型*、**的区别
Apr 11 #Python
Python中的多重装饰器
Apr 11 #Python
Python中的各种装饰器详解
Apr 11 #Python
将Django使用的数据库从MySQL迁移到PostgreSQL的教程
Apr 11 #Python
Python返回真假值(True or False)小技巧
Apr 10 #Python
You might like
php数组中删除元素的实现代码
2012/06/22 PHP
php的mail函数发送UTF-8编码中文邮件时标题乱码的解决办法
2015/10/20 PHP
ThinkPHP3.2.3实现分页的方法详解
2016/06/03 PHP
解析JavaScript中的标签语句
2013/06/19 Javascript
JS 实现图片直接下载示例代码
2013/07/22 Javascript
基于jQuery实现下拉框
2014/11/24 Javascript
js中document.write的那点事
2014/12/12 Javascript
JavaScript中的事件委托及好处
2016/07/12 Javascript
详解使用fetch发送post请求时的参数处理
2017/04/05 Javascript
JavaScript数据结构之广义表的定义与表示方法详解
2017/04/12 Javascript
JS中判断某个字符串是否包含另一个字符串的五种方法
2018/05/03 Javascript
NodeJS搭建HTTP服务器的实现步骤
2018/10/12 NodeJs
如何让Nodejs支持H5 History模式(connect-history-api-fallback源码分析)
2019/05/30 NodeJs
比较详细Python正则表达式操作指南(re使用)
2008/09/06 Python
决策树的python实现方法
2014/11/18 Python
Python中模拟enum枚举类型的5种方法分享
2014/11/22 Python
用python写的一个wordpress的采集程序
2016/02/27 Python
python通过cookie模拟已登录状态的初步研究
2016/11/09 Python
浅谈flask中的before_request与after_request
2018/01/20 Python
python实现创建新列表和新字典,并使元素及键值对全部变成小写
2019/01/15 Python
使用Python自动化破解自定义字体混淆信息的方法实例
2019/02/13 Python
Python 3.6 -win64环境安装PIL模块的教程
2019/06/20 Python
Python中常见的数制转换有哪些
2020/05/27 Python
手把手教你如何用Pycharm2020.1.1配置远程连接的详细步骤
2020/08/07 Python
Hotels.com中国区:好订网
2016/08/18 全球购物
阿巴庭院:Abba Patio
2019/06/18 全球购物
乌克兰在线商店的价格比较:Price.ua
2019/07/26 全球购物
武汉东之林科技有限公司机试
2013/09/17 面试题
急诊科护士自我鉴定
2013/10/14 职场文书
药学专业大学生个人的自我评价
2013/11/04 职场文书
西安交大自主招生自荐信
2014/01/27 职场文书
会计专业毕业自荐书范文
2014/02/08 职场文书
《颐和园》教学反思
2014/02/26 职场文书
格列佛游记读书笔记
2015/06/30 职场文书
HR必备:销售经理聘用合同范本
2019/08/21 职场文书
python字符串常规操作大全
2021/05/02 Python