python新手问httplib和urllib2有哪些不同,httpliburllib2,想从java转Pytho


想从java转Python,Python看了一个特别简单的教程,就想先上手试试。结果就卡了。。。
想调用github的OPEN API试试,搜了一下httpclient(显然java的名字,勿喷。。。),搜到了httplib。然后就照猫画虎写程序。见下。

#!/usr/bin/python#coding=utf8import httplibtry:    httpClient = httplib.HTTPConnection('https://api.github.com/', 80)    headers = {"Content-type":"application/json"}    param = None    httpClient.request('GET','/', param, headers)    response = httpClient.getresponse()    print response.statusexcept Exception, e:    print efinally:    if httpClient:        httpClient.close()

然后运行完了报错[Errno 11004] getaddrinfo failed
搜了很多,发现没有解决了的,只有改用urllib的。
这个倒是成功了。。。

#!/usr/bin/python#coding=utf8import urllib2import jsonresponse = urllib2.urlopen('https://api.github.com/')data = json.load(response) print data

我就特想知道httplib那段程序哪里错了。。。

====
采纳答案的评论里的内容:
httplib.HTTPSConnection('api.github.com/',443)

https 不是80端口,是443端口

pycurl,,,字数补丁

跑个题,您见到如此反人类的 API 设计,还有使用 urllib2 的欲望吗?
珍爱生命,远离 urllib2。(Requests 就不错)

可以考虑使用Requests这个库, 还是挺好用的

编橙之家文章,

评论关闭