【需求】
由于网络等原因,拉取GitHub 仓库代码失败, 自动重试拉取
【脚本】
# -*- coding: utf-8 -*-
#!/usr/bin/python3
"""
pip3 install pexpect
"""
import pexpect
import os
import sys
time = 1
username = 'infuq'
password = 'qwert#123'
def retry():
global time
print('invoke retry %s time' % time)
time = time + 1
# 进入到仓库目录
os.chdir('/home/infuq/Repository/infuq-others')
# 执行拉取
spawner = pexpect.spawn('git pull', encoding='utf-8')
spawner.logfile = sys.stdout
try:
i = spawner.expect(['Already up to date', 'Username']) # ,timeout = 10
# 不需要输入用户名和密码的场景
if i == 0:
spawner.close()
return
# 需要输入用户名和密码的场景
elif i == 1:
# 输入用户名
spawner.sendline(username)
i = spawner.expect(['Password']) # ,timeout = 10
if i == 0:
# 输入密码
spawner.sendline(password)
i = spawner.expect(pexpect.EOF)
if i == 0:
spawner.close()
status = spawner.status
exit_status = spawner.exitstatus
signal_status = spawner.signalstatus
# 正常退出场景
if exit_status == 0:
return
else:
# 异常退出场景, 重试
retry()
else:
# 异常退出场景, 重试
retry()
else:
# 未出现输入密码的提示, 重试
retry()
else:
# 未出现输入用户名的提示, 重试
retry()
except Exception:
# 出现异常, 重试
retry()
if __name__ == '__main__':
retry()
【执行脚本】
[infuq tmp]# python3 retry_pull.py
invoke retry 1 time
fatal: unable to access 'https://github.com/infuq/infuq-others.git/': gnutls_handshake() failed: The TLS connection was non-properly terminated.
invoke retry 2 time
fatal: unable to access 'https://github.com/infuq/infuq-others.git/': Empty reply from server
invoke retry 3 time
invoke retry 4 time
invoke retry 5 time
invoke retry 6 time
invoke retry 7 time
invoke retry 8 time
invoke retry 9 time
invoke retry 10 time
invoke retry 11 time
invoke retry 12 time
invoke retry 13 time
invoke retry 14 time
fatal: unable to access 'https://github.com/infuq/infuq-others.git/': gnutls_handshake() failed: The TLS connection was non-properly terminated.
invoke retry 15 time
fatal: unable to access 'https://github.com/infuq/infuq-others.git/': gnutls_handshake() failed: The TLS connection was non-properly terminated.
invoke retry 16 time
invoke retry 17 time
invoke retry 18 time
invoke retry 19 time
invoke retry 20 time
invoke retry 21 time
invoke retry 22 time
invoke retry 23 time
invoke retry 24 time
invoke retry 25 time
invoke retry 26 time
fatal: unable to access 'https://github.com/infuq/infuq-others.git/': Empty reply from server
invoke retry 27 time
invoke retry 28 time
fatal: unable to access 'https://github.com/infuq/infuq-others.git/': Empty reply from server
invoke retry 29 time
invoke retry 30 time
invoke retry 31 time
invoke retry 32 time
invoke retry 33 time
invoke retry 34 time
invoke retry 35 time
invoke retry 36 time
invoke retry 37 time
invoke retry 38 time
invoke retry 39 time
invoke retry 40 time
invoke retry 41 time
invoke retry 42 time
Username for 'https://github.com': infuq
infuq
Password for 'https://infuq@github.com': qwert#123
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (3/3), done.
From https://github.com/infuq/infuq-others
4058745..fd0bc40 master -> origin/master
Updating 4058745..fd0bc40
Fast-forward
README.md | 1 -
1 file changed, 1 deletion(-)
自动重试了 42 次, 拉取成功