"InvalidSchema" 尝试使用 github3.py 创建文件时

"InvalidSchema" when attempting to create file with github3.py

使用 github3.py 1.0.0a4,我正在尝试制作一个基本的 "server" 程序来创建、更新、删除和获取文件。但是,我的程序在尝试创建文件时引发 "InvalidSchema"。为什么?我用文档检查了我的程序...

代码:

from github3 import login
from github3.repos.repo import Repository

import sys, time

print sys.argv

if len(sys.argv) != 5:
    print 'Usage: server.exe username=... password=... [[-create -delete -update -fetch] file] [repo=server]'
    sys.exit()

try:
    un, pw, mode, f = sys.argv[1].split('='), sys.argv[2].split('='), sys.argv[3], sys.argv[4]
    me = login(un, pw)
except:
    print 'Usage: server.exe username=... password=... [[-create -delete -update -fetch] file] [repo=server]'
    sys.exit()

try:
    repo = sys.argv[5]
except IndexError:
    repo = 'server'

repo = Repository({'name':repo}, session=me)

if mode == '-create':
    f = open(f)
    name = f.name
    data = f.read()
    repo.create_file(name, 'Create '+time.asctime(), data.encode('utf-8'))

if mode == '-delete':
    f = open(f)
    repo.contents(f.name).delete('Delete '+time.asctime(), data.encode('utf-8'))

if mode == '-update':
    f = open(f)
    name = f.name
    data = f.read()
    repo.contents(name).update('Update '+time.asctime(), data.encode('utf-8'))

if mode == '-fetch':
    with open(f, mode='w') as f:
        f.write(repo.contents(f.name))

在命令行上 运行 时的结果:

C:\Python27>python server.py username=... password=... -create pytest.py
['server.py', 'username=...', 'password=...', '-create', 'pytest.py']
Traceback (most recent call last):
  File "server.py", line 30, in <module>
    repo.create_file(name, 'Create '+time.asctime(), data.encode('utf-8'))
  File "C:\Python27\lib\site-packages\github3\decorators.py", line 33, in auth_wrapper
    return func(self, *args, **kwargs)
  File "C:\Python27\lib\site-packages\github3\repos\repo.py", line 745, in create_file
    json = self._json(self._put(url, data=dumps(data)), 201)
  File "C:\Python27\lib\site-packages\github3\models.py", line 199, in _put
    return self.session.put(url, **kwargs)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 521, in put
    return self.request('PUT', url, data=data, **kwargs)
  File "C:\Python27\lib\site-packages\github3\session.py", line 88, in request
    response = super(GitHubSession, self).request(*args, **kwargs)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 468, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 570, in send
    adapter = self.get_adapter(url=request.url)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 644, in get_adapter
    raise InvalidSchema("No connection adapters were found for '%s'" % url)
requests.exceptions.InvalidSchema: No connection adapters were found for ':///contents/pytest.py

unpw 是列表而不是字符串。使用 un = sys.argv[1].split('=')[1] 或类似的东西。

编辑代码:

from github3 import login
from github3.repos.repo import Repository

import sys, time

if len(sys.argv) != 5:
    print 'Usage: server.exe username=... password=... [[-create -delete -update -fetch] file] [repo=server]'
    sys.exit()

try:
    un, pw, mode, f = sys.argv[1].split('=')[1], sys.argv[2].split('=')[1], sys.argv[3], sys.argv[4]
    me = login(un, pw)
except:
    print 'Usage: server.exe username=... password=... [[-create -delete -update -fetch] file] [repo=server]'
    sys.exit()

try:
    repo = sys.argv[5]
except IndexError:
    repo = 'server'

repo = me.repository(un, repo)

if mode == '-create':
    f = open(f)
    name = f.name
    data = f.read()
    repo.create_file(name, 'Create '+time.asctime(), data.encode('utf-8'))

if mode == '-delete':
    f = open(f)
    repo.file_contents(f.name).delete('Delete '+time.asctime())

if mode == '-update':
    f = open(f)
    name = f.name
    data = f.read()
    repo.file_contents(name).update('Update '+time.asctime(), f.read().encode('utf-8'))

if mode == '-fetch':
    with open(f, mode='w') as f:
        f.write(repo.file_contents(f.name).decoded)

(修复了删除等其他问题。)

这一行:

un, pw, mode, f = sys.argv[1].split('='), sys.argv[2].split('='), sys.argv[3], sys.argv[4]

您将 unpw 分配为列表,而您却像使用字符串一样使用它们。您需要找到从 .split() 创建的列表的第二项:将行替换为

un, pw, mode, f = sys.argv[1].split('=')[1], sys.argv[2].split('=')[1], sys.argv[3], sys.argv[4]

对我有用。