473,586 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading packets from a tun device.


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!
Jul 18 '05 #1
1 3460
Andre <an***@netvisio n.com.br> wrote in message news:<ma******* *************** ************@py thon.org>...
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'm not sure if this problem is due to buffering on your client side, but
you can open plain file descriptors in Python. The buffering could also
be on the server side of the tun device. What is connected to the other
side of the tun? Is that code that you wrote too? If so then send a copy
of that and I can understand better what is going on.

The 'os' package supports raw file descriptor file IO which is below stdio.

import os
fd_tun = os.open ('/dev/tun0', os.O_RDWR)
data = os.read (fd_tun, 1500)
os.close (fd_tun)

Let me know how this goes.

Yours,
Noah
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1737
by: Eric Lilja | last post by:
Hello, I have a text file that contains a number of entries describing a recipe. Each entry consists of a number of strings. Here's an example file with only one entry (recipe): Name=Maple Quill Process=Interim Level=10 Technique=Fletching Knowledge=Woodworking Device=Sawhorse Primary components=Refined Maple
1
3645
by: Ldaled | last post by:
Okay, I had a previous post called reading an XML document. Since this post I have revised my code and got it to work. Now, Like Derek had mentioned in answer to my previous post, I am getting an XmlException. The error message is invalid date SEMICOLON expected at Line 1 Position 380. Derek you had said something about the UTF encoding...
0
6649
by: rs | last post by:
Hi guys, I am trying to read from a USB keyboard using vb.net and HID classes. the USB keyboard is not my primary keyboard. I have a ps2 keyboard connected and is detected in device manager as my keyboard. the USB keyboard is detected as HID keyboard device. the program finds the keyboard if it is attached. and I am getting valid handles....
2
8749
by: rs | last post by:
Hi guys, I am trying to read from a USB keyboard using vb.net and HID classes. the USB keyboard is not my primary keyboard. I have a ps2 keyboard connected and is detected in device manager as my keyboard. the USB keyboard is detected as HID keyboard device. the program finds the keyboard if it is attached. and I am getting valid handles....
9
3591
by: Macca | last post by:
Hi, I have a synchronous socket server which my app uses to read data from clients. To test this I have a simulated client that sends 100 byte packets. I have set up the socket server so that its buffer is bigger than this. I did this expecting the data to be read in one pass.
3
4123
by: Pashlipops | last post by:
Hi there I am looking for some guidance. I am new to C++ and whilst I am reading up about the language, my actual goal is to create a program that reads a UDP packet sent by a data logger and displays the reults real-time on screen. The UDP packet is sent every 200ms. I know the port. I have Windows Vicual C++ Express Edition running...
7
2741
by: sreehari | last post by:
Hi All, I have this problem, I have developed a Master - Slave kinda appliacation , were the slave is generally a device.when i broadcast a message from my application, the Device seems to recieve the message and responds correctly, and replies with a broadcast frame. Here is where i face the problem, what happens is that my application...
1
4175
Plater
by: Plater | last post by:
Useing a packet sniffer I have been watching some tcpip traffic between my computer and my embedded device I am developing on (the embeded device runs wince 5.0) and I have been noticing that a LOT (like %40) of the packets leaving the embeded device are showing up as having a bad checksum and having to be re-sent. I was just wondering if that is...
0
1716
by: makTSPL | last post by:
Dear All, I am developing an application in C#. This application communicates with Garmin Forerunner device. I have developed a demo for it, which shows Device software version. but I am not able to get Device history Data. What should I pass for getting history data. Below is the code sample which I am using for fetching device information: ...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8200
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7954
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8215
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.