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 += ["python", "-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))
28 raise socket.error(buf)
30 raise socket.error("unexpected reply from %s: %r" % (host, head))
33 if self.proc is not None:
34 self.proc.stdin.close()
35 self.proc.stdout.close()
39 def send(self, data, flags = 0):
40 self.proc.stdin.write(data)
43 def recv(self, buflen, flags = 0):
44 if (flags & socket.MSG_DONTWAIT) == 0:
45 select.select([self.proc.stdout], [], [])
46 return self.proc.stdout.read(buflen)
49 return self.proc.stdout.fileno()
55 fcntl.fcntl(sys.stdin, fcntl.F_SETFL, fcntl.fcntl(sys.stdin, fcntl.F_GETFL) | os.O_NONBLOCK)
56 sk = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
59 sk.connect(sys.argv[1])
60 except socket.error as err:
61 sys.stdout.write("SSOCK-connect: %s\n" % err)
63 sys.stdout.write("SSOCK+\n")
68 if buf1: wfd.append(sk)
69 if buf2: wfd.append(sys.stdout)
70 rfd, wfd, efd = select.select([sk, sys.stdin], wfd, [])
78 ret = sys.stdin.read()
87 sys.stdout.write(buf2)
93 if __name__ == "__main__":