python封装的GET&POST请求

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

    #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