Reading packets from a tun device.

Andre andre at netvision.com.br
Sat Jul 12 19:22:22 CEST 2003


  I'm trying to read packets from a tun device in Python, the code I used 
for this is the following:

f = file( '/dev/tun0', 'r+' )
pkt = f.read( 1500 )

The problem is that the f.read() call doesn't return. When I used a small 
value in the f.read() call instead of the 1500, like 1 or 5, it did 
return the first bytes of the packet.
I then went to the manuals and found that file() uses stdio's fopen, so I 
changed my code to make file() not do any buffering, like this:

f = file( '/dev/tun0', 'r+', 0 )
pkt = f.read( 1500 )

But the behavior of the program is the same as the one above.

What I'm looking for is a way to open a file in Python without using 
stdio, or a way to read from a tun device using it.
I also tried google'ing for another Python program that uses a tun device, 
but couldn't find any yet, so if you know of a Python program that uses a 
tun device, that'd be enough for me to figure out what I'm doing wrong.

Thank you for reading all this!






More information about the Python-list mailing list