python封装的GET&POST请求

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
    #coding=utf-8  
    import os  
    import urllib  
    import httplib  
    import requests  
    import json  
      
    def get_page_content(baseUrl, path, params, headers, type = 'get'):  
        try:  
            httpClient = httplib.HTTPConnection(baseUrl, 80, timeout=30)  
            if type == 'get':  
                httpClient.request('GET', path)  
                response = httpClient.getresponse()  
                print response.status  
                print response.reason  
                print response.read()  
            elif type == 'post':  
                params = urllib.urlencode(params)  
                httpClient.request("POST", path, params, headers)  
                response = httpClient.getresponse()  
                print response.status  
                print response.reason  
                print response.read()  
                print response.getheaders() #获取头信息  
            else:  
                print 'unsupport'  
        except Exception, e:  
            print Exception, e  
      
              
    def get_page_content_by_requests(url, payload, timeout, headers, type = 'get'):    
        try:  
            if type == 'get':  
                response = requests.get(url, params=payload, timeout=timeout)  
                response.encoding = 'gbk' #设定编码  
                print response.status_code  #返回状态  
                print response.text #返回字符结果  
                print response.content #返回二进制响应内容  
                print response.json() #获取Json响应内容  
                print response.raw #获取原始响应内容  
                print response.raise_for_status() #非200请况下抛出异常  
                print response.headers['Content-Type'] #获取响应头某个字段  
            elif type == 'post':  
                response = requests.post(url, data = json.dumps(payload), headers = headers, timeout=timeout)  
                response.encoding = 'gbk' #设定编码  
                print response.text  
            else:  
                print 'unsupport'  
        except Exception, e:  
            print Exception, e  

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:INI配置文件C#类

下一篇:iOS数据持久化