【问题标题】:How do I add the password to git clone url in python script?如何在 python 脚本中将密码添加到 git clone url?
【发布时间】:2021-12-29 12:31:15
【问题描述】:

我在一个 python lambda 中有这个小代码,它试图从 bitbucket 中提取一个 repo:

import git

def git_clone(username,password):
   new_dir = os.getcwd() + "/temp/"
   os.chdir(new_dir)

   GIT_URL = "https://{0}:{1}@bitbucket.org/test-project/test.git".format(username,password)

   git.Git(new_dir).clone(GIT_URL)

git 方法接受我的用户名但不接受我的密码。我的密码包含字母、数字和特殊字符。我收到此错误:

URL using bad/illegal format or missing URL

这可能是格式问题吗?

【问题讨论】:

  • 尝试将其设为原始字符串 r"
  • 密码中的特殊字符必须是 URL 编码的。
  • 我试过 r" 。仍然没有运气@rv.kvetch
  • 我该怎么做? @phd
  • 不要将凭据放在 URL 中,因为这意味着它们会被写入磁盘。使用credential helper。

标签: python git bitbucket


【解决方案1】:

See文档 如果使用 bitbucket

git clone "https://x-token-auth:{token}@bitbucket.org//"

或

url = r"https://x-token-auth:{token}@bitbucket.org//"

git 克隆网址

这是您问题的解决方案。我测试了它,它可以工作。

#!/usr/bin/env python
import os 

def git_clone(password, username):
    if password and username:
        url_string = r"git clone https://{}:{}@bitbucket.org/{}/test.git".format(username, password, username)
        os.system(url_string)
git_clone(username="<username>", password="<password>")

如果这解决了您的问题,请不要忘记接受并投票作为正确答案

【讨论】:

  • 但我使用的是 bitcbucket?
  • 我修改了答案,请看上面
猜你喜欢
  • 2020-07-04
  • 2023-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-13
  • 2012-09-10
  • 1970-01-01
相关资源
最近更新 更多