Last active
December 16, 2015 20:19
-
-
Save zeroSteiner/5491749 to your computer and use it in GitHub Desktop.
PTY Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import os | |
import select | |
import threading | |
class PTYConnector(threading.Thread): | |
def __init__(self): | |
super(PTYConnector, self).__init__() | |
self.dev1m, self.dev1s = os.openpty() | |
self.dev2m, self.dev2s = os.openpty() | |
self.is_running = threading.Event() | |
def run(self): | |
self.is_running.set() | |
dev1m = self.dev1m | |
dev2m = self.dev2m | |
while self.is_running.is_set(): | |
fds = select.select([dev1m, dev2m], [], [], 1) | |
for fd in fds[0]: | |
if fd == dev1m: | |
os.write(dev2m, os.read(dev1m, 1)) | |
elif fd == dev2m: | |
os.write(dev1m, os.read(dev2m, 1)) | |
def stop(self): | |
self.is_running.clear() | |
self.join() | |
def get_devs(self): | |
return (os.ttyname(self.dev1s), os.ttyname(self.dev2s)) | |
def main(): | |
con = PTYConnector() | |
dev1, dev2 = con.get_devs() | |
print 'dev 1: ' + dev1 | |
print 'dev 2: ' + dev2 | |
con.start() | |
raw_input('Press Enter When Done.') | |
con.stop() | |
return 0 | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment