Python, struct.pack


 
Thread Tools Search this Thread
Top Forums Programming Python, struct.pack
# 1  
Old 11-13-2010
Python, struct.pack

Hello,

I found some code on line.
Is a python function that bring up an Internet interface, here the code:
Code:
        # Get existing device flags
        ifreq = struct.pack('16sh', 'wlan0', 0)
        flags = struct.unpack('16sh', fcntl.ioctl(sockfd, SIOCGIFFLAGS, ifreq))[1]

I'm starting reading something about it. I know that the struct.pack is needed for compatibility with C struct.
But why we pack the wlan0 exactly with 16 string and 1 short (and not 30s10h)?
In my case for example I get:
Quote:
ifreq = wlan0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
and
Quote:
fcntl.ioctl(sockfd, SIOCGIFFLAGS, ifreq) give me:
wlan0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00C\x10
the final result unpacking everything is 4163.
Is it because from the fcntl.ioctl (in this case) I will expect to fill it with string and short?

Thanks

D
# 2  
Old 11-14-2010
You need to check what structure takes/return the specific ioctl() you are evoking. See your Unix documentation.

On Linux, it seems to be the interface flag, see /usr/include/net/if.h, standard interface flag enum.

HTH,
Loïc
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...

Hi all... As you know I like making code backwards compatible for as many platforms as possible. This Python script was in fact dedicated for the AMIGA A1200 using Pythons 1.4.0, 1.5.2, 1.6.0, 2.0.1, and 2.4.6 as that is all we have for varying levels of upgrades from a HDD and 4MB FastRam... (1 Reply)
Discussion started by: wisecracker
1 Replies

2. Programming

Python Results Converted To C Struct Header File

I created python code that produce output in the form of: moses-red-sea=1.00.03 genesis-snake=2.03 deliverance=5.0.010 I need to take this output and create a "C" header file and have it look like this: struct { char *name; char *fixed_version; } filename_versions... (7 Replies)
Discussion started by: metallica1973
7 Replies

3. Shell Programming and Scripting

**python** unable to read the background color in python

I am working on requirement on spreadsheet in python scripting. I have a spreadsheet containing cell values and with background color. I am able to read the value value but unable to get the background color of that particular cell. Actually my requirement is to read the cell value along... (1 Reply)
Discussion started by: giridhar276
1 Replies

4. Programming

Storing C++-struct in file - problem when adding new item in struct

Hi, I have received an application that stores some properties in a file. The existing struct looks like this: struct TData { UINT uSizeIncludingStrings; // copy of Telnet data struct UINT uSize; // basic properties: TCHAR szHost; //defined in Sshconfig UINT iPortNr; TCHAR... (2 Replies)
Discussion started by: Powerponken
2 Replies

5. Solaris

weblogic Maintenance pack

Hi ALL, I am running weblogic portal(9.2.2) on solaris and i wanted to apply maintenance pack and upgrade it to 9.2.3. Without using x-windows system how can i apply maintenance pack.? (0 Replies)
Discussion started by: gaurav183
0 Replies

6. UNIX for Dummies Questions & Answers

How to access a struct within a struct?

Can someone tell me how to do this? Just a thought that entered my mind when learning about structs. First thought was: struct one { struct two; } struct two { three; } one->two->three would this be how you would access "three"? (1 Reply)
Discussion started by: unbelievable21
1 Replies

7. UNIX for Dummies Questions & Answers

Pack current folder

How do I pack (using tar zcvf ?) the current folder inluding all files and folders ?? I need to be sure to get all files and folders/subfolders... Later I will unpack into a new folder on a new server.. Appreciate any help.. (3 Replies)
Discussion started by: WebWatch
3 Replies

8. AIX

Aix - Service Pack

I've recently installed ServicePack1 for Tecnology_Level 9 of AIX 5.2 . The result of installation is "OK" but with oslevel -s i dont see the service pack installed .... after many research i try (with many luck!!) instfix -i|grep SP and the result : All filesets for 5200-08-01_SP... (1 Reply)
Discussion started by: BabylonRocker
1 Replies

9. Shell Programming and Scripting

Perl help!! (pack()?)

Hello everyone. I wrote a perl script to get the two answers from a value: x. By this, I want to do sqrt($x) in different precision. #!/usr/bin/perl print "Input the initial value x:\n"; chomp($x=<STDIN>); $comp=sqrt($x); $float_value=pack("f", $comp); $double_value=pack("d", $comp);... (2 Replies)
Discussion started by: Euler04
2 Replies
Login or Register to Ask a Question