我试图让git命令与paramiko服务器一起工作。我收到了git-receive-pack
命令,但我无法从频道收到任何数据。
class Server(paramiko.ServerInterface):
def __init__(self):
self.event = threading.Event()
def check_channel_request(self, kind, chanid):
if kind == 'session':
return paramiko.OPEN_SUCCEEDED
return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
def check_channel_shell_request(self, channel):
self.event.set()
return True
def check_channel_exec_request(self, channel, command):
command = command.split(' ')
if command[0] not in ['git-receive-pack', 'git-upload-pack']:
return False
command[-1] = '/home/remco/Downloads/git'
self.event.set()
t = threading.Thread(target=git.command, args=[command, channel])
t.start()
return True
def check_global_request(self, kind, msg):
return super().check_global_request(kind, msg)
def check_channel_forward_agent_request(self, channel):
return False
def check_channel_subsystem_request(self, channel, name):
return super().check_channel_subsystem_request(channel, name)
def check_auth_password(self, username, password):
if True:
return paramiko.AUTH_SUCCESSFUL
return paramiko.AUTH_FAILED
def check_auth_publickey(self, username, key):
return paramiko.AUTH_SUCCESSFUL
def get_allowed_auths(self, username):
return 'password,publickey'
def check_channel_pty_request(self, channel, term, width, height, pixelwidth,
pixelheight, modes):
return True
def main():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
sock.bind((config.config['ssh']['host'], config.config['ssh']['port']))
sock.listen(1)
while True:
conn, addr = sock.accept()
host_key = paramiko.RSAKey.from_private_key_file(path.expanduser(
config.config['ssh']['keyfile']))
trans = transport.Transport(conn)
trans.load_server_moduli()
trans.add_server_key(host_key)
server = Server()
trans.start_server(server=server)
server.event.wait(1)
git.command是以下功能:
@logging.logger()
def command(command, channel):
proc = subprocess.Popen(command, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
print(channel.recv_ready())
result = ''
while True:
r_ready, w_ready, x_ready = select.select(
[channel, proc.stdout, proc.stderr], [proc.stdin], [])
if channel in r_ready:
logging.info('in')
data = channel.recv(1024)
if data:
logging.info(data)
proc.stdin.write(data)
if proc.stdout in r_ready:
print('out')
data = proc.stdout.read(1)
result += data.decode('utf-8')
# print(data)
channel.sendall(data)
if proc.stderr in r_ready:
print('err')
data = proc.stderr.read(1)
if data:
print(data)
channel.sendall_stderr(data)
else:
break
print(result)
print('done')
proc.stdin.close()
proc.stdout.close()
proc.stderr.close()
问题是channel.recv_ready()
永远不会True
而channel.recv(n)
会导致无限等待。
@logging.logger()
装饰器打印被调用函数* args和** kwargs。它打印以下内容:
Called command with *(['git-receive-pack', '/home/remco/Downloads/git'], <paramiko.Channel 1 (open) window=2097152 -> <paramiko.Transport at 0x2611ed10 (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>) **{}
在能够接收数据之前,我是否忘记了我需要做的事情?