简体   繁体   中英

Respond to git command line prompt from Python program

I'm currently using a Python program to make automated commits to a repository after it makes some edits to a file. I am using subprocess.Popen to make a child process that calls the git binary. I though I could just use stdin=PIPE when creating the child process and be able to write my user credentials when prompted by the command line, but for some reason this isn't happening. The code looks like the following:

proc = Popen(["git","push","origin","master"], stdin=PIPE)
proc.communicate("username")
proc.communicate("password")

What's happening right now is that it's calling the binary but still showing me the command line prompt. I've taken a look at some other questions on here, but they are all using essentially the same code I am, so I'm a little stumped as to the problem. I know there are plenty of other ways of achieving this result, like caching my credentials, but I'm curious to see if this way can work for a number of reasons.

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()

something like that i guess ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM