FAQ
Can anyone explain this unexpected behavior?
all files chmod 755, i've compiled x.py with py_compilefiles,
also tried within python console with "import x"

system: centos 5.4 32bit

This a PATH related problem i think?


[root at centos-14 cgi-bin]# ./x.pyc
: command not found
./x.pyc: line 2: syntax error near unexpected token `)'
./x.pyc: line 2: `??)Lc@sdGHdGHdkZeiGHdS(sContent-Type:text/
html'


[root at centos-14 cgi-bin]# python x.pyc
Content-Type:text/html


python
2.4.3 (#1, Sep 3 2009, 15:37:12)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-46)]

--------------------------------------------------------------------------------------------

x.py file content:

#!/usr/bin/env python

print "Content-Type:text/html\n\n"
print "python"
import sys
print sys.version

--------------------------------------------------------------------------------------------

Search Discussions

  • Chris Rebert at Jun 30, 2010 at 8:14 am

    On Wed, Jun 30, 2010 at 1:06 AM, Baris CUHADAR wrote:
    Can anyone explain this unexpected behavior?
    all files chmod 755, i've compiled x.py with py_compilefiles,
    also tried within python console with "import x"

    system: ?centos 5.4 32bit

    This a PATH related problem i think? Nope.
    [root at centos-14 cgi-bin]# ./x.pyc
    .pyc files are just bytecode, not scripts, so you can't run them as
    scripts/executables under *nix. Run the .py file instead. Or do
    `python x.pyc` like you showed.

    Cheers,
    Chris
  • Rami Chowdhury at Jun 30, 2010 at 8:31 am

    On 2010-06-30 01:06, Baris CUHADAR wrote:
    Can anyone explain this unexpected behavior?
    I'm sorry -- can you let us know what behavior you're expecting?
    all files chmod 755, i've compiled x.py with py_compilefiles,
    also tried within python console with "import x"

    system: centos 5.4 32bit

    This a PATH related problem i think?
    Perhaps I'm misunderstanding what you're trying to do but from what I can
    see, you are expecting .pyc files to be directly executed by the shell. And
    as I understand them, they are *not* meant to be directly executed by the
    shell at all -- they're simply a sometimes-useful shortcut for the Python
    interpreter.

    Python scripts like your x.py, however, *are* meant to be executed by the
    shell. Have you tried:

    [user at box cgi-bin]$ ./x.py

    ?
    [root at centos-14 cgi-bin]# ./x.pyc
    May I ask, as well, why you're doing all of this as root?

    [root at centos-14 cgi-bin]# python x.pyc
    Content-Type:text/html


    python
    2.4.3 (#1, Sep 3 2009, 15:37:12)
    [GCC 4.1.2 20080704 (Red Hat 4.1.2-46)]

    --------------------------------------------------------------------------------------------

    x.py file content:

    #!/usr/bin/env python

    print "Content-Type:text/html\n\n"
    print "python"
    import sys
    print sys.version

    --------------------------------------------------------------------------------------------


    --
    http://mail.python.org/mailman/listinfo/python-list
  • Baris CUHADAR at Jun 30, 2010 at 8:54 am

    On Jun 30, 11:31?am, Rami Chowdhury wrote:
    On 2010-06-30 01:06, Baris CUHADAR wrote:

    Can anyone explain this unexpected behavior?
    I'm sorry -- can you let us know what behavior you're expecting?
    all files chmod 755, i've compiled x.py with py_compilefiles,
    also tried within python console with "import x"
    system: ?centos 5.4 32bit
    This a PATH related problem i think?
    Perhaps I'm misunderstanding what you're trying to do but from what I can
    see, you are expecting .pyc files to be directly executed by the shell. And
    as I understand them, they are *not* meant to be directly executed by the
    shell at all -- they're simply a sometimes-useful shortcut for the Python
    interpreter.

    Python scripts like your x.py, however, *are* meant to be executed by the
    shell. Have you tried:

    [user at box cgi-bin]$ ./x.py

    ?
    [root at centos-14 cgi-bin]# ./x.pyc
    May I ask, as well, why you're doing all of this as root?


    [root at centos-14 cgi-bin]# python x.pyc
    Content-Type:text/html
    python
    2.4.3 (#1, Sep ?3 2009, 15:37:12)
    [GCC 4.1.2 20080704 (Red Hat 4.1.2-46)]
    --------------------------------------------------------------------------------------------
    x.py file content:
    #!/usr/bin/env python
    print "Content-Type:text/html\n\n"
    print "python"
    import sys
    print sys.version
    --------------------------------------------------------------------------------------------
    --
    http://mail.python.org/mailman/listinfo/python-list
    Thank you for your replies.
    This machine is a local virtual test machine so security issues are
    omitted.

    Actually i wrote some scripts in python that are working as gateway
    controlling scripts iptables/tc/squid-proxy, and i want to execute
    them as cgi. Protection of source code is also important. These
    scripts works fine with ubuntu 9.10 and ubuntu 10.04, trying to
    eleminate this execution error and successfully port them to centos
    5.4. Or i have to rewrite these scripts in C which is time comsuming.

    On centos 5.4, i think httpd tries to execute it as i did "./x.pyc".
  • Christian Heimes at Jun 30, 2010 at 9:06 am

    Actually i wrote some scripts in python that are working as gateway
    controlling scripts iptables/tc/squid-proxy, and i want to execute
    them as cgi. Protection of source code is also important. These
    scripts works fine with ubuntu 9.10 and ubuntu 10.04, trying to
    eleminate this execution error and successfully port them to centos
    5.4. Or i have to rewrite these scripts in C which is time comsuming.
    It looks like you have the binfmt kernel feature installed and
    configured on your Ubuntu machines. Do you have some Python related
    files in /proc/sys/fs/binfmt_misc/ ?

    Christian
  • Baris CUHADAR at Jun 30, 2010 at 11:20 am

    On Jun 30, 12:06?pm, Christian Heimes wrote:
    Actually i wrote some scripts in python that are working as gateway
    controlling scripts iptables/tc/squid-proxy, and i want to execute
    them as cgi. Protection of source code is also important. These
    scripts works fine with ubuntu 9.10 and ubuntu 10.04, trying to
    eleminate this execution error and successfully port them to centos
    5.4. Or i have to rewrite these scripts in C which is time comsuming.
    It looks like you have the binfmt kernel feature installed and
    configured on your Ubuntu machines. Do you have some Python related
    files in /proc/sys/fs/binfmt_misc/ ?

    Christian
    Thanks Christian, before your message i was thinking about writing
    wrapper to turn round this issue.

    my wrapper:
    #!/usr/bin/bash

    /usr/bin/python m_file.pyc

    Yes /proc/sys/fs/binfmt_misc/python2.6 is there.

    enabled
    interpreter /usr/bin/python2.6
    flags:
    offset 0
    magic d1f20d0a

    How can make it possible in centos? With custom kernel compiling?
  • Baris CUHADAR at Jun 30, 2010 at 12:07 pm

    On Jun 30, 2:20?pm, Baris CUHADAR wrote:
    On Jun 30, 12:06?pm, Christian Heimes wrote:

    Actually i wrote some scripts in python that are working as gateway
    controlling scripts iptables/tc/squid-proxy, and i want to execute
    them as cgi. Protection of source code is also important. These
    scripts works fine with ubuntu 9.10 and ubuntu 10.04, trying to
    eleminate this execution error and successfully port them to centos
    5.4. Or i have to rewrite these scripts in C which is time comsuming.
    It looks like you have the binfmt kernel feature installed and
    configured on your Ubuntu machines. Do you have some Python related
    files in /proc/sys/fs/binfmt_misc/ ?
    Christian
    Thanks Christian, before your message i was thinking about writing
    wrapper to turn round this issue.

    my wrapper:
    #!/usr/bin/bash

    /usr/bin/python m_file.pyc

    Yes /proc/sys/fs/binfmt_misc/python2.6 is there.

    enabled
    interpreter /usr/bin/python2.6
    flags:
    offset 0
    magic d1f20d0a

    How can make it possible in centos? With custom kernel compiling?
    According to this documentation below, i've solved my problem.

    http://git.kernel.org/gitweb.cgi?p=linux/kernel/git/torvalds/linux-2.6.git;a=blob;f=Documentation/binfmt_misc.txt;hb=HEAD

    registered .pyc extension to invoke python interpreter.

    Done, thank you everybody, especially Christian..
  • Thomas Jollans at Jun 30, 2010 at 12:10 pm

    On 06/30/2010 01:20 PM, Baris CUHADAR wrote:
    On Jun 30, 12:06 pm, Christian Heimes wrote:
    Actually i wrote some scripts in python that are working as gateway
    controlling scripts iptables/tc/squid-proxy, and i want to execute
    them as cgi. Protection of source code is also important. These
    scripts works fine with ubuntu 9.10 and ubuntu 10.04, trying to
    eleminate this execution error and successfully port them to centos
    5.4. Or i have to rewrite these scripts in C which is time comsuming.
    It looks like you have the binfmt kernel feature installed and
    configured on your Ubuntu machines. Do you have some Python related
    files in /proc/sys/fs/binfmt_misc/ ?

    Christian
    Thanks Christian, before your message i was thinking about writing
    wrapper to turn round this issue.

    my wrapper:
    #!/usr/bin/bash

    /usr/bin/python m_file.pyc

    Yes /proc/sys/fs/binfmt_misc/python2.6 is there.

    enabled
    interpreter /usr/bin/python2.6
    flags:
    offset 0
    magic d1f20d0a

    How can make it possible in centos? With custom kernel compiling?
    The binfmt_misc module has to be loaded, which is presumably is, if
    /proc/sys/fs/binfmt_misc exists (not 100% sure on that)
    It's almost certainly provided by CentOS, even if it's not loaded by
    default.

    Assuming file.pyc was actually compiled by python2.6, and not some other
    version, it should work.

    -- Thomas
  • Baris CUHADAR at Jun 30, 2010 at 1:32 pm

    On Jun 30, 3:10?pm, Thomas Jollans wrote:
    On 06/30/2010 01:20 PM, Baris CUHADAR wrote:


    On Jun 30, 12:06 pm, Christian Heimes wrote:
    Actually i wrote some scripts in python that are working as gateway
    controlling scripts iptables/tc/squid-proxy, and i want to execute
    them as cgi. Protection of source code is also important. These
    scripts works fine with ubuntu 9.10 and ubuntu 10.04, trying to
    eleminate this execution error and successfully port them to centos
    5.4. Or i have to rewrite these scripts in C which is time comsuming.
    It looks like you have the binfmt kernel feature installed and
    configured on your Ubuntu machines. Do you have some Python related
    files in /proc/sys/fs/binfmt_misc/ ?
    Christian
    Thanks Christian, before your message i was thinking about writing
    wrapper to turn round this issue.
    my wrapper:
    #!/usr/bin/bash
    /usr/bin/python m_file.pyc
    Yes /proc/sys/fs/binfmt_misc/python2.6 is there.
    enabled
    interpreter /usr/bin/python2.6
    flags:
    offset 0
    magic d1f20d0a
    How can make it possible in centos? With custom kernel compiling?
    The binfmt_misc module has to be loaded, which is presumably is, if
    /proc/sys/fs/binfmt_misc exists (not 100% sure on that)
    It's almost certainly provided by CentOS, even if it's not loaded by
    default.

    Assuming file.pyc was actually compiled by python2.6, and not some other
    version, it should work.

    -- Thomas
    Yes centos5.4 loads it automatically, however little kickstart script
    in "/etc/rc.local" is required.

    is_python=$(ls -l /proc/sys/fs/binfmt_misc/python2.4 | wc -l)

    if [ $is_python -lt 1 ];
    then
    echo ':python2.4:E::pyc::/usr/bin/python:' > /proc/sys/fs/binfmt_misc/
    register
    fi

    i'm still digging in..

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJun 30, '10 at 8:06a
activeJun 30, '10 at 1:32p
posts9
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase