????Python’s standard urllib2 module provides most of the HTTP capabilities you need?? but the API is thoroughly broken. It was built for a different time — and a different web. It requires an enormous amount of work (even method overrides) to perform the simplest of tasks.
????Things shouldn’t be this way. Not in Python.
????????Python??urllib2????????????????????????http??????????????????????Requests???????????????о???
???????Requests????λ??????????????????????????????????????Щ???????????????????????????м???????????Ч?????????????????JAVA????????????????????????????????????Pythoner??Requests????????????????????????????????DEMO:
>>> r = requests.get('https://api.github.com/user'?? auth=('user'?? 'pass'))
>>> r.status_code
200
>>> r.headers['content-type']
'application/json; charset=utf8'
>>> r.encoding
'utf-8'
>>> r.text
u'{"type":"User"...'
>>> r.json()
{u'private_gists': 419?? u'total_private_repos': 77?? ...}
????Requests???????JSON????????????????????:
>>> import requests
>>> r = requests.get('https://github.com/timeline.json')
>>> r.json()
[{u'repository': {u'open_issues': 0?? u'url': 'https://github.com/...
????????????header???????POST
>>> import json
>>> url = 'https://api.github.com/some/endpoint'
>>> payload = {'some': 'data'}
>>> headers = {'content-type': 'application/json'}
>>> r = requests.post(url?? data=json.dumps(payload)?? headers=headers)
??????????????λPythoner?????????????????????顣
????????????????????????API??EG??