strange python scripting error

M

Mark Tarver

I have a very strange error. I have two test python files test.py and
python.py which contain the following code

#!/usr/bin/python
print "Content-type: text/html"
print
print "<html>"
print "<center>Hello, Linux.com!</center>"
print "</html>"

One file (test.py) works; you call it up and it shows a web page with

Hello, Linux.com

The other fails with a server configuration error. Both are running
under Linux, same server, same permissions. Running a character scan
shows that both files contain the same printable characters and are
therefore typographically identical. They are absolutely the same.

The only hint at a difference I can see is that my ftp program says
the files are of unequal lengths. test.py is 129 bytes long.
python.py 134 bytes long.

A zipped folder containing both files is at

www.lambdassociates.org/weird.zip

Any ideas welcome.

Mark
 
D

Diez B. Roggisch

Mark said:
I have a very strange error. I have two test python files test.py and
python.py which contain the following code

#!/usr/bin/python
print "Content-type: text/html"
print
print "<html>"
print "<center>Hello, Linux.com!</center>"
print "</html>"

One file (test.py) works; you call it up and it shows a web page with

Hello, Linux.com

The other fails with a server configuration error. Both are running
under Linux, same server, same permissions. Running a character scan
shows that both files contain the same printable characters and are
therefore typographically identical. They are absolutely the same.

The only hint at a difference I can see is that my ftp program says
the files are of unequal lengths. test.py is 129 bytes long.
python.py 134 bytes long.

A zipped folder containing both files is at

www.lambdassociates.org/weird.zip

Any ideas welcome.

They have different line-ending-conventions. Not sure if and why that makes
a difference.

Diez
 
R

Richard Brodie

They have different line-ending-conventions. Not sure if and why that makes
a difference.

Depends on your setup. Shells can be a bit dumb about it, so
it will likely break simple cgi-style hosting.

-bash: ./python.py: /usr/bin/python^M: bad interpreter:
 
P

Peter Otten

Diez said:
They have different line-ending-conventions. Not sure if and why that
makes a difference.

Looks like the shell treats the CR as part of the interpreter name:

$ cat python.py
#!/usr/bin/python
print "Content-type: text/html"
print
print "<html>"
print "<center>Hello, Linux.com!</center>"
print "</html>"$
$ python python.py
Content-type: text/html

<html>
<center>Hello, Linux.com!</center>
</html>
$ chmod u+x python.py
$ ./python.py
bash: ./python.py: /usr/bin/python^M: bad interpreter: No such file or
directory

Peter
 
D

Dennis Lee Bieber

I have a very strange error. I have two test python files test.py and
python.py which contain the following code

said:
The other fails with a server configuration error. Both are running

Way out from left field... Have you tried renaming "python.py" to
some other name?

If the server is configured to treat .py as executable files, it
might be getting confused over "python.py" trying to invoke "python".
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
M

Mark Tarver

        Way out from left field... Have you tried renaming "python.py" to
some other name?

        If the server is configured to treat .py as executable files, it
might be getting confused over "python.py" trying to invoke "python".
--
        Wulfraed        Dennis Lee Bieber               KD6MOG
        (e-mail address removed)             (e-mail address removed)
                HTTP://wlfraed.home.netcom.com/
        (Bestiaria Support Staff:               (e-mail address removed))
                HTTP://www.bestiaria.com/

Yes; tried pyth.py - still failed.

Mark
 
M

Mark Tarver

        Just a guess...

        Line endings... <lf> vs <cr><lf>

--
        Wulfraed        Dennis Lee Bieber               KD6MOG
        (e-mail address removed)             (e-mail address removed)
                HTTP://wlfraed.home.netcom.com/
        (Bestiaria Support Staff:               (e-mail address removed))
                HTTP://www.bestiaria.com/

Is that linefeed + ctrl or what? I can't pick up any difference
reading the files char by char in Lisp. How do you find the
difference?

Mark
 
D

Dave Angel

Mark said:
I have a very strange error. I have two test python files test.py and
python.py which contain the following code

#!/usr/bin/python
print "Content-type: text/html"
print
print "<html>"
print "<center>Hello, Linux.com!</center>"
print "</html>"

One file (test.py) works; you call it up and it shows a web page with

Hello, Linux.com

The other fails with a server configuration error. Both are running
under Linux, same server, same permissions. Running a character scan
shows that both files contain the same printable characters and are
therefore typographically identical. They are absolutely the same.

The only hint at a difference I can see is that my ftp program says
the files are of unequal lengths. test.py is 129 bytes long.
python.py 134 bytes long.

A zipped folder containing both files is at

www.lambdassociates.org/weird.zip

Any ideas welcome.

Mark
Easiest explanation is that python.py has Windows-style newlines. In
other words, each line ends with 0d0a, rather than the Unix convention
of 0a.

If your server is Unix-based, it can't handle that first line, since it
has an illegal character (0d) following the

#!/usr/bin/python

line. Convert it to Unix line-endings.

DaveA
 
N

nn

Easiest explanation is that python.py has Windows-style newlines.  In
other words, each line ends with 0d0a, rather than the Unix convention
of 0a.

If your server is Unix-based, it can't handle that first line, since it
has an illegal character (0d) following the

#!/usr/bin/python

line.  Convert it to Unix line-endings.

DaveA

Use dos2unix for conversion of the longer file and try again:

http://linux.about.com/od/commands/l/blcmdl1_dos2uni.htm
 
M

Mark Tarver

That sounds the ticket - but is there anything that runs under Windows
to do the trick?

Mark- Hide quoted text -

- Show quoted text -

OK, got a version

http://www.bastet.com/

has dos2unix.exe for Windows. And it solves the problem.

Many thanks all for help on this baffling error

Mark
 
D

Dave Angel

Mark said:
OK, got a version

http://www.bastet.com/

has dos2unix.exe for Windows. And it solves the problem.

Many thanks all for help on this baffling error

Mark
I use metapad for simple text editing, see
http://en.wikipedia.org/wiki/Metapad

It's free, loads very quickly, and has a number of features I like. One
of its trivial features is to read/write files in four different
formats. In the file menu, you just choose what file-format you need now.

Another thing I'd point out is that some ftp programs will do this
conversion as the file is being sent between a local DOS machine and a
Unix machine on the internet.


DaveA
 
D

Dennis Lee Bieber

Another thing I'd point out is that some ftp programs will do this
conversion as the file is being sent between a local DOS machine and a
Unix machine on the internet.
"some ftp programs"?

Line end conversion is the whole reason, to my knowledge, for text
mode transfer, vs binary mode transfer.


SciTE also can be configured for different line ending conventions,
and has a menu command to convert the line endings in the file to the
current setting. And if one activates the option to show white-space,
one can even see the line ending...
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
D

Dave Angel

Dennis said:
"some ftp programs"?

Line end conversion is the whole reason, to my knowledge, for text
mode transfer, vs binary mode transfer.

<snip>
Perhaps I should have said "most ftp programs," or even "all ftp
programs," but I don't feel qualified to make such a generalization. In
any case, I don't currently use such automatic conversion when I'm doing
transfers because I like to be able to see exactly what's there, and I
don't have another way to directly examine the remote end.

The FTP I use is an add-on for Firefox (FireFTP), and although it has
text and "auto" modes, it's buried in the Tools->options menu, and the
main panel just does a transfer using whichever mode was selected.
Rather than risking corrupting the (mostly binary) files, I choose to
use binary mode all the time.

DaveA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top