用于运行动态curl请求的python脚本

时间:2015-06-22 16:44:37

标签: python bash curl

Python newb here,这是我当前的脚本:

#!/usr/bin/env python
import os
import time
import datetime
import subprocess

ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime(%Y%m%d)

我的卷曲命令:

curl -i -k -H -o <timestamp>.txt "Accept: application/json" -H "Content-Type: application/json" -H "appID:******" -H "appKey:*******" -X GET https://*****.com/csOpen/workplace/hr/v1/employees_s?type=EMP&location=******&term_from=<timestamp>

此curl请求的动态方面来自我脚本的python部分。我需要输出文件是$ currentTime.txt,我需要php变量$ term_from也是时间戳。

到目前为止,我已尝试使用

调用curl命令
os.system('curl -i -k -H -o' + %st + '.txt "Accept: application/json" -H "Content-Type: application/json" -H "appID:arcsght_app" -H "appKey:*****" -X GET https://csopen.teamaol.com/csOpen/workplace/hr/v1/employees_s?type=EMP&location=Dulles&term_from=' + %st)

那不起作用,然后我尝试使用

subprocess.call(<same curl command as above>)

那并没有用。

我已经尝试过来自bash的curl命令,它可以工作,我可以通过时间戳来显示我是如何需要它的。我只是想弄清楚如何将所有东西捆绑在一起。在我发布这篇文章之前,我确实尝试过自己解决这个问题,但这是我第一次真正进入python的冒险,所以我对什么有效以及什么不是很渺茫有所了解。寻求帮助!感谢。

1 个答案:

答案 0 :(得分:0)

my_command = 'curl -i -k -H -o {timestamp}.txt "Accept: application/json" -H "Content-Type: application/json" -H "appID:******" -H "appKey:*******" -X GET https://*****.com/csOpen/workplace/hr/v1/employees_s?type=EMP&location=******&term_from={timestamp}'.format(timestamp=123456)

os.system(my_command)

应该工作正常...我不完全确定你为什么要在python中这样做...但这应该让你没问题