想从java转Python,Python看了一个特别简单的教程,就想先上手试试。结果就卡了。。。
想调用github的OPEN API试试,搜了一下httpclient(显然java的名字,勿喷。。。),搜到了httplib。然后就照猫画虎写程序。见下。
```python
#!/usr/bin/python
#coding=utf8
import httplib
try:
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.status
except Exception, e:
print e
finally:
if httpClient:
httpClient.close()
```
然后运行完了报错[Errno 11004] getaddrinfo failed
搜了很多,发现没有解决了的,只有改用urllib的。
这个倒是成功了。。。
```python
#!/usr/bin/python
#coding=utf8
import urllib2
import json
response = urllib2.urlopen('https://api.github.com/')
data = json.load(response)
print data
```
肿么回事。。。
想调用github的OPEN API试试,搜了一下httpclient(显然java的名字,勿喷。。。),搜到了httplib。然后就照猫画虎写程序。见下。
```python
#!/usr/bin/python
#coding=utf8
import httplib
try:
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.status
except Exception, e:
print e
finally:
if httpClient:
httpClient.close()
```
然后运行完了报错[Errno 11004] getaddrinfo failed
搜了很多,发现没有解决了的,只有改用urllib的。
这个倒是成功了。。。
```python
#!/usr/bin/python
#coding=utf8
import urllib2
import json
response = urllib2.urlopen('https://api.github.com/')
data = json.load(response)
print data
```
肿么回事。。。