美文网首页
python 以客户端形式通HTTP服务交互

python 以客户端形式通HTTP服务交互

作者: 孙广宁 | 来源:发表于2022-06-03 00:00 被阅读0次
    11.1 我们需要以客户端的形式通过HTTP歇息访问多种服务。
    • 使用urllib.request模块通常就可以满足要求。
    from urllib import request,parse
    url = 'http://httpbin.org/get'
    parms = {
        'name1':'value1',
        'name2':'value2'
    }
    qs = parse.urlencode(parms)
    u = request.urlopen(url+"?"+qs)
    resp = u.read()
    print(resp)
    
    • 返回值为
    ~/ning-dashboard/tmp > python3 ttt.py
    b'{\n  "args": {\n    "name1": "value1", \n    "name2": "value2"\n  }, \n  "headers": {\n    "Accept-Encoding": "identity", \n    "Host": "httpbin.org", \n    "User-Agent": "Python-urllib/3.7", \n    "X-Amzn-Trace-Id": "Root=1-6298de82-639ab39566b89bc35bd45c7a"\n  }, \n  "origin": "210.22.74.111", \n  "url": "http://httpbin.org/get?name1=value1&name2=value2"\n}\n'
    
    • 如果需要使用POST方法在请求主题中发送查询参数,可以将参数编码后作为可选参数提供给urlopen()函数
    from urllib import request,parse
    url = 'http://httpbin.org/post'
    parms = {
        'name1':'value1',
        'name2':'value2'
    }
    qs = parse.urlencode(parms)
    u = request.urlopen(url,qs.encode("ascii"))
    resp = u.read()
    print(resp)
    
    • 返回值如下
    ~/ning-dashboard/tmp > python3 ttt.py                                                                                      13s
    b'{\n  "args": {}, \n  "data": "", \n  "files": {}, \n  "form": {\n    "name1": "value1", \n    "name2": "value2"\n  }, \n  "headers": {\n    "Accept-Encoding": "identity", \n    "Content-Length": "25", \n    "Content-Type": "application/x-www-form-urlencoded", \n    "Host": "httpbin.org", \n    "User-Agent": "Python-urllib/3.7", \n    "X-Amzn-Trace-Id": "Root=1-6298df7a-572e0ce9697610826d3e8807"\n  }, \n  "json": null, \n  "origin": "210.22.74.111", \n  "url": "http://httpbin.org/post"\n}\n'
    
    • 如果想在发出的请求中提供一些自定义的http头,比如修改user-agent字段,那么可以创建一个包含字段值的字典,并创建一个request实例,然后将其传给urlopen(),
    from urllib import request,parse
    url = 'http://httpbin.org/post'
    parms = {
        'name1':'value1',
        'name2':'value2'
    }
    headers ={
        'User-agent':'none/ofyourbusiness',
        'Spam':'Eggs'
    }
    qs = parse.urlencode(parms)
    req = request.Request(url,qs.encode("ascii"),headers=headers)
    u = request.urlopen(req)
    resp = u.read()
    print(resp)
    
    • 返回值
    ~/ning-dashboard/tmp > python3 ttt.py                                                                                   2m 18s
    b'{\n  "args": {}, \n  "data": "", \n  "files": {}, \n  "form": {\n    "name1": "value1", \n    "name2": "value2"\n  }, \n  "headers": {\n    "Accept-Encoding": "identity", \n    "Content-Length": "25", \n    "Content-Type": "application/x-www-form-urlencoded", \n    "Host": "httpbin.org", \n    "Spam": "Eggs", \n    "User-Agent": "none/ofyourbusiness", \n    "X-Amzn-Trace-Id": "Root=1-6298e1cb-2cec587f0550b28d35dff7ec"\n  }, \n  "json": null, \n  "origin": "210.22.74.111", \n  "url": "http://httpbin.org/post"\n}\n'
    
    • 也可以尝试使用requests模块
    import requests
    url = 'http://httpbin.org/post'
    
    parms = {
        'name1':'value1',
        'name2':'value2'
    }
    headers ={
        'User-agent':'none/ofyourbusiness',
        'Spam':'Eggs'
    }
    
    resp = requests.post(url,data=parms,headers=headers)
    print(resp.text)
    
    • 返回结果
    ~/ning-dashboard/tmp > python3 ttt.py                                                                                      20s
    {
      "args": {},
      "data": "",
      "files": {},
      "form": {
        "name1": "value1",
        "name2": "value2"
      },
      "headers": {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate",
        "Content-Length": "25",
        "Content-Type": "application/x-www-form-urlencoded",
        "Host": "httpbin.org",
        "Spam": "Eggs",
        "User-Agent": "none/ofyourbusiness",
        "X-Amzn-Trace-Id": "Root=1-6298e122-393530d343acb327131bcca8"
      },
      "json": null,
      "origin": "210.22.74.111",
      "url": "http://httpbin.org/post"
    }
    

    相关文章

      网友评论

          本文标题:python 以客户端形式通HTTP服务交互

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