httplib — HTTP protocol client:
This module defines classes which implement the client side of the HTTP and HTTPS protocols. It is
normally not used directly — the module urllib uses it to handle URLs that use HTTP and HTTPS.
这是python API当中关于httplib的介绍,意思是:
这个组件实现了HTTP和HTTPS的客户端协议,它通常不被直接使用----urilib使用它控制URLs用于HTTP和HTTPS
urllib和urllib2:
urllib 和urllib2实现的功能大同小异,但urllib2要比urllib功能等各方面更高一个层次。目前的HTTP访问大部分都使用urllib2来进行访问
urllib2:
req = urllib2.Request('http://wwty.iteye.com')
response = urllib2.urlopen(req)
the_page = response.read()
FTP同样:
req = urllib2.Request('ftp://wwty.iteye.com')
urlopen返回的应答对象response有两个很有用的方法info()和geturl()
geturl -- 这个返回获取的真实的URL,这个很有用,因为urlopen(或者opener对象使用的)或许
会有重定向。获取的URL或许跟请求URL不同。
Data数据
有时候你希望发送一些数据到URL
post方式:
values ={'body' : 'test short talk','via':'xxxx'}
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
get方式:
data['name'] = 'Somebody Here'
data['location'] = 'Northampton'
data['language'] = 'Python'
url_values = urllib.urlencode(data)
url = 'http://www.example.com/example.cgi'
full_url = url + '?' + url_values
data = urllib2.open(full_url)
URLError--HTTPError:
from urllib2 import Request, urlopen, URLError, HTTPError
req = Request(someurl)
try:
response = urlopen(req)
except HTTPError, e:
print 'Error code: ', e.code
except URLError, e:
print 'Reason: ', e.reason
else:
.............
或者:
from urllib2 import Request, urlopen, URLError
req = Request(someurl)
try:
response = urlopen(req)
except URLError, e:
if hasattr(e, 'reason'):
print 'Reason: ', e.reason
elif hasattr(e, 'code'):
print 'Error code: ', e.code
else:
.............
通常,URLError在没有网络连接(没有路由到特定服务器),或者服务器不存在的情况下产生
异常同样会带有"reason"属性,它是一个tuple,包含了一个错误号和一个错误信息
req = urllib2.Request('http://www.pretend_server.org')
try:
urllib2.urlopen(req)
except URLError, e:
print e.reason
print e.code
print e.read()
最后需要注意的就是,当处理URLError和HTTPError的时候,应先处理HTTPError,后处理URLError
Openers和Handlers:
opener使用操作器(handlers)。所有的重活都交给这些handlers来做。每一个handler知道
怎么打开url以一种独特的url协议(http,ftp等等),或者怎么处理打开url的某些方面,如,HTTP重定向,或者HTTP
cookie。
默认opener有对普通情况的操作器 (handlers)- ProxyHandler, UnknownHandler, HTTPHandler,
HTTPDefaultErrorHandler, HTTPRedirectHandler, FTPHandler, FileHandler, HTTPErrorProcessor.
再看python API:Return an OpenerDirector instance, which chains the handlers in the order given
这就更加证明了一点,那就是opener可以看成是一个容器,这个容器当中放的是控制器,默认的,容器当中有五个控制
器,程序员也可以加入自己的控制器,然后这些控制器去干活。
class HTTPHandler(AbstractHTTPHandler):
def http_open(self, req):
return self.do_open(httplib.HTTPConnection, req)
http_request = AbstractHTTPHandler.do_request_
HTTPHandler是Openers当中的默认控制器之一,看到这个代码,证实了urllib2是借助于httplib实现的,同时也证实了Openers和Handlers的关系。
分享到:
相关推荐
使用urllib2,太强大了 试了下用代理登陆拉取cookie,跳转抓图片…… 文档:http://docs.python.org/library/urllib2.html 直接上demo代码了 包括:直接拉取,使用Reuqest(post/get),使用代理,cookie,跳转处理 #!/...
httplib2的使用例子,python3的,很实用。比标准的urllib更抽象
– urllib2 – httplib2 – pycurl – requests 1.urllib2 import urllib2, urllib github_url = 'https://api.github.com/user/repos' password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm() ...
opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler) urllib2.install_opener(opener) # 安装opener,此后调用urlopen()时都会使用安装过的opener对象 response = urllib2.urlopen(url) 3.2 ...
urllib、urllib2、urllib3、httplib、httplib2 都是和 HTTP 相关的 Python 模块,看名字就觉得很反人类,更糟糕的是这些模块在 Python2 与 Python3 中有很大的差异,如果业务代码要同时兼容 2 和 3,写起来会让人...
6.httplib2 – 网络库。 7.RoboBrowser – 一个简单的、极具Python风格的Python库,无需独立的浏览器即可浏览网页。 8.MechanicalSoup -一个与网站自动交互Python库。 9.mechanize -有状态、可编程的Web浏览库。 10....
M2加密 维护者: ... 对Python的httplib,urllib和xmlrpclib的HTTPS扩展。 用于Web会话管理的不可伪造的HMAC'ing AuthCookies 。 FTP / TLS客户端和服务器。 S / MIME v2 。 ZServerSSL : Zope的H
使用纯套接字调用和它自己的HTTP解析器(它不基于httplib或urllib2) 将HTTP资源映射到Python对象 即时阅读和发送 重用连接 和支持 两种方式均支持。 支持和 。 多部分形式和url编码形式 流媒体支持 代理处理 ...
keywords:python,http,multi-threads,thread,threading,httplib,urllib,urllib2,Queue,http pool,httppool 废话少说,上源码: # -*- coding:utf-8 -*- import urllib, httplib import thread import time fr
推荐用urllib, urllib2, httplib2. HTTPConnection 对象 class httplib.HTTPConnection(host[, port[, strict[, timeout[, source_address]]]]) 创建HTTPConnection对象 HTTPConnection.request(method, url[, body...
import httplib,urllib #加载模块 #urllib可以打开网站去拿 #res = urllib.urlopen('http://baidu.com'); #print res.headers #定义需要进行发送的数据 params = urllib.urlencode({'param':'6'}); #定义一些文件头 ...
相信做过自动化运维的同学都用过REST API接口来完成某些动作。API是一套成熟系统所必需的接口,可以被其他系统或脚本来调用,这也是自动化运维的必修课。 本文主要介绍python中...password_manager = urllib2.HTTPPass
python-twitter-api #!/ usr / bin / python2.4##版权所有2007 Python-Twitter开发人员##根据Apache... #请参阅许可证,以了解许可证下特定的语言管理权限和限制。 '''提供与Twitter API的Python接口的库'''
下面python代码通过urllib2抓取指定的url的内容,并且使用自定义的user-agent,可防止网站屏蔽采集器 import urllib2 req = urllib2.Request('http://192.168.1.2/') req.add_header('User-agent', 'Mozilla 5.10')...
复制代码 代码如下: ...说白了这些方式都是封装了curl库的,另外还有一些关于http的模块,例如perl的LWP,python的httplib,urllib或者urllib2 也可以去实现,今天就走的比较低级一些,就使用socket方式来进行
闲暇之余,在家里自建了个服务器,因为用的小区宽带,IP位动态分配...import httplib, urllib, urllib2 import time import sys,os import re import json username = 'xxxx' #账号 password = 'xxx' #密码 format = 'j
') From 网页: urllib urllib2 httplib httplib2 import urllib import re dBytes = urllib.request.urlopen('http://aaa.bbb.ccc/page').read() dStr = dBytes.decode() #在python3中urllib.read() 语句功能是将...
import urllib,urllib2,httplib,cookielib def auto_login_hi(url,name,pwd): url_hi="http://passport.baidu.com/?login" #设置cookie cookie=cookielib.CookieJar() cj=urllib2.HTTPCookieProcessor(co