2015-04-06 24 views
0
curl https://api.smartsheet.com/1.1/sheet/7846568935090052 -H "Authorization: Bearer 4ziycpnqjvto49oah6t6urd4un" -H "Accept: application/vnd.ms-excel" -o output.xls 

我已經試過這樣:我如何使用pycurl執行此操作?

with open('test.xls') as xls: 
    data = xls.read() 

    c = pycurl.Curl() 
    c.setopt(pycurl.URL, 'https://api.smartsheet.com/1.1/sheet/5481016912570244') 
    c.setopt(pycurl.POST, 1) 
    c.setopt(pycurl.POSTFIELDS, data) 
    c.setopt(pycurl.HTTPHEADER, ['Authorization: Bearer 3e181f72o602q4dnq3yrg7jd3u', 
          'Content-Type: application/vnd.ms-excel']) 
    c.perform() 

return redirect('https://app.smartsheet.com/b/home') 

我TEST.XLS內: output.xls

預期輸出: 我登錄到smartsheet並進入我的data.My代碼應轉換該表格轉換爲Excel表格。

此代碼的工作,但我使用pycurl

curl https://api.smartsheet.com/1.1/sheet/7846568935090052 -H "Authorization: Bearer 4ziycpnqjvto49oah6t6urd4un" -H "Accept: application/vnd.ms-excel" -o output.xls 

回答

0

鋁始終,RTFM ...看一看想要的輸出:

https://docs.python.org/2/library/subprocess.html

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
# 
# test_curl.py 
# 
# Copyright 2015 John Coppens 
# 
# This program is free software; you can redistribute it and/or modify 
# it under the terms of the GNU General Public License as published by 
# the Free Software Foundation; either version 2 of the License, or 
# (at your option) any later version. 
# 
# This program is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty of 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
# GNU General Public License for more details. 
# 
# You should have received a copy of the GNU General Public License 
# along with this program; if not, write to the Free Software 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
# MA 02110-1301, USA. 
# 
# 

import subprocess as sp 

def main(): 
    res = sp.check_output("curl" + \ 
          " https://api.smartsheet.com/1.1/sheet/7846568935090052" + \ 
          " -H \"Authorization: Bearer 4ziycpnqjvto49oah6t6urd4un\"" + \ 
          " -H \"Accept: application/vnd.ms-excel\"", 
          shell = True) 
    with open("test.xls", "w") as f: 
     f.write(res) 

    return 0 

if __name__ == '__main__': 
    main() 

添加errorchecks等根據個人口味調味。 'shell = True'可能存在安全風險。看文檔。

+0

我認爲OP意味着使用['PycURL'](http://pycurl.sourceforge.net/)庫,而不是直接調用curl可執行文件。 – Nazar554

+0

是的。但是,當我回復時,編輯不在那裏... – jcoppens

+0

任何代碼建議? – Sagi