美文网首页
Python发送post请求实例

Python发送post请求实例

作者: SunStill | 来源:发表于2016-01-28 19:33 被阅读0次
    
    #!/usr/bin/env python
    #-*- coding: utf-8 -*-
    
    import httplib, urllib
    import json
    httpClient = None
    try:
        params = {'user':'ero','password':'em3'}  #python object
        print params[1]
        #x =json.JSONEncoder()
        #params_str = x.encode(params)   #python obj   to json(str)
        params_str = json.dumps(params)  #python obj   to json(str)
        print params_str
        headers = {'content-type': 'application/json;charset=UTF-8', 'Accept':'text/plain'}
    
        httpClient = httplib.HTTPConnection("172.28.125.159",21112, timeout=5)
        httpClient.request("POST", "/esmpro/api/login-session", params_str, headers)
    
        response = httpClient.getresponse()
        print response.status
        print response.reason
    
        print response.read()
    
        print response.getheaders()#取得返回值
        y =   response.getheaders() #python object
    
        print y[2]
       
        i= json.dumps(response.getheaders())
        print i
        n = json.loads(i)
        print n[0][0]
    
    
    except Exception, e:
        print e
    finally:
        if httpClient:
            httpClient.close()
    
    

    相关文章

      网友评论

          本文标题:Python发送post请求实例

          本文链接:https://www.haomeiwen.com/subject/korjkttx.html