python http客户端(requests)

来源:互联网 发布:数据采集系统解决方案 编辑:程序博客网 时间:2024/05/01 14:13

httpclient发送json对象

在header中要注明发送的内容'Content-Type' : 'application/json;charset=UTF-8',

dictHeaders = {'Accept':'application/json, text/plain, */*',
'Accept-Language' : 'zh-CN',
'Content-Type' : 'application/json;charset=UTF-8',
#'Content-Type' : 'application/x-www-form-urlencoded',
#'Accept-Encoding' : 'deflate',
'Connection' : 'Keep-Alive',
'User-Agent' : 'Mozilla/5.0 compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0',
#'User-Agent': 'Mozilla/4.0 compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E',
#'Referer': 'https://pbsz.ebank.cmbchina.com/CmbBank_CreditCardV2/UI/CreditCardPC/CreditCardV2_AccountManager/am_QueryReckoningSurveyNew.aspx',
'Pragma': 'no-cache'}


requests使用

class HttpTest(ApiBase):
    def POST(self):
        self.auth_or_redirect()
        kwargs = utils.get_body_object()
        resp = requests.request(**kwargs)
        result = {
            'status': resp.status_code,
            'data': resp.content,
            'headers': dict(resp.headers.items())
        }
        return json.dumps(result, ensure_ascii=False)

{"url":"http://api.sit.ffan.com/basicservice/v1/developers","method":"POST","params":"develop_id=C00000000001&develop_key=3a8253b975148c428f09c329666a4134","json":{"developer_name":"ffan","bizId":"1001001001"}}

request设置代理:

kwargs = {'url' : strUrl, 
             'method' : 'GET',
             'params' : dictParams,           
             'headers':  {},
             'proxies': {"http":"http://用户名:密码@IP:端口/"}
             }  

0 0