Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Python程序响应git命令行提示符_Python_Git_Pipe_Subprocess_Popen - Fatal编程技术网

从Python程序响应git命令行提示符

从Python程序响应git命令行提示符,python,git,pipe,subprocess,popen,Python,Git,Pipe,Subprocess,Popen,我目前正在使用Python程序在对文件进行一些编辑后自动提交到存储库。我正在使用subprocess.Popen创建一个调用git二进制文件的子进程。我想我可以在创建子进程时使用stdin=PIPE,并在命令行提示时编写用户凭据,但由于某种原因,这没有发生。代码如下所示: proc = Popen(["git","push","origin","master"], stdin=PIPE) proc.communicate("username") proc.communicate("passwor

我目前正在使用Python程序在对文件进行一些编辑后自动提交到存储库。我正在使用
subprocess.Popen
创建一个调用git二进制文件的子进程。我想我可以在创建子进程时使用
stdin=PIPE
,并在命令行提示时编写用户凭据,但由于某种原因,这没有发生。代码如下所示:

proc = Popen(["git","push","origin","master"], stdin=PIPE)
proc.communicate("username")
proc.communicate("password")
现在发生的事情是,它正在调用二进制文件,但仍然向我显示命令行提示符。我在这里看了一些其他的问题,但是它们都使用了与我基本相同的代码,所以我对这个问题有点困惑。我知道还有很多其他方法可以达到这个效果,比如缓存我的凭证,但我很好奇,看看这种方法是否能奏效,原因有很多

proc = Popen(["git","push","origin","master"], stdin=PIPE,stdout=PIPE,stderr=PIPE)
time.sleep(1)
proc.stdin.write("username")
time.sleep(1)
proc.stdin.write("password")
#or maybe
for letter in "password":
    proc.stdin.write(letter)
    time.sleep(0.1)
print proc.communicate()

像这样的事情我想…

就像我说的,我知道还有其他方法可以做到这一点。我只是想知道这种方法是否会因一些特定于项目的原因而起作用。您不能使用Communication(通信)两次,写信给STDINUSE write(写入)会因为某种原因给我一个I/O错误。虽然这不能回答您的问题,我可以提到,从Python处理GIT的模块已经编写好了,您可能想改用它。您需要用户名和密码,还是只需要密码?当我按或拉的时候,我只需要输入我的密码。似乎没有完全工作。它会将用户名写到屏幕上,但git在这之后从不提示输入密码,程序只是退出。嗯,无论如何,我建议切换到keyauthentication。。。在一个猜测中,它被编程到git中,一次只需要一个字符或者别的什么,并且一次发送整个密码就会破坏它。。。如果它太难,你可能想用它是的,我想你是对的。或者,您知道是否可以在“git push origin master”的同一行中指定用户名和密码。我知道git cred store将creds维护为“用户名:password@repo.com“.取决于我认为是哪个git客户端。。。阿法克乌龟git让你以某种方式做到这一点。。。您可能只想使用pygit