本文整理汇总了Python中sys.stdin.fileno函数的典型用法代码示例。如果您正苦于以下问题:Python fileno函数的具体用法?Python fileno怎么用?Python fileno使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了fileno函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: myinput
def myinput(timeout=0):
ESCSEQ = b'\x1b['
tty.setraw(stdin.fileno())
#stdout = os.fdopen(stdin.fileno(), 'wb', 0)
special = False
while True:
rlist, wlist, xlist = select([stdin], [], [], 0)
ch = os.read(stdin.fileno(), 1)
ch = ch.decode()
if ch == '\x1b':
if special:
yield ESC
else:
special = True
elif ch == '[':
if not special:
yield ch
else:
if special:
special = False
if ch == 'A': yield UP
elif ch == 'B': yield DOWN
elif ch == 'C': yield RIGHT
elif ch == 'D': yield LEFT
else:
yield ch
示例2: run_client
def run_client():
ctx = zmq.Context()
sub = ctx.socket(zmq.SUB)
sub.connect('tcp://localhost:4455')
sub.setsockopt(zmq.SUBSCRIBE, "")
push = ctx.socket(zmq.PUSH)
push.connect('tcp://localhost:4456')
plr = zmq.Poller()
plr.register(stdin.fileno(), zmq.POLLIN)
plr.register(sub, zmq.POLLIN)
while True:
try:
fds = dict(plr.poll())
except KeyboardInterrupt:
print "\nClosing gracefully..."
return
if stdin.fileno() in fds:
line = stdin.readline()[:-1] # trim newline char
push.send(line)
if sub in fds:
msg = sub.recv()
print "Got message: %r" % (msg,)
示例3: getchar
def getchar():
fd = stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(stdin.fileno())
ch = stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
示例4: cbreak
def cbreak():
if hasattr(stdin, "fileno") and os.isatty(stdin.fileno()):
old_attrs = termios.tcgetattr(stdin)
tty.setcbreak(stdin)
tty.setraw(stdin)
try:
yield
finally:
if hasattr(stdin, "fileno") and os.isatty(stdin.fileno()):
termios.tcsetattr(stdin, termios.TCSADRAIN, old_attrs)
示例5: init
def init():
flags = fcntl.fcntl(stdin.fileno(), fcntl.F_GETFL)
if flags == flags | os.O_NONBLOCK:
return
else:
rv = fcntl.fcntl(stdin.fileno(), fcntl.F_SETFL, flags | os.O_NONBLOCK)
if rv == 0:
print "ok"
else:
print "broken"
示例6: stdin_handler
def stdin_handler():
fcntl.fcntl(stdin, fcntl.F_SETFL, os.O_NONBLOCK)
while True:
wait_read(stdin.fileno())
cmd = stdin.readline().strip().lower()
if cmd == 'quit':
exit(0)
示例7: connect
def connect(host, handshake):
s = socket()
s.connect((host, PORT))
s.send(handshake)
print('Connected, press ^C or ^D to terminate the connection.')
try:
fd = stdin.fileno()
old_settings = tcgetattr(fd)
setraw(fd)
while True:
r, _, _ = select([stdin, s], [], [])
for ready in r:
if ready is s:
r = s.recv(4096)
if not r:
print('Connection closed by remote peer', file=stderr)
return
stdout.write(r)
stdout.flush()
elif ready is stdin:
r = stdin.read(1)
if not r or chr(3) in r or chr(4) in r:
s.shutdown(SHUT_RDWR)
return
s.send(r)
finally:
tcsetattr(fd, TCSADRAIN, old_settings)
示例8: main
def main():
global parser
global verbose
global pretty
parser.add_argument("-v", "--verbose", dest="verbose", action='store_true',
help="verbose output")
parser.add_argument("-p", "--pretty", dest="pretty", action='store_true',
help="pretty output. requires PTable module")
parser.add_argument("-S", "--section", dest="section", default=None,
help="""\
define a section in the following format;
NAME SIZE SYM1 SYM2 SYM3
use SIZE as zero to skip size usage calculation
\
""", action='append')
options = parser.parse_args()
is_pipe = not isatty(stdin.fileno())
if not is_pipe or not options.section:
usage()
quit(1)
if options.verbose:
verbose = True
if options.pretty:
pretty = True
sections = parse_arg_sections(options.section)
sizes = parse(sections)
output(sizes)
示例9: _daemonize
def _daemonize(self):
try:
pid = os.fork()
if pid > 0:
exit()
except OSError as e:
error(_('Error entering daemon mode: %s') % e.strerror)
exit()
os.chdir('/')
os.setsid()
os.umask(0)
stdout.flush()
stderr.flush()
si = open(os.devnull, 'r')
so = open(os.devnull, 'a+')
se = open(os.devnull, 'a+')
os.dup2(si.fileno(), stdin.fileno())
os.dup2(so.fileno(), stdout.fileno())
os.dup2(se.fileno(), stderr.fileno())
on_exit(self._quit)
old_log = getLogger()
if old_log.handlers:
for handler in old_log.handlers:
old_log.removeHandler(handler)
log(filename=self.logfile, level=self.loglevel,
format='%(asctime)s %(levelname)-8s %(message)s')
self._set_pid()
示例10: pos
def pos(stream=stdout):
'''Get cursor position.'''
# Save term settings
fd = stdin.fileno()
prev = termios.tcgetattr(fd)
stream.write("\033[6n")
resp = ""
ch = ''
# Immitate getch() until 'R' (end of tty response, e.g. \033[1;1R)
try:
tty.setraw(fd)
while ch!='R':
ch = stdin.read(1)
resp += ch
finally:
# Reset term mode
termios.tcsetattr(fd, termios.TCSADRAIN, prev)
try:
# First two chars in response are \033 and [, last is R
return [int(c) for c in resp[2:-1].split(';')]
except:
# In case of failure
return [-1, -1]
示例11: get_terminal_size_posix
def get_terminal_size_posix():
def ioctl_GWINSZ(fd):
try:
import fcntl
import termios
cr = struct.unpack('hh',
fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
return cr
except:
pass
cr = ioctl_GWINSZ(stdin.fileno()) or \
ioctl_GWINSZ(stdout.fileno()) or \
ioctl_GWINSZ(stderr.fileno())
if not cr:
try:
with os.open(os.ctermid(), os.O_RDONLY) as fd:
cr = ioctl_GWINSZ(fd)
except:
pass
if not cr:
try:
cr = (os.environ['LINES'], os.environ['COLUMNS'])
except:
pass
if not cr:
raise TerminalError('cannot determine terminal size from POSIX')
return int(cr[1]), int(cr[0])
示例12: daemonize
def daemonize(self):
"""
Forks the process(es) from the controlling terminal
and redirects I/O streams for logging.
"""
self.fork()
chdir(getcwd())
setsid()
umask(0)
self.fork()
stdout.flush()
stderr.flush()
si= file(self.stdin, 'w+')
so= file(self.stdout, 'a+')
se= file(self.stderr, 'a+', 0)
dup2(si.fileno(), stdin.fileno())
dup2(so.fileno(), stdout.fileno())
dup2(se.fileno(), stderr.fileno())
register(self.del_pid)
self.set_pid()
示例13: enableEchoMode
def enableEchoMode():
fd = stdin.fileno()
state = tcgetattr(fd)
if state[TERMIO_LFLAGS] & ECHO:
return False
state[TERMIO_LFLAGS] = state[TERMIO_LFLAGS] | ECHO
tcsetattr(fd, TCSADRAIN, state)
return True
示例14: clear
def clear(): # LIMPANDO A TELA
import curses
import termios
from sys import stdin
fd = stdin.fileno()
scr = termios.tcgetattr(fd)
scrn = curses.initscr()
scrn.clear()
termios.tcsetattr(fd, termios.TCSADRAIN, scr)
示例15: daemonize
def daemonize(self):
try:
pid = fork()
if pid > 0:
# exit first parent
_exit(0)
except OSError as e:
stderr.write(
"fork #1 failed: {0:d} ({1:s})\n".format(
e.errno, str(e)
)
)
raise SystemExit(1)
# decouple from parent environment
chdir(self.path)
setsid()
umask(0)
# do second fork
try:
pid = fork()
if pid > 0:
# exit from second parent
_exit(0)
except OSError as e:
stderr.write(
"fork #2 failed: {0:d} ({1:s})\n".format(
e.errno, str(e)
)
)
raise SystemExit(1)
# redirect standard file descriptors
stdout.flush()
stderr.flush()
maxfd = getrlimit(RLIMIT_NOFILE)[1]
if maxfd == RLIM_INFINITY:
maxfd = 2048
closerange(0, maxfd)
si = open(self.stdin, "r")
so = open(self.stdout, "a+")
se = open(self.stderr, "a+")
dup2(si.fileno(), stdin.fileno())
dup2(so.fileno(), stdout.fileno())
dup2(se.fileno(), stderr.fileno())
self.fire(writepid())
self.fire(daemonized(self))