can python do some kernel stuff?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jimmy

    can python do some kernel stuff?

    Hi to all

    python now has grown to a versatile language that can
    accomplish tasks for many different purposes. However,
    AFAIK, little is known about its ability of kernel coding.

    So I am wondering if python can do some kernel coding that
    used to be the private garden of C/C++. For example, can python
    intercept the input of keyboard on a system level? someone told me
    it's a kernel thing, isn't it?
  • Andrew Lee

    #2
    Re: can python do some kernel stuff?

    Jimmy wrote:
    Hi to all
    >
    python now has grown to a versatile language that can
    accomplish tasks for many different purposes. However,
    AFAIK, little is known about its ability of kernel coding.
    >
    So I am wondering if python can do some kernel coding that
    used to be the private garden of C/C++. For example, can python
    intercept the input of keyboard on a system level? someone told me
    it's a kernel thing, isn't it?



    Comment

    • Jimmy

      #3
      Re: can python do some kernel stuff?

      On May 23, 3:05 pm, Andrew Lee <fiacre.patr... @gmail.comwrote :
      Jimmy wrote:
      Hi to all
      >
      python now has grown to a versatile language that can
      accomplish tasks for many different purposes. However,
      AFAIK, little is known about its ability of kernel coding.
      >
      So I am wondering if python can do some kernel coding that
      used to be the private garden of C/C++. For example, can python
      intercept the input of keyboard on a system level? someone told me
      it's a kernel thing, isn't it?
      >
      http://wiki.python.org/moin/elmer
      well, straightly speaking, how can I know a key is pressed on a system-
      level if
      using python?

      Comment

      • Diez B. Roggisch

        #4
        Re: can python do some kernel stuff?

        Jimmy schrieb:
        On May 23, 3:05 pm, Andrew Lee <fiacre.patr... @gmail.comwrote :
        >Jimmy wrote:
        >>Hi to all
        >>python now has grown to a versatile language that can
        >>accomplish tasks for many different purposes. However,
        >>AFAIK, little is known about its ability of kernel coding.
        >>So I am wondering if python can do some kernel coding that
        >>used to be the private garden of C/C++. For example, can python
        >>intercept the input of keyboard on a system level? someone told me
        >>it's a kernel thing, isn't it?
        >http://wiki.python.org/moin/elmer
        >
        well, straightly speaking, how can I know a key is pressed on a system-
        level if
        using python?
        What has that todo with kernel programming? You can use e.g. pygame to
        get keystrokes. Or under linux, read (if you are root) the keyboard
        input file - I've done that to support several keyboards attached to a
        machine.

        And the original question: no, python can't be used as kernel
        programming language. Amongst other reasons, performance & the GIL
        prevent that.

        Diez

        Comment

        • Andrew Lee

          #5
          Re: can python do some kernel stuff?

          Jimmy wrote:
          On May 23, 3:05 pm, Andrew Lee <fiacre.patr... @gmail.comwrote :
          >Jimmy wrote:
          >>Hi to all
          >>python now has grown to a versatile language that can
          >>accomplish tasks for many different purposes. However,
          >>AFAIK, little is known about its ability of kernel coding.
          >>So I am wondering if python can do some kernel coding that
          >>used to be the private garden of C/C++. For example, can python
          >>intercept the input of keyboard on a system level? someone told me
          >>it's a kernel thing, isn't it?
          >http://wiki.python.org/moin/elmer
          >
          well, straightly speaking, how can I know a key is pressed on a system-
          level if
          using python?
          Source code: Lib/curses The curses module provides an interface to the curses library, the de-facto standard for portable advanced terminal handling. While curses is most widely used in the Unix en...


          Unless you are using an ancient piece of hardware -- a terminal is a
          pseudo terminal and a key stroke isn't a kernel event at all.

          If you were looking for examples of kernel level functions you might
          want to consider driver interfaces -- how to program device interfaces
          -- that, too, can be done in Python -- but, again, you are always
          calling some underlying C function exposed via SWIG or another cross
          compilation tool. Python doesn't reinvent system calls! :-)

          Comment

          • Andrew Lee

            #6
            Re: can python do some kernel stuff?

            Diez B. Roggisch wrote:
            Jimmy schrieb:
            >On May 23, 3:05 pm, Andrew Lee <fiacre.patr... @gmail.comwrote :
            >>Jimmy wrote:
            >>>Hi to all
            >>>python now has grown to a versatile language that can
            >>>accomplish tasks for many different purposes. However,
            >>>AFAIK, little is known about its ability of kernel coding.
            >>>So I am wondering if python can do some kernel coding that
            >>>used to be the private garden of C/C++. For example, can python
            >>>intercept the input of keyboard on a system level? someone told me
            >>>it's a kernel thing, isn't it?
            >>http://wiki.python.org/moin/elmer
            >>
            >well, straightly speaking, how can I know a key is pressed on a system-
            >level if
            >using python?
            >
            What has that todo with kernel programming? You can use e.g. pygame to
            get keystrokes. Or under linux, read (if you are root) the keyboard
            input file - I've done that to support several keyboards attached to a
            machine.
            >
            And the original question: no, python can't be used as kernel
            programming language. Amongst other reasons, performance & the GIL
            prevent that.
            >
            Diez


            Of course you can code kernel routines in Python -- you are just calling
            the underlying C interface. The GIL means you have to manage
            threadsafety on your own -- it doesn't imply kernel programming can not
            be done.

            I'm not talking about writing an OS in Python (though a simple DOS-like
            OS is very possible). Nor would I suggest writing device drivers in
            Python -- but of course you can call kernel routines and manage some
            kernel resources effectively. Python's IPC libraries expose kernel
            functionality.

            Comment

            • Diez B. Roggisch

              #7
              Re: can python do some kernel stuff?

              Andrew Lee schrieb:
              Diez B. Roggisch wrote:
              >Jimmy schrieb:
              >>On May 23, 3:05 pm, Andrew Lee <fiacre.patr... @gmail.comwrote :
              >>>Jimmy wrote:
              >>>>Hi to all
              >>>>python now has grown to a versatile language that can
              >>>>accomplis h tasks for many different purposes. However,
              >>>>AFAIK, little is known about its ability of kernel coding.
              >>>>So I am wondering if python can do some kernel coding that
              >>>>used to be the private garden of C/C++. For example, can python
              >>>>intercept the input of keyboard on a system level? someone told me
              >>>>it's a kernel thing, isn't it?
              >>>http://wiki.python.org/moin/elmer
              >>>
              >>well, straightly speaking, how can I know a key is pressed on a system-
              >>level if
              >>using python?
              >>
              >What has that todo with kernel programming? You can use e.g. pygame to
              >get keystrokes. Or under linux, read (if you are root) the keyboard
              >input file - I've done that to support several keyboards attached to a
              >machine.
              >>
              >And the original question: no, python can't be used as kernel
              >programming language. Amongst other reasons, performance & the GIL
              >prevent that.
              >>
              >Diez
              >

              >
              Of course you can code kernel routines in Python -- you are just calling
              the underlying C interface. The GIL means you have to manage
              threadsafety on your own -- it doesn't imply kernel programming can not
              be done.
              I understood the OP's question as "can one program kernelspace routines
              in python". Which I don't think is possible. And I don't see how py-pf
              does that either.

              Diez

              Comment

              • Andrew Lee

                #8
                Re: can python do some kernel stuff?

                Diez B. Roggisch wrote:
                Andrew Lee schrieb:
                >Diez B. Roggisch wrote:
                >>Jimmy schrieb:
                >>>On May 23, 3:05 pm, Andrew Lee <fiacre.patr... @gmail.comwrote :
                >>>>Jimmy wrote:
                >>>>>Hi to all
                >>>>>python now has grown to a versatile language that can
                >>>>>accompli sh tasks for many different purposes. However,
                >>>>>AFAIK, little is known about its ability of kernel coding.
                >>>>>So I am wondering if python can do some kernel coding that
                >>>>>used to be the private garden of C/C++. For example, can python
                >>>>>intercep t the input of keyboard on a system level? someone told me
                >>>>>it's a kernel thing, isn't it?
                >>>>http://wiki.python.org/moin/elmer
                >>>>
                >>>well, straightly speaking, how can I know a key is pressed on a system-
                >>>level if
                >>>using python?
                >>>
                >>What has that todo with kernel programming? You can use e.g. pygame
                >>to get keystrokes. Or under linux, read (if you are root) the
                >>keyboard input file - I've done that to support several keyboards
                >>attached to a machine.
                >>>
                >>And the original question: no, python can't be used as kernel
                >>programming language. Amongst other reasons, performance & the GIL
                >>prevent that.
                >>>
                >>Diez
                >>
                >http://www.kernel-panic.it/programming/py-pf/
                >>
                >Of course you can code kernel routines in Python -- you are just
                >calling the underlying C interface. The GIL means you have to manage
                >threadsafety on your own -- it doesn't imply kernel programming can
                >not be done.
                >
                I understood the OP's question as "can one program kernelspace routines
                in python". Which I don't think is possible. And I don't see how py-pf
                does that either.
                >
                Diez

                OP: "I am wondering if python can do some kernel coding that
                used to be the private garden of C/C++."

                The answer is yes. IPC and py-pf are examples. If you don't think of
                packet filtering as kernel coding, I can understand. But clearly the
                Python interfaces to fork(), waitpid(), signal(), alarm() and so forth
                are forays into the once private garden of C.

                Comment

                • Diez B. Roggisch

                  #9
                  Re: can python do some kernel stuff?

                  >
                  OP: "I am wondering if python can do some kernel coding that
                  used to be the private garden of C/C++."
                  "kernel coding" is pretty clear I'd say - coding a or in the kernel. Not
                  coding that runs on an OS that happens to have a kernel.
                  The answer is yes. IPC and py-pf are examples. If you don't think of
                  packet filtering as kernel coding, I can understand. But clearly the
                  Python interfaces to fork(), waitpid(), signal(), alarm() and so forth
                  are forays into the once private garden of C.
                  Also not "kernel coding" in my book. system-near coding - yes. But not
                  coding a kernel...

                  Diez

                  Comment

                  • Jimmy

                    #10
                    Re: can python do some kernel stuff?

                    On May 23, 5:53 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
                    Jimmy schrieb:
                    >
                    On May 23, 3:05 pm, Andrew Lee <fiacre.patr... @gmail.comwrote :
                    Jimmy wrote:
                    >Hi to all
                    >python now has grown to a versatile language that can
                    >accomplish tasks for many different purposes. However,
                    >AFAIK, little is known about its ability of kernel coding.
                    >So I am wondering if python can do some kernel coding that
                    >used to be the private garden of C/C++. For example, can python
                    >intercept the input of keyboard on a system level? someone told me
                    >it's a kernel thing, isn't it?
                    >http://wiki.python.org/moin/elmer
                    >
                    well, straightly speaking, how can I know a key is pressed on a system-
                    level if
                    using python?
                    >
                    What has that todo with kernel programming? You can use e.g. pygame to
                    get keystrokes. Or under linux, read (if you are root) the keyboard
                    input file - I've done that to support several keyboards attached to a
                    machine.
                    >
                    And the original question: no, python can't be used as kernel
                    programming language. Amongst other reasons, performance & the GIL
                    prevent that.
                    >
                    Diez
                    sorry, my aim is not limited to one particular program. Yes, many
                    library can
                    permit you to respond to keyboard event, however, what I want is a
                    universal
                    function. as long as a key is pressed, no matter where, my program can
                    repond.

                    I am quite strange with this topic. But according to my understanding,
                    any event, keyboard event
                    for example, once triggered, will be dilivered by keyboard driver to X
                    system, and then
                    any running program can either choose to respond or ignore. So my
                    question can be translated to:
                    how to make my program respond ?

                    Comment

                    • Jimmy

                      #11
                      Re: can python do some kernel stuff?

                      On May 23, 11:14 pm, Jimmy <mcknight0...@g mail.comwrote:
                      On May 23, 5:53 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
                      >
                      >
                      >
                      Jimmy schrieb:
                      >
                      On May 23, 3:05 pm, Andrew Lee <fiacre.patr... @gmail.comwrote :
                      >Jimmy wrote:
                      >>Hi to all
                      >>python now has grown to a versatile language that can
                      >>accomplish tasks for many different purposes. However,
                      >>AFAIK, little is known about its ability of kernel coding.
                      >>So I am wondering if python can do some kernel coding that
                      >>used to be the private garden of C/C++. For example, can python
                      >>intercept the input of keyboard on a system level? someone told me
                      >>it's a kernel thing, isn't it?
                      >>http://wiki.python.org/moin/elmer
                      >
                      well, straightly speaking, how can I know a key is pressed on a system-
                      level if
                      using python?
                      >
                      What has that todo with kernel programming? You can use e.g. pygame to
                      get keystrokes. Or under linux, read (if you are root) the keyboard
                      input file - I've done that to support several keyboards attached to a
                      machine.
                      >
                      And the original question: no, python can't be used as kernel
                      programming language. Amongst other reasons, performance & the GIL
                      prevent that.
                      >
                      Diez
                      >
                      sorry, my aim is not limited to one particular program. Yes, many
                      library can
                      permit you to respond to keyboard event, however, what I want is a
                      universal
                      function. as long as a key is pressed, no matter where, my program can
                      repond.
                      >
                      I am quite strange with this topic. But according to my understanding,
                      any event, keyboard event
                      for example, once triggered, will be dilivered by keyboard driver to X
                      system, and then
                      any running program can either choose to respond or ignore. So my
                      question can be translated to:
                      how to make my program respond ?
                      maybe I'd better elaborate on my question. Back to my original
                      question:
                      intercept keyboard event on a system level. If you are writing program
                      in
                      emacs, of course, the keyboard inputs are meant for emacs only. What
                      I
                      want is no matter what program you're running, keyboard events can be
                      anyway caught by my program.

                      Am I clear with myself? :)

                      Comment

                      • Diez B. Roggisch

                        #12
                        Re: can python do some kernel stuff?

                        Jimmy schrieb:
                        On May 23, 11:14 pm, Jimmy <mcknight0...@g mail.comwrote:
                        >On May 23, 5:53 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
                        >>
                        >>
                        >>
                        >>Jimmy schrieb:
                        >>>On May 23, 3:05 pm, Andrew Lee <fiacre.patr... @gmail.comwrote :
                        >>>>Jimmy wrote:
                        >>>>>Hi to all
                        >>>>>python now has grown to a versatile language that can
                        >>>>>accompli sh tasks for many different purposes. However,
                        >>>>>AFAIK, little is known about its ability of kernel coding.
                        >>>>>So I am wondering if python can do some kernel coding that
                        >>>>>used to be the private garden of C/C++. For example, can python
                        >>>>>intercep t the input of keyboard on a system level? someone told me
                        >>>>>it's a kernel thing, isn't it?
                        >>>>http://wiki.python.org/moin/elmer
                        >>>well, straightly speaking, how can I know a key is pressed on a system-
                        >>>level if
                        >>>using python?
                        >>What has that todo with kernel programming? You can use e.g. pygame to
                        >>get keystrokes. Or under linux, read (if you are root) the keyboard
                        >>input file - I've done that to support several keyboards attached to a
                        >>machine.
                        >>And the original question: no, python can't be used as kernel
                        >>programming language. Amongst other reasons, performance & the GIL
                        >>prevent that.
                        >>Diez
                        >sorry, my aim is not limited to one particular program. Yes, many
                        >library can
                        >permit you to respond to keyboard event, however, what I want is a
                        >universal
                        >function. as long as a key is pressed, no matter where, my program can
                        >repond.
                        >>
                        >I am quite strange with this topic. But according to my understanding,
                        >any event, keyboard event
                        >for example, once triggered, will be dilivered by keyboard driver to X
                        >system, and then
                        >any running program can either choose to respond or ignore. So my
                        >question can be translated to:
                        >how to make my program respond ?
                        >
                        maybe I'd better elaborate on my question. Back to my original
                        question:
                        intercept keyboard event on a system level. If you are writing program
                        in
                        emacs, of course, the keyboard inputs are meant for emacs only. What
                        I
                        want is no matter what program you're running, keyboard events can be
                        anyway caught by my program.
                        >
                        Am I clear with myself? :)
                        Do you want to intercept the call (prevent that it is passed through to
                        e.g. emacs), or are you merely interested in getting it? If the latter,
                        you can (as root) access the /dev/input keyboard device and get the
                        scan-codes.

                        The former is more complicated - without research I don't know out of my
                        head how to accomplish that. But it must be possible, as e.g. KDE
                        observes global key-shortcuts. Most probably a X-server thing.



                        Diez

                        Comment

                        • Jimmy

                          #13
                          Re: can python do some kernel stuff?

                          On May 24, 12:34 am, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
                          Jimmy schrieb:
                          >
                          >
                          >
                          On May 23, 11:14 pm, Jimmy <mcknight0...@g mail.comwrote:
                          On May 23, 5:53 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
                          >
                          >Jimmy schrieb:
                          >>On May 23, 3:05 pm, Andrew Lee <fiacre.patr... @gmail.comwrote :
                          >>>Jimmy wrote:
                          >>>>Hi to all
                          >>>>python now has grown to a versatile language that can
                          >>>>accomplis h tasks for many different purposes. However,
                          >>>>AFAIK, little is known about its ability of kernel coding.
                          >>>>So I am wondering if python can do some kernel coding that
                          >>>>used to be the private garden of C/C++. For example, can python
                          >>>>intercept the input of keyboard on a system level? someone told me
                          >>>>it's a kernel thing, isn't it?
                          >>>>http://wiki.python.org/moin/elmer
                          >>well, straightly speaking, how can I know a key is pressed on a system-
                          >>level if
                          >>using python?
                          >What has that todo with kernel programming? You can use e.g. pygame to
                          >get keystrokes. Or under linux, read (if you are root) the keyboard
                          >input file - I've done that to support several keyboards attached to a
                          >machine.
                          >And the original question: no, python can't be used as kernel
                          >programming language. Amongst other reasons, performance & the GIL
                          >prevent that.
                          >Diez
                          sorry, my aim is not limited to one particular program. Yes, many
                          library can
                          permit you to respond to keyboard event, however, what I want is a
                          universal
                          function. as long as a key is pressed, no matter where, my program can
                          repond.
                          >
                          I am quite strange with this topic. But according to my understanding,
                          any event, keyboard event
                          for example, once triggered, will be dilivered by keyboard driver to X
                          system, and then
                          any running program can either choose to respond or ignore. So my
                          question can be translated to:
                          how to make my program respond ?
                          >
                          maybe I'd better elaborate on my question. Back to my original
                          question:
                          intercept keyboard event on a system level. If you are writing program
                          in
                          emacs, of course, the keyboard inputs are meant for emacs only. What
                          I
                          want is no matter what program you're running, keyboard events can be
                          anyway caught by my program.
                          >
                          Am I clear with myself? :)
                          >
                          Do you want to intercept the call (prevent that it is passed through to
                          e.g. emacs), or are you merely interested in getting it? If the latter,
                          you can (as root) access the /dev/input keyboard device and get the
                          scan-codes.
                          >
                          The former is more complicated - without research I don't know out of my
                          head how to accomplish that. But it must be possible, as e.g. KDE
                          observes global key-shortcuts. Most probably a X-server thing.
                          >
                          Diez
                          thanks, right now I am content with just knowing a key is pressed.
                          as you said, I checked /etc/input/event1 which seems the input of
                          keyboard. Then I got some extremely strange code. however, how can
                          I just simply know a key is pressed?

                          Comment

                          • Ivan Illarionov

                            #14
                            Re: can python do some kernel stuff?

                            On 23 ÍÁÊ, 22:32, Jimmy <mcknight0...@g mail.comwrote:
                            [...]
                            however, how can I just simply know a key is pressed?
                            If you are on Linux, use XLib


                            You need to catch the KeyPress or KeyRelease X events.
                            while 1:
                            ev = display.next_ev ent()
                            if ev.type == X.KeyPress:
                            key_code = ev.detail
                            ....
                            ....

                            Ivan

                            Comment

                            • Ethan Furman

                              #15
                              Re: can python do some kernel stuff?

                              Andrew Lee wrote:
                              Diez B. Roggisch wrote:
                              >
                              >Andrew Lee schrieb:
                              >>
                              >>Diez B. Roggisch wrote:
                              >>>
                              >>>What has that todo with kernel programming? You can use e.g. pygame
                              >>>to get keystrokes. Or under linux, read (if you are root) the
                              >>>keyboard input file - I've done that to support several keyboards
                              >>>attached to a machine.
                              >>>>
                              >>>And the original question: no, python can't be used as kernel
                              >>>programmin g language. Amongst other reasons, performance & the GIL
                              >>>prevent that.
                              >>>>
                              >>>Diez
                              >>>
                              >>>
                              >>http://www.kernel-panic.it/programming/py-pf/
                              >>>
                              >>Of course you can code kernel routines in Python -- you are just
                              >>calling the underlying C interface. The GIL means you have to
                              >>manage threadsafety on your own -- it doesn't imply kernel
                              >>programming can not be done.
                              >>
                              >>
                              >I understood the OP's question as "can one program kernelspace
                              >routines in python". Which I don't think is possible. And I don't see
                              >how py-pf does that either.
                              >>
                              >Diez
                              >
                              >
                              >
                              OP: "I am wondering if python can do some kernel coding that
                              used to be the private garden of C/C++."
                              >
                              The answer is yes. IPC and py-pf are examples. If you don't think of
                              packet filtering as kernel coding, I can understand. But clearly the
                              Python interfaces to fork(), waitpid(), signal(), alarm() and so forth
                              are forays into the once private garden of C.
                              Being able to call routines in the kernel is *not* the same as kernel
                              coding. Calling C routines is *not* the same as kernel coding.
                              Actually writing the routines that are to be called, and that constitute
                              the kernel itself, *is* kernel coding. And as wonderful as Python is,
                              it is *not* for kernel coding.

                              Having just looked at Py-PF, it is *managing* the firewall, not
                              implementing it. Again, not kernel coding.
                              --
                              Ethan

                              Comment

                              Working...