gpt4 book ai didi

python - 尝试使用 github3.py 创建文件时出现 "InvalidSchema"

转载 作者:行者123 更新时间:2023-11-30 22:58:00 26 4
gpt4 key购买 nike

使用 github3.py 1.0.0a4,我试图制作一个基本的“服务器”程序来创建、更新、删除和获取文件。但是,我的程序在尝试创建文件时引发“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)

(修复了其他一些错误,例如删除。)

关于python - 尝试使用 github3.py 创建文件时出现 "InvalidSchema",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36380033/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com