python http urllib2的httplib,urllib和urllib2的区别及用

问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
想从java转Python,Python看了一个特别简单的教程,就想先上手试试。结果就卡了。。。
想调用github的OPEN API试试,搜了一下httpclient(显然java的名字,勿喷。。。),搜到了httplib。然后就照猫画虎写程序。见下。
#!/usr/bin/python
#coding=utf8
import httplib
httpClient = httplib.HTTPConnection('/', 80)
headers = {"Content-type":"application/json"}
param = None
httpClient.request('GET','/', param, headers)
response = httpClient.getresponse()
print response.status
except Exception, e:
if httpClient:
httpClient.close()
然后运行完了报错[Errno 11004] getaddrinfo failed
搜了很多,发现没有解决了的,只有改用urllib的。
这个倒是成功了。。。
#!/usr/bin/python
#coding=utf8
import urllib2
import json
response = urllib2.urlopen('/')
data = json.load(response)
print data
我就特想知道httplib那段程序哪里错了。。。
采纳答案的评论里的内容:
httplib.HTTPSConnection('/',443)
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
https 不是80端口,是443端口
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
跑个题,您见到如此反人类的 API 设计,还有使用 urllib2 的欲望吗?
珍爱生命,远离 urllib2。(Requests 就不错)
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
pycurl,,,字数补丁
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
可以考虑使用Requests这个库, 还是挺好用的
同步到新浪微博
分享到微博?
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
我要该,理由是:I am Hack SparrowCaptain of the Internets.
Posted on July 27th, 2011 under
Tags: , , , ,
What is the difference between urllib and urllib2 modules of Python?You might be intrigued by the existence of two separate URL modules in Python - urllib and urllib2. Even more intriguing: they are not alternatives for each other. So what is the difference between urllib and urllib2, and do we need them both?urllib and urllib2 are both Python modules that do URL request related stuff but offer different functionalities. Their two most significant differences are listed below:urllib2 can accept a
object to set the headers for a URL request, urllib accepts only a URL. That means, you cannot masquerade your User Agent string etc.urllib provides the
method which is used for the generation of GET query strings, urllib2 doesn't have such a function. This is one of the reasons why urllib is often used along with urllib2.For other differences between urllib and urllib2 refer to their documentations, the links are given in the References section.Tip: if you are planning to do HTTP stuff only, check out , it is much better than httplib or urllib or urllib2.ExerciseWhat is User Agent?What is the difference between GET and POST request methods?What is HTTP?References
Related to this post
Wordpress Hashcash needs javascript to work, but your browser has javascript disabled. Your comment will be queued in Akismet!拒绝访问 | lovesoo.org | 百度云加速
请打开cookies.
此网站 (lovesoo.org) 的管理员禁止了您的访问。原因是您的访问包含了非浏览器特征(3c70-ua98).
重新安装浏览器,或使用别的浏览器python httplib2 urllib区别_百度知道
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。
python httplib2 urllib区别
python 里面的httplib2 urllib2区别,这两个分别适用于什么情况?能举例子说明吗
功能上没什么区别吧,httlib2比urllib更进一步把,比如在长链接支持方面,运行速度方面更优越一点儿,适用情况差不多。个人感觉pycurl更强大一点。
采纳率:78%
来自团队:
为您推荐:
其他类似问题
您可能关注的内容
python的相关知识
等待您来回答若只使用python3.X, 下面可以不看了, 记住有个urllib的库就行了
python2.X 有这些库名可用:&,&,
urllib3,&, httplib2, requests
python3.X 有这些库名可用: urllib, urllib3, httplib2, requests
两者都有的urllib3和requests, 它们不是标准库. urllib3 提供线程安全连接池和文件post支持,与urllib及urllib2的关系不大.&&自称HTTP for Humans, 使用更简洁方便
对于python2.X:
urllib和urllib2的主要区别:
urllib2可以接受Request对象为URL设置头信息,修改用户代理,设置cookie等, urllib只能接受一个普通的URL.urllib提供一些比较原始基础的方法而urllib2没有这些, 比如 urlencode
urllib官方文档的几个例子
使用带参数的GET方法取回URL
&&& import urllib
&&& params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
&&& f = urllib.urlopen(&http://www./cgi-bin/query?%s& % params)
&&& print f.read()
使用POST方法
&&& import urllib
&&& params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
&&& f = urllib.urlopen(&http://www./cgi-bin/query&, params)
&&& print f.read()
使用HTTP代理,自动跟踪重定向
&&& import urllib
&&& proxies = {'http': ':8080/'}
&&& opener = urllib.FancyURLopener(proxies)
&&& f = opener.open(&http://www.python.org&)
&&& f.read()
不使用代理
&&& import urllib
&&& opener = urllib.FancyURLopener({})
&&& f = opener.open(&http://www.python.org/&)
&&& f.read()
urllib2的几个官方文档的例子:
GET一个URL
&&& import urllib2
&&& f = urllib2.urlopen('http:
&&& print f.read()
使用基本的HTTP认证
import urllib2
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(realm='PDQ Application',
uri='https:
user='klem',
passwd='kadidd!ehopper')
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
urllib2.urlopen('http:
build_opener() 默认提供很多处理程序, 包括代理处理程序, 代理默认会被设置为环境变量所提供的.
一个使用代理的例子
proxy_handler = urllib2.ProxyHandler({'http': 'http:
proxy_auth_handler = urllib2.ProxyBasicAuthHandler()
proxy_auth_handler.add_password('realm', 'host', 'username', 'password')
opener = urllib2.build_opener(proxy_handler, proxy_auth_handler)
opener.open('http:
添加HTTP请求头部
import urllib2
req = urllib2.Request('http:
req.add_header('Referer', 'http:
r = urllib2.urlopen(req)
更改User-agent
import urllib2
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
opener.open('http:
httplib 和 httplib2&httplib 是http客户端协议的实现,通常不直接使用, urllib是以httplib为基础 httplib2 是第三方库, 比httplib有更多特性
httplib比较底层,一般使用的话用urllib和urllib2即可
对于python3.X:
这里urllib成了一个包, 此包分成了几个模块,&
.request 用于打开和读取,
.error 用于处理前面引起的异常,
.parse 用于解析,
.robotparser用于解析.txt文件
python2.X 中的 urllib.urlopen()被废弃, urllib2.urlopen()相当于python3.X中的urllib.request.urlopen()
几个官方例子:
GET一个URL
&&& import urllib.request
&&& with urllib.request.urlopen('http://www.python.org/') as f:
print(f.read(300))
PUT一个请求
import urllib.request
DATA=b'some data'
req = urllib.request.Request(url='http://localhost:8080', data=DATA,method='PUT')
with urllib.request.urlopen(req) as f:
print(f.status)
print(f.reason)
基本的HTTP认证
import urllib.request
auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(realm='PDQ Application',
uri='https://mahler:8092/site-updates.py',
user='klem',
passwd='kadidd!ehopper')
opener = urllib.request.build_opener(auth_handler)
urllib.request.install_opener(opener)
urllib.request.urlopen('/login.html')
proxy_handler = urllib.request.ProxyHandler({'http': ':3128/'})
proxy_auth_handler = urllib.request.ProxyBasicAuthHandler()
proxy_auth_handler.add_password('realm', 'host', 'username', 'password')
opener = urllib.request.build_opener(proxy_handler, proxy_auth_handler)
opener.open('/login.html')
import urllib.request
req = urllib.request.Request('/')
req.add_header('Referer', 'http://www.python.org/')
r = urllib.request.urlopen(req)
更改User-agent
import urllib.request
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
opener.open('/')
使用GET时设置URL的参数
&&& import urllib.request
&&& import urllib.parse
&&& params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
&&& url = &http://www./cgi-bin/query?%s& % params
&&& with urllib.request.urlopen(url) as f:
print(f.read().decode('utf-8'))
使用POST时设置参数
&&& import urllib.request
&&& import urllib.parse
&&& data = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
&&& data = data.encode('ascii')
&&& with urllib.request.urlopen(&http://requestb.in/xrbl82xr&, data) as f:
print(f.read().decode('utf-8'))
&&& import urllib.request
&&& proxies = {'http': ':8080/'}
&&& opener = urllib.request.FancyURLopener(proxies)
&&& with opener.open(&http://www.python.org&) as f:
f.read().decode('utf-8')
不使用proxy, 覆盖环境变量的proxy
&&& import urllib.request
&&& opener = urllib.request.FancyURLopener({})
&&& with opener.open(&http://www.python.org/&) as f:
f.read().decode('utf-8')
python2.X中的httplib被重命名为 http.client
使用 2to3 工具转换源码时, 会自动处理这几个库的导入
总的来说, 使用python3, 记住只有urllib, 想要更简洁好用就用requests, 但不够通用&
本文已收录于以下专栏:
相关文章推荐
python2中,urllib和urllib2
都是接受URL请求的相关模块,但是提供了不同的功能。两个最显著的不同如下:
urllib2.urlopen accepts an instance of...
2.x版本的python可以直接使用import urllib来进行操作,但是3.x版本的python使用的是import urllib.request来进行操作,下面是简单的例子:=========...
可以通过 /p/urllib3/ 下载相关库和资料。
先列出使用方法:
# coding=utf8
import urllib3
import da...
urllib是python的一个获取url(Uniform Resource Locators,统一资源定址器)的模块。它用urlopen函数的形式提供了一个非常简洁的接口。这使得用各种各样的协议获取...
按照官方意思,这个模块最大的特点就是:
支持403转向
支持数据压缩
支持客户端证书
说是非常适合抓站。
文档地址:
http://urllib3.readthedocs.or...
最近在研究Python,熟悉了一些基本语法和模块的使用;现在打算研究一下Python爬虫。学习主要是通过别人的博客和自己下载的一下文档进行的,自己也写一下博客作为记录学习自己过程吧。Python代码写...
慢慢的把它们总结一下,总结就是最好的学习方法
首先来看一下他们的区别
urllib和urllib2
urllib 和urllib2都是接受URL请求的相关模块,但是urllib2可以接受一个R...
简单的python3 urllib3 多线程 抓取图片
接着上一篇,继续翻译
对应的是原文档的
Request Objects官方源文档地址:https://docs.python.org/3.5/library/urllib.re...
在python3.3后urllib2已经不能再用,只能用urllib.request来代替
如在python3.3.0中运行一下脚本
import urllib2response=urllib2....
他的最新文章
讲师:李江龙
讲师:司徒正美
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)}

我要回帖

更多关于 python httplib https 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信