2 import subprocess, socket, fcntl, select
4 class sshsocket(object):
5 def __init__(self, host, path, user = None, port = None):
8 args += ["-u", str(user)]
10 args += ["-p", str(int(port))]
12 args += ["python3", "-m", "pdm.sshsock", path]
13 self.proc = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True)
14 fcntl.fcntl(self.proc.stdout, fcntl.F_SETFL, fcntl.fcntl(self.proc.stdout, fcntl.F_GETFL) | os.O_NONBLOCK)
17 raise socket.error("unexpected reply from %s: %r" % (host, head))
24 raise socket.error("unexpected EOF in SSH socket stream")
36 raise socket.error(buf.decode("utf-8"))
38 raise socket.error("unexpected reply from %s: %r" % (host, head))
41 if self.proc is not None:
42 self.proc.stdin.close()
43 self.proc.stdout.close()
47 def send(self, data, flags = 0):
48 self.proc.stdin.write(data)
51 def recv(self, buflen, flags = 0):
52 if (flags & socket.MSG_DONTWAIT) == 0:
53 select.select([self.proc.stdout], [], [])
54 return self.proc.stdout.read(buflen)
57 return self.proc.stdout.fileno()
63 fcntl.fcntl(sys.stdin.buffer, fcntl.F_SETFL, fcntl.fcntl(sys.stdin.buffer, fcntl.F_GETFL) | os.O_NONBLOCK)
64 sk = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
67 sk.connect(sys.argv[1])
68 except socket.error as err:
69 sys.stdout.write("SSOCK-connect: %s\n" % err)
72 sys.stdout.write("SSOCK+\n")
78 if buf1: wfd.append(sk)
79 if buf2: wfd.append(sys.stdout.buffer)
80 rfd, wfd, efd = select.select([sk, sys.stdin.buffer], wfd, [])
87 if sys.stdin.buffer in rfd:
88 ret = sys.stdin.buffer.read()
96 if sys.stdout.buffer in wfd:
97 sys.stdout.buffer.write(buf2)
98 sys.stdout.buffer.flush()
103 if __name__ == "__main__":