您的位置:首页 > 理论基础 > 计算机网络

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

2014-05-25 17:37 369 查看
#!/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