zoukankan      html  css  js  c++  java
  • Python # 一个api接口调用POST请求

    ###

    import urllib2,json
    url='http://xxx.xxx.com.cn/api/v1'
    data=json.dumps({'organId':'1'})                #数据
    headers={'Content-Type':'application/json'}     #头部
    request = urllib2.Request(url,data,headers)     #urllib2用一个Request对象来映射你提出的HTTP请求
    print request
    response = urllib2.urlopen(request)             #通过调用urlopen并传入Request对象,将返回一个相关请求response对象
    print response
    message = response.read()                       #这个应答对象如同一个文件对象,所以你可以在Response中调用.read()
    print message

    curl命令

    curl -X POST -H "Content-Type:application/json" -d {"organId":"1"} http://xxx.xxx.com.cn/api/

    ###

    urllib2另外一种用法:

    import urllib2,json,urllib
    
    response= urllib2.urlopen('http://www.ttlsa.com/')
    html = response.read()
    print html
    
    
    req = urllib2.Request('http://www.ttlsa.com/')
    response = urllib2.urlopen(req)
    the_page = response.read()
    print the_page
    print urllib2.Request.get_method(req)

    ###

    import urllib2
    import urllib
    
    values = {'query' : '捷豹'}
    
    url_values = urllib.urlencode(values)
    url = 'https://www.sogou.com/web'
    full_url = url + '?' + url_values
    print full_url
    req = urllib2.Request(full_url)
    res = urllib2.urlopen(req)
    page = res.read()
    print page

    ###

  • 相关阅读:
    SQL中的union
    SQL的类型转换
    Keytool生成证书
    Openssl生成证书
    Python示例-Json Parse
    Python示例-TCP Port Scan
    Python套接字
    TCP端口扫描
    Linux环境变量
    Python示例-Logging
  • 原文地址:https://www.cnblogs.com/lwsup/p/7545802.html
Copyright © 2011-2022 走看看