美文网首页
Httplib用法小结

Httplib用法小结

作者: N景波 | 来源:发表于2017-01-20 16:42 被阅读0次

    Http请求

    带端口的Host地址的Get请求:

    import httplib
    BODY = "***filecontents***"
    conn = httplib.HTTPConnection("IP String", Port_num)
    conn.request("GET", "/your_uri")
    response = conn.getresponse()
    print response.status, response.reason
    print response.read()
    

    Https请求

    Post请求:

    import httplib,
    import urllib
    import json
    host = 'www.google.com'
    uri = '/docs'
    
    params = {
              "user": "test",
              "pwd": "123456"
              }
    
    headers = {
               'User-Agent': 'python',
               'Content-Type': 'application/x-www-form-urlencoded',
               }
    
    params = urllib.urlencode(params)
    
    conn = httplib.HTTPSConnection(host)
    # 发送请求
    conn.request("POST", uri, params, headers)
    
    # 获取结果
    response = conn.getresponse()
    data = response.read()
    
    print 'Response: ', response.status, response.reason
    print 'Data:  ', data
    

    修改Header的Post请求:

    import httplib,
    import urllib
    import json
    host = 'www.google.com'
    uri = '/docs'
    
    params = {
              "user": "test",
              "pwd": "123456"
              }
    
    token = 'abc1234'
    
    params = urllib.urlencode(params)
    
    conn = httplib.HTTPSConnection(host)
    # 发送请求
    conn.putrequest("POST", uri)
    
    # 设置Header,并装填Post的参数
    conn.putheader("authorization", token)
    conn._set_content_length(params , "POST")
    conn.endheaders(params )
    
    # 获取结果
    response = conn.getresponse()
    data = response.read()
    
    print 'Response: ', response.status, response.reason
    print 'Data:  ', data
    

    相关文章

      网友评论

          本文标题:Httplib用法小结

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