PortIO is a wrapper or binding library for those low-level port input and output C functions, such as outb(2) and inb(2):


#include <sys/io.h>

unsigned char inb(unsigned short int port);
void outb(unsigned char value, unsigned short int port);

It supports the complete list of these I/O functions: outb, outw, outl, outsb, outsw, outsl, outb_p, outw_p, outl_p, inb, inw, inl, insb, insw, insl, inb_p, inw_p, inl_p, ioperm, iopl.

Almost four years ago, I wrote this C code for disabling i8042 keyboard:


#include <unistd.h>
#include <sys/io.h>

#define I8042_COMMAND_REG 0x64

int main(int argc, char *argv[]) {
char data = 0xae; // enable keyboard

ioperm(I8042_COMMAND_REG, 1, 1);

if (argc == 2 && argv[1][0] == '0')
data = 0xad; // disable keyboard
outb(data, I8042_COMMAND_REG);
return 0;
}

With the help of PortIO, as C extension of Python, I now could port it to Python:


#!/usr/bin/env python

import sys

from portio import ioperm, outb

I8042_COMMAND_REG = 0x64


def main():

data = 0xae # enable keyboard

ioperm(I8042_COMMAND_REG, 1, 1)

if len(sys.argv) == 2 and sys.argv[1] == '0':
data = 0xad # disable keyboard

outb(data, I8042_COMMAND_REG)


if __name__ == '__main__':
main()

PortIO is written by Fabrizio Pollastri under the GPLv2 License, currently version 0.5 (2012-10-25/2014-05-28), it works for both Python 2 and 3.