2010-06-27 5 views
0
#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
########## THIS NOW WORKS! ########## 

UNSUITABLE_ENVIRONMENT_ERROR = \ 
    "This program requires at least Python 2.6 and Linux" 

import sys 
import struct 
import os 
from array import array 

# +++ Check environment 
try: 
    import platform # Introduced in Python 2.3 
except ImportError: 
    print >>sys.stderr, UNSUITABLE_ENVIRONMENT_ERROR 
if platform.system() != "Linux": 
    print >>sys.stderr, UNSUITABLE_ENVIRONMENT_ERROR 
if platform.python_version_tuple()[:2] < (2, 6): 
    print >>sys.stderr, UNSUITABLE_ENVIRONMENT_ERROR 

# --- Check environment 

HDIO_GETGEO = 0x301 # Linux 
import fcntl 

def get_disk_geometry(fd): 
    geometry = array('c',"XXXXXXXX") 
    fcntl.ioctl(fd, HDIO_GETGEO, geometry, True) 
    heads, sectors, cylinders, start = \ 
     struct.unpack("BBHL",geometry.tostring()) 
    return { 'heads' : heads, 'cylinders': cylinders, 'sectors': sectors, "start": start } 

from pprint import pprint 
fd=os.open("/dev/sdb", os.O_RDWR) 
pprint(get_disk_geometry(fd)) 
+0

여기에 질문이 있습니까? –

+0

그래, 왜이 코드가 작동하지 않습니까? D – user376403

+0

TypeError : ioctl에 파일 또는 파일 설명자, 정수 및 선택적으로 정수 또는 버퍼 인수가 필요합니다. 잡히지 않은 예외입니다. 사후 부검 디버깅 프로그램을 다시 시작합니다 '계속'을 또는 '단계'실행을 입력 >/홈/강탈/ricedisk (25) get_disk_geometry() -> fcntl.ioctl (FD, HDIO_GETGEO, 기하학, 참) PDB() P는 HDIO_GETGEO PDB() p 형 (HDIO_GETGEO) PDB() p 형 (FD) user376403

답변

0

왜이 일을 할 수 없는지 말할 수있는 사람은 없지만 ctypes로 처리하면 문제가되지 않습니다.

#!/usr/bin/env python 
from ctypes import * 
import os 
from pprint import pprint 

libc = CDLL("libc.so.6") 
HDIO_GETGEO = 0x301 # Linux 

class HDGeometry(Structure): 
    _fields_ = (("heads", c_ubyte), 
       ("sectors", c_ubyte), 
       ("cylinders", c_ushort), 
       ("start", c_ulong)) 

    def __repr__(self): 
     return """Heads: %s, Sectors %s, Cylinders %s, Start %s""" % (
       self.heads, self.sectors, self.cylinders, self.start) 

def get_disk_geometry(fd): 
    """ Returns the heads, sectors, cylinders and start of disk as rerpoted by 
    BIOS. These us usually bogus, but we still need them""" 

    buffer = create_string_buffer(sizeof(HDGeometry)) 
    g = cast(buffer, POINTER(HDGeometry)) 
    result = libc.ioctl(fd, HDIO_GETGEO, byref(buffer)) 
    assert result == 0 
    return g.contents 

if __name__ == "__main__": 
    fd = os.open("/dev/sdb", os.O_RDWR) 
    print repr(get_disk_geometry(fd)) 
+0

'import *'를 사용하면 네임 스페이스가 오염되어 프로그램을 읽고 디버그하기가 더 어려워집니다. 간결함이 필요한 경우'import extralongmodulename as e' 사용을 고려하십시오. –

+0

ctypes가 내가하는 유일한 모듈이며 동의합니다. 그러나 ctypes는 엉덩이의 일반적인 통증입니다. C 또는 뭔가를 가져올 것이라고 생각합니다. – user376403

관련 문제