This is not really a python question, but a posix question. (If it's too
little of a python question to be OT, please let me know.)
I wanted a simple "bi-directional cat", i.e. something that passes stdin to
a device, and takes the device's output to stdout (Like 'screen
/dev/ttyS0'). I couldn't find a simple thing like this anywhere, so I
figured I would just write one.
However, I can't seem to get it to work: nothing is ever output or input on
either end (also, if run in emacs it hangs it until I can kill python--it's
eating stdin and stdout, I suspect). I suspect I have a deep ignorance of
file descriptors, but I don't understand what it could be. Might it also be
possible that the interpreter is somehow getting "in the way" of
stdin/stdout? A C-c still sends KeyboardInterrupt, for example, so I know
python is still looking at stdin.
Here's the important code, which went through some revisions:
import os, sys
from errno import EAGAIN
import fcntl
usage = """usage: scat TTY-DEV"""
def main():
try:
ttyfn = sys.argv[1]
except IndexError:
print >> sys.stderr, usage
return 1
try:
ttyfdout = os.open(ttyfn, os.O_RDONLY | os.O_NONBLOCK)
ttyfdin = os.open(ttyfn, os.O_WRONLY | os.O_NONBLOCK)
except:
print >> sys.stderr, usage
print >> sys.stderr, "invalid filename " + ttyfn
return 1
if not os.isatty(ttyfdin):
print >> sys.stderr, usage
print >> sys.stderr, ttyfn + " is not a tty"
return 1
stdin = sys.stdin.fileno()
stdout = sys.stdout.fileno()
# make the above non-blocking
fcntl.fcntl(stdin, fcntl.F_SETFL, os.O_NONBLOCK)
fcntl.fcntl(stdout, fcntl.F_SETFL, os.O_NONBLOCK)
while 1:
#read tty, write to stdout
try:
ttybuf = os.read(ttyfdout, 1)
except OSError, err:
if err == EAGAIN:
pass
else:
os.write(stdout, ttybuf)
#read stdin, write to tty
try:
stdinbuf = os.read(stdin, 1)
except OSError, err:
if err == EAGAIN:
pass
else:
os.write(ttyfdin, stdinbuf)
if __name__ == '__main__':
main()
--
Francis Avila 2 4610
Francis Avila wrote: try: ttybuf = os.read(ttyfdout, 1) except OSError, err: if err == EAGAIN: pass else: os.write(stdout, ttybuf)
Well, admitting that I don't have any idea what you want to do, the above
code silences *any* OSError, i. e.
if err == EAGAIN:
pass
is a NOOP. If you want to catch only EAGAIN, you might consider
try:
ttybuf = os.read(ttyfdout, 1)
except OSError, err:
if err != EAGAIN:
raise
else:
os.write(stdout, ttybuf)
instead.
Peter
"Peter Otten" <__*******@web.de> wrote in message
news:bl*************@news.t-online.com... Francis Avila wrote:
try: ttybuf = os.read(ttyfdout, 1) except OSError, err: if err == EAGAIN: pass else: os.write(stdout, ttybuf)
Well, admitting that I don't have any idea what you want to do, the above code silences *any* OSError, i. e.
if err == EAGAIN: pass
is a NOOP. If you want to catch only EAGAIN, you might consider
try: ttybuf = os.read(ttyfdout, 1) except OSError, err: if err != EAGAIN: raise else: os.write(stdout, ttybuf)
instead.
Peter
That's true, thanks. I was thinking a bit sloppily, catching EAGAIN as
though it were itself an exception. However, it doesn't effect functioning
unless some other fatal exception pops up.
The problem was simply that my tty wasn't raw enough. It wasn't sufficient
to make the tty raw from the shell (using, in this case, 'stty raw pass8').
Calling tty.setraw on each of the four fd's solved the problem without
incident, and the code works exactly as expected.
Of course, now there are no excape characters, so I can't even ^C to exit,
but this is all resolvable.
--
Francis Avila This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Bob Rock |
last post by:
Hello,
a simple question (if you know the answer) ... how to convert from and array
of bytes to a string (and viceversa). I haven't the conversion to be simple
... but even using the Convert or...
|
by: Shibu |
last post by:
Hi,
I have a situation where I need to convert business objects to a flat table.
The reverse is also required. I am using c# and Oracle ODP. I am looking for
an easier method to do the below...
|
by: Bhan |
last post by:
Using Ptr of derived class to point to base class and viceversa
class base
{
....
}
class derived : public base
{
....
}
|
by: Mike |
last post by:
Hi,
Does anyone know of reliable programs that convert C# to Java and viceversa?
Thanks
Mike
|
by: Nevyn Twyll |
last post by:
I have a set of libraries that do processing output to Console.Out, and
Console.Error.
How do I direct that output to a Textbox on a Windows form in CSharp?
Thanks in advance!
- Nevyn
|
by: Shibu |
last post by:
Hi,
I have a situation where I need to convert business objects to a flat table.
The reverse is also required. I am using c# and
Oracle ODP. I am looking for an easier method to do the below...
|
by: Bob Rock |
last post by:
Hello,
converting from the managed to the unmanaged world (and viceversa strings)
and byte arrays is something I do often and I'd like to identify the most
correct and efficient way to do it....
|
by: tobesurveyor via AccessMonster.com |
last post by:
Hello all,
The program I am creating takes certain files found on a Pocket PC and places
them on a specific directory on a network drive. It is automated in the
transfering of the files, and in...
|
by: tarlino |
last post by:
Hi! I'm play with the sound generation (c++ and directX).
Now I would like to manage same filter on my sample. But now I must to
convert my audio buffer "BYTE" (from DirectX) to an arry of "short"...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |