search

Home  >  Q&A  >  body text

python - 用curl如何curl

curl -s -X POST -u cu2:l5f7jrRQttWdxsLmY7FV4+MA= -H "Accept:application/xml" -H "Date:Fri, 14 Apr 2017 02:07:17 GMT" -d "date=2017-04-13&channel=vod.tv.cn&isExactMatch=false&region=&startdate=2017-04-13&enddate=2017-04-14" http://opencenter.com/myview/bandwidth-origin

像这样一个又带-u-H-d的url要怎么用python通过curl访问并获取返回结果?

PHPzPHPz2795 days ago877

reply all(2)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:36:43

    It is recommended that you change the question title.

    According to your statement, the code you provided can actually be run, but you don't know how to get its output.

    Seeing that you marked Python, in fact, you only need to copy a copy of the Python code for executing terminal commands and you will understand.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:36:43

    已经找到方法了,很多人会去用到pycurl,可是我百度找了下都只写了代码,代码含义都没有写,后来找到了`http://docs.python-requests.org/zh_CN/latest/user/quickstart.html`真的是个好东西,里面有关于requests模块的详细说明。
    简单分析下我的需求,1、http请求 2、‘-u’身份验证 3、‘-H’请求header头 4、‘-d’请求参数,理清思路再看文档就简单多了(刚开始我连-H是什么意思都没弄明白)。
    import requests
    url = 'http://opencenter.com/myview/bandwidth-origin'##定义http请求的地址,即1
    headers = {'Accept': "application/xml",'Date':'Fri, 14 Apr 2017 02:07:17 GMT'}##定义header头,用dict方式定义,即3
    data = {'channel': 'vod.tv.cn', 'dataformat': 'json','date':'2017-04-13'}##定义参数,同样用dict定义,即4
    res = requests.post(url, data=data, headers=headers, auth=('tv2', sign))##post请求,且认证user=‘tv2’,password=‘sign’
    print res.text##就能看到打印结果了。
    

    reply
    0
  • Cancelreply