我知道已经有人说过了,但我强烈建议你使用
requestsPython软件包。
如果你使用的是除python之外的语言,则可能是在考虑
urllib并且
urllib2易于使用,代码不多且功能强大,这就是我以前的想法。但是该
requests程序包是如此有用且太短,以至于每个人都应该使用它。
首先,它支持完全宁静的API,并且非常简单:
import requestsresp = requests.get('http://www.mywebsite.com/user')resp = requests.post('http://www.mywebsite.com/user')resp = requests.put('http://www.mywebsite.com/user/put')resp = requests.delete('http://www.mywebsite.com/user/delete')无论是GET / POST,你都无需再次对参数进行编码,只需将字典作为参数即可。
userdata = {"firstname": "John", "lastname": "Doe", "password": "jdoe123"}resp = requests.post('http://www.mywebsite.com/user', data=userdata)加上它甚至还具有内置的JSON解码器(再次,我知道json.loads()编写的内容不多,但这很方便):
resp.json()
或者,如果你的响应数据只是文本,请使用:
resp.text
这只是冰山一角。这是请求站点中的功能列表:
- International Domains and URLs
- Keep-Alive & Connection Pooling
- Sessions with cookie Persistence
- Browser-style SSL Verification
- Basic/Digest Authentication
- Elegant Key/Value cookies
- Automatic Decompression
- Unipre Response Bodies
- Multipart File Uploads
- Connection Timeouts
- .netrc support
- List item
- Python 2.6—3.4
- Thread-safe.



