Python Scripting

software development


I am trying to get my simple hello world python script running on DH. I call it with the URL:

and the file contains two lines:

print "Content-Type: text/plain\n\n"
print “Hello World!”

I am getting an error 500 when I run this script.
Can anyone tell me what I am doing wrong?

Herb Lainchbury
Dynamic Solutions Inc.


You forgot the shebang line, which is used to determine what program to pass the file to.[code]-------------------------------

print "Content-Type: text/plain\n\n"
print “Hello World!”
-------------------------------[/code]Don’t forget to chmod it to 755 as well.

:cool: Perl / MySQL / HTML+CSS


OK, sounds reasonable. Now the system won’t let me chmod to 755 (using smartftp). Can you help with that as well?
Thanks very much.

Herb Lainchbury
Dynamic Solutions Inc.


Hmm… still no go.
What am I missing…

Script with shebang is now:
print "Content-Type: text/plain\n\n"
print “Hello World!”

chmod is 755

url is

When I call it I get:

500 Internal Server Error
blah blah blah…

Any other ideas? Thanks for the help so far guys.

Hey, do I need an .htaccess file? I don’t for php but is python treated differently?

Herb Lainchbury
Dynamic Solutions Inc.


Thanks Bob.
I have cgi enabled… permissions are 755 on the file but my account doesn’t allow me to chmod the directory. Is that necessary?

If so, I am not sure how to do that. The account my website is in (account A) only allows FTP accesss, which doesn’t have chmod. The account (account A) wont let me telnet in. So, I can telnet in with another user (account B), switch to the place where my site is (account A’s home dir) but apparently I don’t have authority (as that user B).

Does anyone have this working?

Thanks for your help and patience.


Herb Lainchbury
Dynamic Solutions Inc.


I have cgi enabled… permissions are 755 on the file but my account doesn’t allow me to chmod the directory. Is that necessary?[/quote]
Yes. Well, the permissions on the directory have to disable world-write IIRC.

What do you mean, doesn’t have chmod? Are you using a client that does not support that function, or do you get errors when trying to use it? I use CuteFTP Pro, it has chmod function in it, and I do not get any errors using it with an FTP-only user on DreamHost.

Correct. Each file or directory is owned by a particular user and the user must set the appropiate permissions for allowing others access. There are three classes of permissions: owner, group, and public. It is not wise to let a ‘public’ user make changes to executable files you own, so DreamHost has it setup so that scripts will not execute if the public has the ability to change them. Needless to say a user should not be able to change the permissions on a file that is not their own.

:cool: Perl / MySQL / HTML+CSS


OK, I admit, that was a bit dense… thanks for the FTP tip… yes of course I can chmod through my client, duh! Done.

So, I did that, now the file and the directory are 755. And STILL no go.

I’ll triple check everything and try again. Thanks.

Herb Lainchbury
Dynamic Solutions Inc.


OK, so I noticed that you are running your script from a subdirectory called test-py… so naturally I tried that too, thinking that maybe I just can’t do it from the root of my website… wrong. Still no go. Now I the same folder as you, with presumably the same script and both are chmod 755. And, it doesn’t work on mine… exactly the same error as before:
500 internal server error

So, I checked in the log and this is the error I get in the log:

failed to open log file
fopen: Permission denied
[Sun Dec 14 20:08:01 2003] [error] [client] Premature end of script headers: /home/hlainchb/

Does this help?

I see that your script works. Cool.

Thanks again.

Herb Lainchbury
Dynamic Solutions Inc.


LOL, no, thanks for asking though. I wish that was it.

Herb Lainchbury
Dynamic Solutions Inc.


Not sure if this will help,
Is the output correct when run from the shell?
Did you upload the file using ASCII mode, if on Windows or Mac?

:cool: Perl / MySQL / HTML CSS


Well, I thought of that. I can run the script from the shell using the command:
and it works fine.
Thanks Atropos7

Herb Lainchbury
Dynamic Solutions Inc.


OK, so I asked the DH people to help finally and now it is working. Just wanted to finish the thread in case someone else comes along and experiences the same thing.

It turns out everything is set up OK but the script itself had some bad characters in it. I am working from a windoze box you see and I guess either the editor I am using put them in or I am doing my FTP wrong or whatever. Anyway, they copied and cleaned up my script and it works fine now. I can finally get to work on my site now.

Thanks DH.

Herb Lainchbury
Dynamic Solutions Inc.


On DOS/Windows, the end of a line consists of a carriage return character followed by a line feed character (). On Unix, the end of a line is marked by just a line feed character (). On Mac OS, the end of a line is marked by a carriage return ().

Some programs, such as the Python interpreter, have problems when you try to use a file that was formatted for the wrong platform. You can tell you have a DOS formatted file on Unix if you open the file in a text editor on the Unix system and some or all of the lines end with a ^M character.

One way to fix the problem on each file is to use the dos2unix program on the Unix box. You will need to get a shell connection, such as via telnet or ssh.

After you cd into the directory with the file, just enter:

If you want to see what other options dos2unix offers, enter:
$dos2unix -h

You can use standard shell wildcards to apply it to multiple files in a directory. The following command will convert all the .py files in the current directory:

$dos2unix *.py

Another option is to use a Windows or Mac text editor that has an option for saving files with Unix line endings. This is a good option if you aren’t comfortable using the Unix shell.



Or, you can FTP the files in ASCII mode instead of binary, and the platform-specific line breaks will be converted automatically.

– Dan