要通过python';s子进程或pycurl
我有以下命令,它将用户添加到要通过python';s子进程或pycurl,python,curl,subprocess,pycurl,Python,Curl,Subprocess,Pycurl,我有以下命令,它将用户添加到gerrit实例的管理员组中 curl -X POST -H "Content-Type:application/json;charset=UTF-8" -u nidhi:pswd http://host_ip:port/a/groups/Administrators/members.add -d '{"members":["user@example.com"]}' 当我在终端上运行这个命令时,它运行得很好,并给出了预期的输出 但是,我想在python中使用子进程
gerrit
实例的管理员组中
curl -X POST -H "Content-Type:application/json;charset=UTF-8" -u nidhi:pswd http://host_ip:port/a/groups/Administrators/members.add -d '{"members":["user@example.com"]}'
当我在终端上运行这个命令时,它运行得很好,并给出了预期的输出
但是,我想在python中使用子进程
或pycurl
库执行此命令
使用子流程
我编写了以下代码
def add_user_to_administrator(u_name,url):
bashCommand = 'curl -X POST -H "Content-Type:application/json;charset=UTF-8" -u nidhi:pswd http://'+url+'/a/groups/Administrators/members.add -d '+"'"+'{"members":["$u_n@example.com"]}'+"'"
bashCommand = string.Template(bashCommand).substitute({'u_n':u_name})
print bashCommand.split()
process = subprocess.Popen(bashCommand.split())
它不显示任何错误,但在管理员组中看不到任何更改
我使用pycurl
尝试了同样的方法
def add_user_to_administrator2(u_name,url):
pf = json.dumps({"members":[str(str(u_name)+"@example.com")]})
headers = ['Content-Type:application/json;charset=UTF-8']
pageContents = StringIO.StringIO()
p = pycurl.Curl()
p.setopt(pycurl.FOLLOWLOCATION, 1)
p.setopt(pycurl.POST, 1)
p.setopt(pycurl.HTTPHEADER, headers)
p.setopt(pycurl.POSTFIELDS, pf)
p.setopt(pycurl.WRITEFUNCTION, pageContents.write)
p.setopt(pycurl.VERBOSE, True)
p.setopt(pycurl.DEBUGFUNCTION, test)
p.setopt(pycurl.USERPWD, "nidhi:pswd")
pass_url=str("http://"+url+"/a/groups/Administrators/Administrators/members.add").rstrip('\n')
print pass_url
p.setopt(pycurl.URL, pass_url)
p.perform()
p.close()
pageContents.seek(0)
print pageContents.readlines()
这会引发一个错误,它找不到帐户成员
提到的变量url
的形式为host\u ip:port
为了纠正这些错误,我做了很多努力。我不知道我哪里出错了。任何帮助都将不胜感激 - 您应该尝试[
](urllib2
)或(python2
)发布json数据李>python3
- 对于
:subprocess.Popen
()可能会给您提供帮助,或者只需在shell中执行bash命令即可查看差异李>subprocess.Popen.communicate
- 对于
,我没有使用它,但请您添加错误信息pycurl
- 您应该尝试[
](urllib2
)或(python2
)发布json数据李>python3
- 对于
:subprocess.Popen
()可能会给您提供帮助,或者只需在shell中执行bash命令即可查看差异李>subprocess.Popen.communicate
- 对于
,我没有使用它,但请您添加错误信息pycurl
'
:
…填充“+”“+”更多。填充…
在字符之前使用\
进行转义,即使用
“curl-X POST-H\”内容类型:application/json;字符集=UTF-8\“”
将在内容类型部分周围保留“
有关转义字符的更多信息,请参见此处:
…反斜杠()字符用于转义具有特殊含义的字符
b) popen
查看他们用于将命令行拆分为参数的示例。shlex
拆分字符串的方式稍有不同:
print(bashCommand.split())
['curl', '-X', 'POST', '-H', '"Content-Type:application/json;charset=UTF-8"', '-u', 'nidhi:pswd', 'http://TEST_URL/a/groups/Administrators/members.add', '-d', '\'{"members":["TEST_USER@example.com"]}\'']
print(shlex.split(bashCommand))
['curl', '-X', 'POST', '-H', 'Content-Type:application/json;charset=UTF-8', '-u', 'nidhi:pswd', 'http://TEST_URL/a/groups/Administrators/members.add', '-d', '{"members":["TEST_USER@example.com"]}']
您可以看到shlex
删除了多余的报价
c) http响应代码
尝试使用curl
中的-I
选项获取HTTP响应代码(以及其他HTTP头):
即使您正在使用子流程
启动/发出请求,它仍应将返回值打印到控制台(stdout)
d) 把它们放在一起
我更改了url
和u\u name
插入字符串的方式
import shlex
import subprocess
def add_user_to_administrator(u_name, url):
bashCommand = "curl -I -X POST -H \"Content-Type:application/json;charset=UTF-8\" -u nidhi:pswd http://%(url)s/a/groups/Administrators/members.add -d '{\"members\":[\"%(u_n)s@example.com\"]}'"
bashCommand = bashCommand % {'u_n': u_name, 'url': url}
args = shlex.split(bashCommand)
process = subprocess.Popen(args)
add_user_to_administrator('TEST_USER', 'TEST_URL')
如果这些都没有帮助,而且您没有收到gerrit的响应,我会检查gerrit日志,看看当它收到您的请求时会发生什么。a)字符串转义
对于subprocess/curl用法,您应该转义字符串标记,而不是手动添加额外的'
:
…填充“+”“+”更多。填充…
在字符之前使用\
进行转义,即使用
“curl-X POST-H\”内容类型:application/json;charset=UTF-8\”
将在内容类型部分周围保留“
有关转义字符的更多信息,请参见此处:
…反斜杠()字符用于转义具有特殊含义的字符
b) popen
查看他们用于将命令行拆分为参数的示例shlex
以稍微不同的方式拆分字符串:
print(bashCommand.split())
['curl', '-X', 'POST', '-H', '"Content-Type:application/json;charset=UTF-8"', '-u', 'nidhi:pswd', 'http://TEST_URL/a/groups/Administrators/members.add', '-d', '\'{"members":["TEST_USER@example.com"]}\'']
print(shlex.split(bashCommand))
['curl', '-X', 'POST', '-H', 'Content-Type:application/json;charset=UTF-8', '-u', 'nidhi:pswd', 'http://TEST_URL/a/groups/Administrators/members.add', '-d', '{"members":["TEST_USER@example.com"]}']
您可以看到shlex
删除了多余的报价
c) http响应代码
尝试使用curl
中的-I
选项获取HTTP响应代码(以及其他HTTP头):
即使您正在使用子流程
启动/发出请求,它仍应将返回值打印到控制台(stdout)
d) 把它们放在一起
我更改了url
和u\u name
插入字符串的方式
import shlex
import subprocess
def add_user_to_administrator(u_name, url):
bashCommand = "curl -I -X POST -H \"Content-Type:application/json;charset=UTF-8\" -u nidhi:pswd http://%(url)s/a/groups/Administrators/members.add -d '{\"members\":[\"%(u_n)s@example.com\"]}'"
bashCommand = bashCommand % {'u_n': u_name, 'url': url}
args = shlex.split(bashCommand)
process = subprocess.Popen(args)
add_user_to_administrator('TEST_USER', 'TEST_URL')
如果这些都没有帮助,并且gerrit没有响应,我会检查gerrit日志,看看当它收到您的请求时会发生什么。您为什么使用curl而不是像模块一样的请求或urllib?您为什么使用curl而不是像模块一样的请求或urllib?感谢您的详细解释。。。我帮了大忙。。。!谢谢你的详细解释。。。我帮了大忙。。。!