Python: http查询(GET,POST)简单代码

#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
    版权所有 (c) 2014 yao_yu (http://blog.csdn.net/yao_yu_126)
    本代码采用MIT许可

    http助手
'''

import http.client
from functools      import partial

__all__ = ['url_get_simple', 'url_post_simple']

def __url_request_simple_impl(host, url, method, coding='gb2312'):
    conn = http.client.HTTPConnection(host)
    conn.request(method, url)
    response = conn.getresponse()
    body = response.read()
    return body.decode(coding)

# 使用GET查询数据
url_get_simple = partial(__url_request_simple_impl, method = 'GET')
# 使用POST查询数据
url_post_simple = partial(__url_request_simple_impl, method = 'POST')

if __name__ == '__main__':
    print(len(url_get_simple('www.baidu.com', '/', coding='utf8')))
    print(len(url_get_simple('quote.eastmoney.com', '/')))
    print(len(url_get_simple('quote.eastmoney.com', '/stocklist.html', coding = 'GBK')))

你可能感兴趣的:(Python)